How to wait for some text to appear in the panel
In a shell script, I would like to be able to send keys to the tmux session after waiting for commands to change anything in the panel.
Here's one of my options:
tmux send-keys -t ... 'vi .' c-m # This opens NERDTree
sleep 3 # Sometimes sleep 2 is not enough
tmux send-keys -t ... c-w l # Go to tab right
Commands can run the send key command by outputting them, but if there is a better way I would listen.
The first idea I had, and actually good for my simple first use case, was clumsy
function wait_for_text_event
{
while :; do
tmux capture-pane -t ... -p | grep "string triggering completion" && return 0
done
# never executed unless a timeout mechanism is implemented
return 1
}
Now I can do
tmux send-keys -t ... 'vi .' c-m
wait_for_text_event 'README.md' # meaning NERDTree has opened the split window
tmux send-keys -t ... c-w l # Go to tab right
However, Implementing timeouts gets trickier in the shell, and busy waiting is ugly anyway.
Is there any command (or a way to implement it) that just blocks until some text is displayed in the panel, eg.
tmux wait-for-text -t ... "Hello World" && & tmux send-keys ...
possibly with a timeout.
Or maybe I am approaching this the wrong way?
source to share
You can use the built-in timeout executable in Linux and run a subshell. Try something like this:
# $1: string to grep for
# $2: timeout in s
wait_for_text_event()
{
timeout $2 bash <<EOT
while :; do
sleep 1
echo "test" | grep $1 2>&1 > /dev/null && break
done
EOT
echo $?
}
echo $(wait_for_text_event "text" 5) # this times out, returncode !=0
echo $(wait_for_text_event "test" 5) # this passes, returncode == 0
source to share