【问题标题】:How does __git_ps1 update the bash prompt?__git_ps1 如何更新 bash 提示符?
【发布时间】:2019-06-25 22:17:38
【问题描述】:

我是一名 Windows 用户,我使用 Git Bash shell 作为我的日常驱动程序。我很好奇__git_ps1 函数是如何在您每次更改目录时更新提示的。这确实是我见过的即时更新 bash 提示的唯一示例。如果我打开了 RDP 会话,我想在我自己的函数中利用这种行为来在我的提示符上添加一个显示。

tldr:关于 __git_ps1 函数如何动态评估 bash 提示的任何想法????

所以这是我查看 RDP 客户端是否正在运行的简单函数

function __rdp_ps1() {
  local MATCH=
  if tasklist | grep --quiet mstsc; then
    MATCH="\e[41mRDP\e[0m"
  fi
  echo "$MATCH"
}

所以我的想法是我想用红色背景显示 RDP,并且我希望我的 shell 以 __git__ps1 看似能够的方式即时评估它。

到目前为止我所调查的(没有真正成功)

/etc/profile.d/git-prompt.sh

这个块似乎创建了我的 shell 正在使用的 PS1

    PS1='\[\033]0;$TITLEPREFIX:$PWD\007\]' # set window title
    PS1="$PS1"'\n'                 # new line
    PS1="$PS1"'\[\033[32m\]'       # change to green
    PS1="$PS1"'\u@\h '             # user@host<space>
    PS1="$PS1"'\[\033[35m\]'       # change to purple
    PS1="$PS1"'$MSYSTEM '          # show MSYSTEM
    PS1="$PS1"'\[\033[33m\]'       # change to brownish yellow
    PS1="$PS1"'\w'                 # current working directory
    if test -z "$WINELOADERNOEXEC"
    then
        GIT_EXEC_PATH="$(git --exec-path 2>/dev/null)"
        COMPLETION_PATH="${GIT_EXEC_PATH%/libexec/git-core}"
        COMPLETION_PATH="${COMPLETION_PATH%/lib/git-core}"
        COMPLETION_PATH="$COMPLETION_PATH/share/git/completion"
        if test -f "$COMPLETION_PATH/git-prompt.sh"
        then
            . "$COMPLETION_PATH/git-completion.bash"
            . "$COMPLETION_PATH/git-prompt.sh"
            PS1="$PS1"'\[\033[36m\]'  # change color to cyan

# 在这里尝试了 hamjamming PS1="$PS1 `__rdp_ps1`",它只在登录时有效

            PS1="$PS1"'`__git_ps1`'   # bash function
        fi
    fi
    PS1="$PS1"'\[\033[0m\]'        # change color
    PS1="$PS1"'\n'                 # new line
    PS1="$PS1"'$ '                 # prompt: always $

所以我去看看这个文件的来源,看看是否能找到答案

/etc/bash.bashrc

最后一行是金牌

# Fixup git-bash in non login env
shopt -q login_shell || . /etc/profile.d/git-prompt.sh`

所以我评估了shopt login_shell 并且它总是打开,但我真的不知道这意味着什么,因为评论让我相信当登录 env 关闭时,提示脚本将被评估

有什么想法吗???

【问题讨论】:

  • @melpomene 我不明白你在说什么——我正在使用命令替换并且我得到了正确的显示,它只是在登录时被评估。你能解释一下吗?
  • 您在问题中没有提到这一点。你在哪里/如何设置你的PS1 来调用__rdp_ps1
  • # 在这里尝试了 hamjamming PS1="$PS1 __rdp_ps1",它只在登录时有效

标签: bash git shell


【解决方案1】:

您的问题可能是您使用双引号定义了$PS1,bash 在执行时会对其进行解释。这意味着 __rdp_ps1 在定义 $PS1 时运行。

在您的.bashrc 中,尝试将定义替换为:

PS1='$PS1 `__rdp_ps1`' # Note the single quote. 

我的 PS1 上有一个类似的功能(但在后台显示作业数量),这是完整版(可在此处获得:https://github.com/padawin/dotfiles/blob/master/.bashrc#L70):

function j(){
    jobs | wc -l | egrep -v ^0 | sed -r 's/^([0-9]+)/ (\1)/'
}

PROMPT_COMMAND=__prompt_command # Func to gen PS1 after CMDs

__prompt_command() {
    local EXIT="$?" # This needs to be first
    PS1="$(virtual_env_name)"

    local RCol='\[\e[0m\]'

    local Red='\e[0;31m'
    local Gre='\e[0;32m'
    local Blu='\e[1;34m'

    PS1+="${Gre}\u@\h$(j)${RCol}: ${Red}\w${Blu}$(__git_ps1)"
    if [ $EXIT != 0 ]; then
        PS1+="$Red \342\234\226 (${EXIT})"
    else
        PS1+="$Gre \342\234\224"
    fi
    PS1+="$RCol\n> "
}

.bashrc中可以简化为:

function j(){
    jobs | wc -l | egrep -v ^0 | sed -r 's/^([0-9]+)/ (\1)/'
}

PS1='\u$(j) > ' # Note the single quote here

其行为如下:

padawin > vim

[1]+  Stopped                 vim
padawin (1) > fg
vim
padawin > 

【讨论】:

    【解决方案2】:

    您正在寻找的是PROMPT_COMMAND。 Bash 将在显示提示之前执行其中的任何内容。如果您的 PS1 正在即时更新,您可能已经有一个 PROMPT_COMMAND

    【讨论】:

      猜你喜欢
      • 2012-04-28
      • 2013-03-01
      • 2013-10-12
      • 2012-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多