2011-02-19 53 views
3

我在OsX上定製了我的bash提示,包括git分支和分支狀態的一些標記。這打破了換行。破碎的bash提示包裝線

我知道I have to add \[ and \] to prevent this issue,但在函數中這樣做確實顯示\ [和\] litteraly。

我能做些什麼來逃避這些功能的序列?

免責聲明:這些是我第一次嘗試bash腳本。

function parse_git_dirty { 
    # TODO make git status response a variable 
    # [branch+] : working dir has staged changes 
    if [[ $(git status 2> /dev/null | grep "to be committed") ]] 
    then S=$S"$(tput setaf 2)+$(tput sgr0)" 
    fi 
    # [branch+] : working dir has unstaged changes 
    if [[ $(git status 2> /dev/null | grep "not staged for commit") ]] 
    then S=$S"$(tput setaf 1)+$(tput sgr0)" 
    fi 
    # [branch+] : working dir has untracked files 
    if [[ $(git status 2> /dev/null | grep "tracked files") ]] 
    then S=$S"$(tput setaf 1)+$(tput sgr0)" 
    fi 
    # [branch<] : local branch is behind origin 
    if [[ $(git status 2> /dev/null | grep "Your branch is behind") ]] 
    then S=$S"$(tput setaf 5)<$(tput sgr0)" 
    fi 
    # [branch>] : local branch is ahead origin 
    if [[ $(git status 2> /dev/null | grep "branch is ahead of") ]] 
    then S=$S"$(tput setaf 5)>$(tput sgr0)" 
    fi 
    # [branch<>] : branches have diverged 
    if [[ $(git status 2> /dev/null | grep "have diverged") ]] 
    then S=$S"$(tput setaf 5)<>$(tput sgr0)" 
    fi 
    echo $S 
} 
function parse_git_branch { 
    git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/' 
} 
function show_git_branch { 
    if [[ $(parse_git_branch) ]] 
    then echo "$(tput setaf 2)($(tput sgr0)$(parse_git_branch)$(parse_git_dirty)$(tput setaf 2))$(tput sgr0)" 
    fi 
} 
export PS1="\u\[$(tput setaf 2)\]@\[$(tput sgr0)\]\h\[$(tput setaf 2)\]:\[$(tput sgr0)\]\W\[\$(show_git_branch)\] " 
+0

不確定問題是什麼,你的代碼似乎正常工作,使用它我得到以下提示,hutcho @ hutcho-M17x:math(master +)。順便說一句,這是一個很酷的想法。 –

+0

我有一些(不是所有不常見的)存儲庫,其中'git status'需要大約40秒才能完成,並且您的代碼將爲每個bash提示符運行'git status'六次!我在下面添加了一個答案,提到'__git_ps1',它可能做你想做的事情,並且可以在輸出中針對不同級別的細節進行配置。 –

回答

5

我很高興聽到你已經解決了您的版本問題,但我想這可能是值得指出的是Git已經與分佈式一個有用的和仔細思考的bash函數,名爲__git_ps1,您可以在您的PS1中包含該函數。例如,你可以使用它像這樣:

export PS1='blah blah blah$(__git_ps1 " (%s)") ' 

如果你在一個Git倉庫是不是,$(__git_ps1 " (%s)")會變成空字符串。但是,如果你是,那麼將使用格式字符串。這通常會向您顯示您當前的分支,但是如果您處於合併或轉義的中間位置,將會顯示。

默認情況下,__git_ps1不會顯示樹是否髒或存在未跟蹤的文件,因爲在某些存儲庫中,這可能會讓您的bash提示出現過於緩慢。但是,如果您也想看到這些信息,那麼如果您將GIT_PS1_SHOWDIRTYSTATEGIT_PS1_SHOWUNTRACKEDFILES設置爲非空,它會顯示它們。

你可以在git-completion.sh source file頂部找到更多的信息。

+0

我知道我不需要學習bash ......謝謝! –

+0

有趣的是,我是如何重新發明輪子的,並且對於骯髒的狀態以及本地與遠程狀態,得到與__git_ps1幾乎相同的標記。 –

3

您在作業需要大約值單引號:

export PS1='\u\[$(tput setaf 2)\]@\[$(tput sgr0)\]\h\[$(tput setaf 2)\]:\[$(tput sgr0)\]\W\[$(show_git_branch)\] ' 

由於內容進行評估發出提示時,你沒有,你會在其他情況下需要雙引號。

+0

謝謝!我只需要刪除show_git_branch函數的escpaing並且它工作。 –

+0

@BenoîtPointet:對不起,我錯過了。我糾正了我的答案。 –

1

感謝丹尼斯,更正後的代碼是:

function parse_git_dirty { 
    # TODO make git status response a variable 
    # [branch+] : working dir has staged changes 
    if [[ $(git status 2> /dev/null | grep "to be committed") ]] 
    then S=$S"$(tput setaf 2)+$(tput sgr0)" 
    fi 
    # [branch+] : working dir has unstaged changes 
    if [[ $(git status 2> /dev/null | grep "not staged for commit") ]] 
    then S=$S"$(tput setaf 1)+$(tput sgr0)" 
    fi 
    # [branch+] : working dir has untracked files 
    if [[ $(git status 2> /dev/null | grep "tracked files") ]] 
    then S=$S"$(tput setaf 1)+$(tput sgr0)" 
    fi 
    # [branch<] : local branch is behind origin 
    if [[ $(git status 2> /dev/null | grep "Your branch is behind") ]] 
    then S=$S"$(tput setaf 5)<$(tput sgr0)" 
    fi 
    # [branch>] : local branch is ahead origin 
    if [[ $(git status 2> /dev/null | grep "branch is ahead of") ]] 
    then S=$S"$(tput setaf 5)>$(tput sgr0)" 
    fi 
    # [branch<>] : branches have diverged 
    if [[ $(git status 2> /dev/null | grep "have diverged") ]] 
    then S=$S"$(tput setaf 5)<>$(tput sgr0)" 
    fi 
    echo $S 
} 
function parse_git_branch { 
    git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/' 
} 
function show_git_branch { 
    if [[ $(parse_git_branch) ]] 
    then echo "$(tput setaf 2)($(tput sgr0)$(parse_git_branch)$(parse_git_dirty)$(tput setaf 2))$(tput sgr0)" 
    fi 
} 
export PS1='\u\[$(tput setaf 2)\]@\[$(tput sgr0)\]\h\[$(tput setaf 2)\]:\[$(tput sgr0)\]\W\[$(show_git_branch)\] '