2015-10-06 158 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

相關問題