【问题标题】:OSX Bash prompt displaying git statusOSX Bash 提示显示 git 状态
【发布时间】:2014-03-19 21:08:32
【问题描述】:

我试图说服我的 bash 提示符在我当前的 repo 有任何类型的未提交更改时添加颜色。

git_status() {
    printf "%s : %s" $(git rev-parse --abbrev-ref HEAD) $(git rev-parse --short=10 HEAD)
}

has_changed() {
    if [[ -n `git diff HEAD` ]]; then
        printf " - %s Δ %s"  ${Green} ${Color_Off}
    fi
}

PS1=${Purple}"\w"${Color_Off}" @ \$(git_status) \$(has_changed)\n \$ "

这部分有效,但 has_changed 函数返回转义字符而不是颜色:

~/projects/project @ master : 8675309 - \033[0;32m Δ \033[0m

bash 版本:GNU bash, version 3.2.51(1)-release (x86_64-apple-darwin13),在 OSX Mavericks 上。

【问题讨论】:

  • 如果您对第三方提示感兴趣,我强烈推荐liquidprompt。除其他外,它会将 git 状态、当前分支等放入您的提示符中。

标签: git bash environment-variables command-prompt prompt


【解决方案1】:

为了在提示中显示状态,您需要使用 PROMPT_COMMAND 环境变量。

PROMPT_COMMAND

If set, the value is interpreted as a command to execute before the printing of each primary prompt ($PS1).

这是一个简单的示例(经过测试并可以使用),您可以根据自己的需要进行调整:

PROMPT_COMMAND='echo -ne $(if [[ -n `git diff HEAD` ]]; then echo true; fi)'

还相关:What is the difference between PS1 and PROMPT_COMMAND

【讨论】:

    猜你喜欢
    • 2017-10-29
    • 2013-05-08
    • 1970-01-01
    • 1970-01-01
    • 2017-05-25
    • 2023-03-19
    • 2012-09-28
    • 2022-01-21
    • 2020-02-17
    相关资源
    最近更新 更多