首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用当前命令生成fish shell提示

使用当前命令生成fish shell提示
EN

Stack Overflow用户
提问于 2014-01-02 20:47:52
回答 2查看 2.4K关注 0票数 1

我正试图在鱼壳提示符中生成git状态。我遇到的问题是获得git状态有点慢。因此,我希望将当前的git状态保存在全局变量中,并且只在用户运行某些命令(例如"cd“和"git")时更新它。我正在尝试使用"history“命令获得用户执行的最后一个命令,但我得到的结果是混合的。这是我的代码:

代码语言:javascript
复制
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更新为正确的字符串。

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

更新

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

代码语言:javascript
复制
~> 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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-01-06 06:59:11

我发现这是个已知的问题,还没有解决。

https://github.com/fish-shell/fish-shell/issues/984

实际问题是,将项添加到历史记录是在线程中完成的,该线程在fish_prompt之后执行。

票数 2
EN

Stack Overflow用户

发布于 2014-01-03 00:12:29

您可以使用echo $history[1]从历史记录中获得前面的命令

您可能需要在/usr/share/ __terlar_git_prompt /函数中检查该函数。

您可以折叠开关箱:

代码语言:javascript
复制
  switch $__last_command
    case 'git *' 'cd *' 'cd'
      update_git_status
  end

我怀疑你遇到了时间问题。如果不对其进行研究,则可能在更新$history数组之前对提示进行处理。但是,考虑使用fish附带的函数:

代码语言:javascript
复制
$ function fish_prompt; printf "%s%s> " (prompt_pwd) (__terlar_git_prompt); end
~> cd src/glennj/skel
~/s/g/skel|master✓> 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20891580

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档