Git 使用current命令生成鱼壳提示

Git 使用current命令生成鱼壳提示,git,shell,prompt,fish,Git,Shell,Prompt,Fish,我正在尝试在fish shell提示符中生成git状态。我遇到的问题是获取git状态有点慢。因此,我希望将当前git状态保存在一个全局变量中,并且只在用户运行某些命令(例如“cd”和“git”)时更新它。我试图获取用户使用“history”命令执行的最后一个命令,但结果好坏参半。这是我的密码: function update_git_status --description 'Calculate new git current branch based on location and set _

我正在尝试在fish shell提示符中生成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 dir,则需要执行另一个命令才能将git_状态更新为正确的字符串

是否有其他方法在需要生成提示之前执行命令

[更新]

以下是终端输出,用于尝试并显示实际问题:

~> 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目录(在本例中为.oh my fish)时,分支名称应该立即出现。然而,只有在我执行另一个命令之后,它才最终出现。我认为“echo$history[1]”在命令行上返回正确的值,但在prompt方法中运行时不会返回


顺便说一下,这里有一个指向我的github repo的链接,其中包含所有这些代码:

您可以使用
echo$history[1]

您可能需要检查/usr/share/fish/functions中的函数

您可以折叠开关盒:

  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✓> 

我发现这是一个已知的问题,尚未修复


实际的问题是,将项目添加到历史记录中是在线程中完成的,在fish_提示符后执行这些都是很好的建议(我已经更新了代码)。但是,它们没有解决核心问题,即历史记录没有正确显示最后执行的命令。我将尝试在上面添加更多细节。我查看了提示。这很好,但它没有显示我的分行在前/后多少,这是我喜欢的。