我正试图在鱼壳提示符中生成git状态。我遇到的问题是获得git状态有点慢。因此,我希望将当前的git状态保存在全局变量中,并且只在用户运行某些命令(例如"cd“和"git")时更新它。我正在尝试使用"history“命令获得用户执行的最后一个命令,但我得到的结果是混合的。这是我的代码:
function update_git_status --description 'Calculate new git current branch based on location and set __git_status'
set -g __git_status (python ~/.oh-my-fish/themes/oneself/gitstatus.py)
end
function fish_right_prompt
set -l git_color (set_color red)
set -l normal (set_color normal)
# Update git current branch when certain commands are run
switch $history[1]
case 'git *' 'cd *' 'cd'
update_git_status
end
# Git status
echo "$git_color$__git_status$normal"
end这段代码的主要问题是历史并不总是因为某种原因立即返回最后一个命令。如果我运行另一个命令-- " cd“或" git”,然后将cd放入git,则需要执行另一个命令才能将git_status更新为正确的字符串。
是否有其他方法在需要生成提示符之前执行命令?
更新
下面是终端输出,以尝试显示实际问题:
~> history --clear
Are you sure you want to clear history ? (y/n)
read> y
History cleared!
~> echo $history[1]
~> ls
documents etc bin downloads Desktop media notes
~> echo $history[1]
ls
~> cd ~/.oh-my-fish/
~/oh-my-fish> echo $history[1]
cd ~/.oh-my-fish/
~/oh-my-fish> master⚡当我cd到一个git (在本例中是.噢-我的鱼),分支名称应该立即出现。但是,只有在我执行了另一个命令之后,它才最终出现。我认为"echo $history1“在命令行上返回正确的值,但在提示符方法中运行时不返回。
顺便提一下,这里有一个指向我的github的链接,它包含所有这些代码:https://github.com/oneself/oh-my-fish
发布于 2014-01-06 06:59:11
我发现这是个已知的问题,还没有解决。
https://github.com/fish-shell/fish-shell/issues/984
实际问题是,将项添加到历史记录是在线程中完成的,该线程在fish_prompt之后执行。
发布于 2014-01-03 00:12:29
您可以使用echo $history[1]从历史记录中获得前面的命令
您可能需要在/usr/share/ __terlar_git_prompt /函数中检查该函数。
您可以折叠开关箱:
switch $__last_command
case 'git *' 'cd *' 'cd'
update_git_status
end我怀疑你遇到了时间问题。如果不对其进行研究,则可能在更新$history数组之前对提示进行处理。但是,考虑使用fish附带的函数:
$ function fish_prompt; printf "%s%s> " (prompt_pwd) (__terlar_git_prompt); end
~> cd src/glennj/skel
~/s/g/skel|master✓> https://stackoverflow.com/questions/20891580
复制相似问题