在this question我正在探索如何监视我的项目目录以进行更改,以便我可以保留一个显示始终最新的git diff的终端,以便我可以跳过键入{{1}一遍又一遍地进入shell并进一步简化我的工作流程。
假设我有一个足够智能的工具,可以确切地知道文件系统何时发生了变化,我怎样才能破坏git diff的显示?最终的解决方案是能够找出当前显示滚动到的位置,并重新滚动到相同的位置(合理的,因为一切都可能已经改变,当然)。我确信有一种方法可以找出缓冲区与git diff
的距离,然后将其与新实例一起传递。
但问题是如何以编程方式控制也交互的界面?这是可以做的吗?我可以使用某些重型工具(如tmux)将输入发送到程序中吗?根据我自定义tmux来做很多事情的经验,它确实能够从shell接收命令并执行shell命令,并且它有一个发送密钥的命令。
答案 0 :(得分:2)
这是什么?一个接近投票?也许有人认为这不可能。 ;)
我向您展示了完整的解决方案(您可以在github上找到最新版本):
〜/ 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:
#!/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
这是光荣的。通过the super fast fswatch
program即时更新。
什么也很好,是因为fswatch
虚假地解雇(不幸的是它)。如果我的git diff
未更改,则不会通过tmux重新运行,因此会保留滚动偏移。