本文介绍了通过外部(父)脚本控制寻呼机(如less程序)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题中,我正在探索如何监视项目目录中的更改,以便我可以保留一个始终显示最新git diff的终端,以便可以跳过在外壳中一遍又一遍地键入 git diff 并进一步简化我的工作流程.

In this question I am exploring how to go about monitoring my project directory for changes so that I can keep a terminal that shows an always up-to-date git diff so that I can skip typing git diff over and over into the shell and further streamline my workflow.

假设我有一个足够智能的工具,可以准确地知道文件系统何时更改,我该如何中断git diff的显示?最终的解决方案将能够弄清楚当前显示被滚动到的位置,然后重新滚动到相同的位置(在合理的情况下,因为可能会更改所有内容).我敢肯定,有一种方法可以使用 less 找出缓冲区中的深度,然后将其与新实例一起传递.

Supposing that I have a sufficiently intelligent tool that knows exactly when the filesystem has changed, how can I go about disrupting the display of git diff? The ultimate solution would be able to figure out where the current display is scrolled to, and re-scroll to the same position (where reasonable, as everything could potentially have been changed, of course). I'm sure there's a way to find out how far down in the buffer you are with less and then pass that along with the new instance of it.

但是这里的问题是我如何以编程方式控制也是交互式的界面?这可能吗?我可以利用某些重型工具(例如tmux)将输入发送到程序中吗?根据我对tmux进行自定义处理的经验,它确实具有从shell接收命令并执行shell命令的能力,并且确实具有发送密钥的命令.

But the question here is how do I programmatically control an interface that is also interactive? Is this something that is possible to do? Could I make use of certain heavy-duty tools like tmux, to send input into the program? In my experience of customizing tmux to do many things it does have the ability to receive commands from the shell and also to execute shell commands, and it does have a command which sends keys.

推荐答案

这是什么?接近投票?也许有人认为这是不可能的.;)

What's this? a close vote? Maybe somebody thought this couldn't be done. ;)

我向您提供了完整的解决方案(您可以在 github ):

I present to you the full solution (you can find the most up to date versions on github):

〜/util/git-diff-puppet:

~/util/git-diff-puppet:

#!/bin/sh

# This script runs git diff through tmux at the current directory, so that you can
# interactively scroll it. Listens for changes to the filesystem at this directory
# and tmux is used to issue commands to reload the diff.
set -e
[[ -f .tmp_git_diff ]] && echo "found .tmp_git_diff, exiting" && exit -1
# save the current git diff string to use for comparison
git diff > .tmp_git_diff
function cleanup {
    kill $FSWATCHPID
    rm .tmp_git_diff
    tmux kill-session -t git-diff-puppet
}
trap cleanup EXIT
tmux new-session -d -s git-diff-puppet sh
tmux send-keys -t git-diff-puppet "git diff" enter
fswatch . ~/util/git-diff-puppet-onchange &
FSWATCHPID=$!
tmux attach -t git-diff-puppet
echo "tmux finished: puppet script exiting"

〜/util/git-diff-puppet-onchange:

~/util/git-diff-puppet-onchange:

#!/bin/sh

# This script is not for invoking directly. It is for use in conjunction (as a "callback") with
# git-diff-puppet: this script will be looking for the .tmp_git_diff file
set -e

[[ ! -f .tmp_git_diff ]] && echo ".tmp_git_diff not found; i was probably invoked in error, aborting" && exit 1
# diffing the current diff with the saved diff to see if we should re-show the git diff in tmux
if ! git diff | diff - .tmp_git_diff > /dev/null; then
    tmux send-keys -t git-diff-puppet q "git diff" enter
    git diff > .tmp_git_diff
fi

这是光荣的.借助超快速的 fswatch 程序进行即时更新.

It is glorious. Instantaneous update thanks to the super fast fswatch program.

还不错的是,我对 fswatch 的虚假发射负责(不幸的是).如果我的 git diff 不变,则不会通过tmux重新运行,因此保留了滚动偏移量.

What is also nice, is that I account for fswatch firing spuriously (which it unfortunately does). If my git diff is unchanged, no re-running via tmux is done, so the scroll offset is preserved.

这篇关于通过外部(父)脚本控制寻呼机(如less程序)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 23:17