如何使用多行提示保留 git_ps1 呈现的颜色?

How to preserve colours rendered by git_ps1 with a multiline prompt?

我有以下 .bash_profile

#Change alias for ls to include colours
alias ls='ls -Gh'

#Enable git branch completion
source ~/git-completion.bash

#Allows git information to be visible in prompt
source ~/git-prompt.sh

GIT_PS1_SHOWDIRTYSTATE=1
GIT_PS1_SHOWCOLORHINTS=1

# ANSI colors: http://www.tldp.org/HOWTO/Bash-Prompt-HOWTO/x329.html
RED="\[3[0;31m\]"
YELLOW="\[3[0;33m\]"
GREEN="\[3[0;32m\]"
CYAN="\[3[0;36m\]"
LIGHT_GREY="\[3[0;37m\]"
DARK_GREY="\[3[1;30m\]"
NO_COLOUR="\[3[0m\]"

##################################
#Configure multiline prompt
# Prompt appearance should be:
#[RED]user@host[\RED] [CYAN]working_directory[\CYAN] git_branch [COLOUR_FROM_GIT]asterix_indicator[COLOUR_FROM_GIT] ==>
#==>
##################################
#This works for a multiline prompt and no colours
PS1='\[3[0;31m\]\u@\h\[3[0m\] \[3[0;36m\]\w\[3[0m\] $(__git_ps1 "(%s)")==> \n==>'

根据 git-prompt.sh 文件,我应该在设置 GIT_PS1_SHOWDIRTYSTATEGIT_PS1_SHOWCOLORHINTS 标志时看到颜色。如果有任何未提交的更改,我应该会看到一个红色星号。我只看到一个绿色星号。

一旦发生这种情况,我就能够得到红色星号并保存我的 bash_profile。但是,当打开一个新终端时,我的更改消失了。

任何想法为什么:

我正在使用 OSX Sierra。

谢谢!

编辑:我已经接近我想要使用的地方了: PROMPT_COMMAND='__git_ps1 "\[3[0;31m\]\u@\h\[3[0m\]:\[3[0;36m\]\w\[3[0m\]" "\$ "' 我暂时放弃了对颜色使用变量名。但是,如果我尝试在其中插入换行符,例如: PROMPT_COMMAND='__git_ps1 "[\033[0;31m]\u@\h[\033[0m]:[\033[ 0;36m]\w[\033[0m]" "\\$ \n$"' 并将 '$' 替换为 '==>' 然后它会破坏 git 分支信息。有什么建议吗?

According to the git-prompt.sh file, I should see colours when setting the GIT_PS1_SHOWDIRTYSTATE and GIT_PS1_SHOWCOLORHINTS flags.

在那个文件中你可以阅读(强调我的):

The colors are based on the colored output of "git status -sb" and are available only when using __git_ps1 for PROMPT_COMMAND or precmd.

因此,为了看到彩色提示,您需要对 PROMPT_COMMAND 使用 __git_ps1。在脚本顶部有一个如何执行此操作的示例:

PROMPT_COMMAND='__git_ps1 "\u@\h:\w" "\$ "'