2013-04-07 36 views
2

this question我正在探索如何去监视我的项目目录的变化,以便我可以让终端显示始终最新的git diff,这样我就可以跳过一遍又一遍地输入git diff进入shell并进一步简化我的工作流程。从外部(父母)脚本控制寻呼机(如较少的程序)

假设我有一个足够聪明的工具,可以确切地知道文件系统何时发生变化,我该如何去干扰显示git diff?最终的解决方案将能够找出当前显示滚动到的位置,并重新滚动到相同的位置(当然合理,因为所有内容都可能已被更改)。我确信有一种方法可以找出在less缓冲区内有多远,然后将其与新的实例一起传递。

但这里的问题是如何以编程方式控制一个接口,即交互?这是可能做到的吗?我能否使用某些重载工具(如tmux)将输入发送到程序中?根据我的经验,定制tmux可以做很多事情,它具有从shell接收命令以及执行shell命令的功能,并且它具有发送密钥的命令。

+0

只是想说至少有一个开发人员(这个!)仍然对这个程序化解决方案的信息感兴趣(不需要'tmux'包装器)。 – ELLIOTTCABLE 2014-10-04 08:32:12

+0

包装并没有真正的工作。呃,实际上,也许它确实有效。尽管如此,它只是可怕的,所以我永远不会使用它。现在好消息是,iTerm2最近获得了这项功能。你可以在自己的任期窗口滚动一整天!然而,它仍然不能在tmux内部工作,所以这很吸引人。 – 2014-10-04 09:19:24

回答

2

这是什么?近距离投票?也许有人认为这是无法完成的。 ;)

我呈现给你完整的解决方案(你可以找到最最新版本的github):

〜/ UTIL /混帐的Diff-木偶:

#!/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执行重新运行,因此将保留滚动偏移量。