2011-02-19 25 views
3

gitブランチとブランチステートのマークを含むようにOsXでbashプロンプトをカスタマイズしています。これにより、行折り返しが解除されます。壊れたbashプロンプト折り返し線

私はそのことを知っていますが、関数内でそうすることで\ 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'を6回実行します!以下の答えを '__git_ps1'と言いました。おそらくあなたが望むものを行い、出力のさまざまなレベルの詳細に対して設定することができます。 –

答えて

5

あなたのバージョンとの問題を解決してきましたが、私はそれはgitのは、すでに一緒に配布されていることを指摘する価値があるかもしれないと思ったことを聞いて、私は嬉しいです有用で慎重に考え出された__git_ps1というbash関数を、PS1に含めることができます。たとえば、あなたはこのようにそれを使用することができます:あなたはgitリポジトリにない場合

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

$(__git_ps1 " (%s)")は空の文字列に変わります。しかし、あなたがそうであれば、フォーマット文字列が使用されます。これは通常あなたの現在のブランチを表示しますが、代わりに表示されるマージまたはリベースの途中であれば表示されます。

デフォルトでは、特定のリポジトリでは、bashプロンプトが表示されるのが非常に遅くなる可能性があるため、ツリーが汚れているかどうかはわかりません。ただし、この情報も表示したい場合は、GIT_PS1_SHOWDIRTYSTATEまたはGIT_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関数のエスケープ処理を削除するだけで済みました。 –

+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)\] ' 
関連する問題