events - 如何等待某些文本显示在 Pane 中

标签 events tmux

在 shell 脚本中,我希望能够在等待命令更改 Pane 中的某些内容后将 key 发送到 tmux session 。

这是我的用例之一:

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

这些命令可以通过其输出触发发送键命令,但如果有更好的方法我会倾听。

我的第一个想法(实际上对于我简单的第一个用例来说是可以的)是一个笨拙的

  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
  }

现在我可以了

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

但是 在 shell 中实现超时已经很棘手,而且忙碌的等待无论如何都是丑陋的。

是否有一些命令(或实现它的方法)会阻塞,直到某个 文本显示在 Pane 中,例如

tmux 等待文本 -t ...“Hello World”&& tmux 发送 key ...

可能有超时。

或者也许我以错误的方式处理这个问题?

最佳答案

您可以在 Linux 中使用内置的超时可执行文件并运行子 shell。尝试这样的事情:

# $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

关于events - 如何等待某些文本显示在 Pane 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25184643/

相关文章:

android sdk公共(public)变量在onClick()事件中访问

javascript - Meteor template.find 未定义

terminal - Tmuxinator Pane 代码不起作用

terminal - 从文件加载 tmux session

java - java swing中多个多边形上的鼠标事件

jquery - 监听文本框中的变化

macos - 如何使用 tmux 向下滚动鼠标滚轮

terminal - 使用 tmux 在终端中进行鼠标滚动和选择

terminal - tmux: Pane 分割线显示终端之间的差异

java - 鼠标事件: the order of operations