简体   繁体   English

OSX El Capitan 终端/.bash_profile 颜色错误

[英]OSX El Capitan terminal/.bash_profile color bug

I recently updated to El Capitan and I've been seeing some issues with my terminal, and I narrowed it down to my .bash_profile.我最近更新到 El Capitan,我发现我的终端出现了一些问题,我将其范围缩小到我的 .bash_profile。 I have this in my .bash_profile, so the prompt will change color based on changes in git.我的 .bash_profile 中有这个,所以提示会根据 git 中的变化改变颜色。

# A more colorful prompt
# \[\e[0m\] resets the color to default color
c_reset='\[\e[0m\]'
# \e[0;31m\ sets the color to red
c_path='\[\e[0;31m\]'
# \e[0;32m\ sets the color to green
c_git_clean='\[\e[0;32m\]'
# \e[0;31m\ sets the color to red
c_git_dirty='\[\e[0;31m\]'

It was working with the latest update from OSX Yosemite.它正在使用来自 OSX Yosemite 的最新更新。 Also, so far as I can tell, the color codes are correct.另外,据我所知,颜色代码是正确的。 However, this is how my terminal appears:但是,这就是我的终端的显示方式:

github.io [\[\e[0;31m\]working\[\e[0m\]]:>

As you can see, I am on my "working" branch of a github directory.如您所见,我在 github 目录的“工作”分支上。 Anything that is not on github appears normal.任何不在 github 上的东西看起来都很正常。

Downloads:> 

As of right now, I've switched to iTerm which doesn't seem to have the issue for the latest version (which is updated to accomodate El Capitan).截至目前,我已切换到 iTerm,它似乎没有最新版本的问题(已更新以适应 El Capitan)。 Leaving me to think it is a terminal issue rather than github.让我认为这是一个终端问题而不是 github。

终端截图

I found that tput setaf worked well for me.我发现tput setaf对我来说效果很好。 Documentation here.文档 在这里。

# A more colorful prompt
# \[\e[0m\] resets the color to default color
c_reset=$(tput setaf 0)
#  \e[0;31m\ sets the color to purple
c_path=$(tput setaf 55)
# \e[0;32m\ sets the color to green
c_git_clean=$(tput setaf 2)
# \e[0;31m\ sets the color to red
c_git_dirty=$(tput setaf 9)

To see all the colors, I ran one of the scripts from that documentation:要查看所有颜色,我运行了该文档中的一个脚本:

for C in {0..255}; do
    tput setaf $C
    echo -n "$C "
done
tput sgr0
echo

Then whatever colors you want, you know the number to insert after 'tput setaf '然后,无论您想要什么颜色,您都知道要在 'tput setaf ' 后插入的数字

Side note: it looks like we have the same bash_profile source.旁注:看起来我们有相同的 bash_profile 源。 I also found that upgrading to El Capitan broke it.我还发现升级到 El Capitan 会破坏它。 You can also fix the path color by adding a semicolon in the middle of this line:您还可以通过在此行中间添加分号来修复路径颜色:

# PS1 is the variable for the prompt you see everytime you hit enter
PROMPT_COMMAND=$PROMPT_COMMAND'; PS1="${c_path}\W${c_reset}$(git_prompt) :> "'

That seems to have fixed my path name color as well.这似乎也修复了我的路径名颜色。 :) :)

It may be simplest to just set all of these to the default terminal color.将所有这些设置为默认终端颜色可能是最简单的。 That escape sequence appears to be working correctly in your terminal example:该转义序列似乎在您的终端示例中正常工作:

# A more colorful prompt
# \[\e[0m\] resets the color to default color
c_reset='\[\e[0m\]'
# \e[0;31m\ sets the color to red
c_path='\[\e[0m\]'
# \e[0;32m\ sets the color to green
c_git_clean='\[\e[0m\]'
# \e[0;31m\ sets the color to red
c_git_dirty='\[\e[0m\]'

If that does not work.如果那不起作用。 see if you have access to tput and get the needed colors from that.看看您是否可以访问tput并从中获取所需的颜色。 Or finally you can try using ANSI escape sequences或者最后你可以尝试使用 ANSI 转义序列

Perhaps try experimenting with ANSI escape sequences in the terminal using printf :也许尝试在终端中使用printf尝试使用 ANSI 转义序列:

printf "\033[32m This will appear green on most terminals\n"

printf "\033[31m This will appear red on most terminals\n"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM