2015-10-06 122 views
2

我最近更新到El Capitan,我一直在看我的终端出现了一些问题,并将其缩小到我的.bash_profile。我在.bash_profile中有这个,所以提示会根据git的变化改变颜色。OSX El Capitan终端/ .bash_profile颜色错误

# 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\]' 

它与OSX Yosemite的最新更新一起工作。另外,据我所知,颜色代码是正确的。但是,这是我的终端如何显示:

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

正如你所看到的,我在我的github目录的“工作”分支。任何不在github上的东西都显得很正常。

Downloads:> 

至于现在,我已经切换到的iTerm这似乎并没有对最新版本的问题(这是更新,以适应埃尔卡皮坦)。让我认为这是一个终端问题,而不是github。

Screenshot of Terminal

+0

硬编码转义序列保证它只能在特定类型的终端上工作。正确的方法是使用能够为终端类型生成适当转义序列的终端功能。 – alvits

回答

1

我发现tput setaf工作很适合我。文档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) 

要看到所有的颜色,我跑了从文档中的脚本之一:

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

然后你想要的任何颜色,你知道后“tput的setaf”

插入数字

注意:它看起来像我们有相同的bash_profile来源。我还发现升级到埃尔卡皮坦打破了它。您还可以通过在该行的中间添加一个分号修复路径颜色:

# 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) :> "' 

这似乎已经解决了我的路径名的颜色为好。 :)

+0

这是最好的答案。 'tput'生成适合于终端类型的转义序列。 – alvits

0

这可能是最简单的,只是将所有的这些到默认的颜色终端。该转义序列似乎在您的终端示例中正常工作:

# 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\]' 

如果这样不起作用。看看你是否有权访问tput并从中获取所需的颜色。终于还是可以尝试使用ANSI转义序列

也许尝试使用printf终端与ANSI转义序列实验:

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

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

谢谢!我一定要检查一下。 – nathanmjpark