如何使用多行提示保留git

编程入门 行业动态 更新时间:2024-10-25 03:21:32
如何使用多行提示保留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="\[\033[0;31m\]"
YELLOW="\[\033[0;33m\]"
GREEN="\[\033[0;32m\]"
CYAN="\[\033[0;36m\]"
LIGHT_GREY="\[\033[0;37m\]"
DARK_GREY="\[\033[1;30m\]"
NO_COLOUR="\[\033[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='\[\033[0;31m\]\u@\h\\[\033[0m\] \[\033[0;36m\]\w\[\033[0m\] $(__git_ps1 "(%s)")==> \n==>'
 

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

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

任何想法为什么:

我看不到红色的星号(答案) 为什么我不能在提示中使用为颜色声明的变量名称,例如: $RED\u@\h$NO_COLOUR并且必须使用ASCII值。 使用变量名时,它们将呈现为字符串。 使用source ~\.bash_profile在更改后重新加载后,设置无法正确呈现

我正在使用OSX Sierra。

谢谢!

编辑:我已经接近我想要的地方使用: PROMPT_COMMAND='__git_ps1 "\[\033[0;31m\]\u@\h\[\033[0m\]:\[\033[0;36m\]\w\[\033[0m\]" "\\\$ "'我暂时放弃使用颜色的变量名称。 但是,如果我尝试在此插入换行符,例如:PROMPT_COMMAND ='__ git_ps1“[\ 033 [0; 31m] \ u @ \ h [\ 033 [0m]:[\ 033 [0; 36m] \ w [\ 033 [0m]“”\\ $ \ n $“'并用'==>'替换'$'然后它打破了git分支信息。 任何建议?

I have the following .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="\[\033[0;31m\]"
YELLOW="\[\033[0;33m\]"
GREEN="\[\033[0;32m\]"
CYAN="\[\033[0;36m\]"
LIGHT_GREY="\[\033[0;37m\]"
DARK_GREY="\[\033[1;30m\]"
NO_COLOUR="\[\033[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='\[\033[0;31m\]\u@\h\\[\033[0m\] \[\033[0;36m\]\w\[\033[0m\] $(__git_ps1 "(%s)")==> \n==>'
 

According to the git-prompt.sh file, I should see colours when setting the GIT_PS1_SHOWDIRTYSTATE and GIT_PS1_SHOWCOLORHINTS flags. I should see a red asterisk if there are any uncommitted changes. I only see a green asterisk.

I was able to get the red asterisk and saved my bash_profile once this occurred. However, when opening a new terminal my changes were gone.

Any ideas why:

I cannot see the red asterisk (ANSWERED) Why I cannot use the variable names declared for colours in the prompt e.g: $RED\u@\h$NO_COLOUR and have to use the ASCII values. When variable names are used they are rendered as strings. The settings did not render correctly after I reloaded it post change with source ~\.bash_profile

I am using OSX Sierra.

Thanks!

EDIT: I've gotten close to where I want using: PROMPT_COMMAND='__git_ps1 "\[\033[0;31m\]\u@\h\[\033[0m\]:\[\033[0;36m\]\w\[\033[0m\]" "\\\$ "' I've temporarily given up on using variable names for colours. However, if I try to insert a newline into this e.g: PROMPT_COMMAND='__git_ps1 "[\033[0;31m]\u@\h[\033[0m]:[\033[0;36m]\w[\033[0m]" "\\$ \n$"' and substitute '$' with '==>' then it breaks the git branch information. Any advice?

最满意答案

根据git-prompt.sh文件,我应该在设置GIT_PS1_SHOWDIRTYSTATE和GIT_PS1_SHOWCOLORHINTS标志时看到颜色。

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

颜色基于“git status -sb”的彩色输出, 仅在将__git_ps1用于PROMPT_COMMAND或precmd时可用

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

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

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

In that very file you can read (emphasis mine):

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.

So in order to see the colored hint, you need to use __git_ps1 for PROMPT_COMMAND. There's an example of how to do this at the top of the script:

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

更多推荐

本文发布于:2023-07-31 19:59:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1347279.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   提示   git

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!