2009-10-17
■[Zsh][Git] git のブランチ名を zsh の右プロンプトに表示+ status に応じて色もつけてみた

Update
改良しました→ git のブランチ名 *と作業状態* を zsh の右プロンプトに表示+ status に応じて色もつけてみた - ヤルキデナイズドだった
以下古い情報
こんなんなります。
- 作業ディレクトリがクリーンなら緑
- 追跡されていないファイルがあるときは黄色
- 追跡されているファイルに変更があるときは赤
- 変更あり+未追跡ファイルありで太字の赤
そんな感じで。
コード
# ${fg[...]} や $reset_color をロード autoload -U colors; colors function rprompt-git-current-branch { local name st color if [[ "$PWD" =~ '/\.git(/.*)?$' ]]; then return fi name=$(basename "`git symbolic-ref HEAD 2> /dev/null`") if [[ -z $name ]]; then return fi st=`git status 2> /dev/null` if [[ -n `echo "$st" | grep "^nothing to"` ]]; then color=${fg[green]} elif [[ -n `echo "$st" | grep "^nothing added"` ]]; then color=${fg[yellow]} elif [[ -n `echo "$st" | grep "^# Untracked"` ]]; then color=${fg_bold[red]} else color=${fg[red]} fi # %{...%} は囲まれた文字列がエスケープシーケンスであることを明示する # これをしないと右プロンプトの位置がずれる echo "%{$color%}$name%{$reset_color%} " } # プロンプトが表示されるたびにプロンプト文字列を評価、置換する setopt prompt_subst RPROMPT='[`rprompt-git-current-branch`%~]'
gist に貼っておきました:http://gist.github.com/214109
補足
- プロンプト文字列の展開についての詳細:Encountered a 404 error
- ${fg[...]} で指定できる色一覧:% autoload -U colors; where colors
トラックバック - http://d.hatena.ne.jp/uasi/20091017/1255712789
リンク元
- 52 http://pipes.yahoo.com/pipes/pipe.info?_id=tDfBdGWF3RGl9XNm1L3fcQ
- 28 http://reader.livedoor.com/reader/
- 27 http://pipes.yahoo.com/pipes/pipe.info?_id=12e453e301454b799b3ac6642aa089b5
- 25 http://veadardiary.blog29.fc2.com/blog-entry-2377.html
- 21 http://b.hatena.ne.jp/hotentry/it
- 14 http://www.google.com/reader/view/
- 11 http://pipes.yahoo.com/pipes/pipe.info?_id=aj8hCPy13RGtsx651L3fcQ
- 8 http://pipes.yahoo.com/pipes/pipe.info?_id=faa858a20082ef6d25ad27557e37e011
- 8 http://twitter.com/
- 8 http://www.google.co.jp/reader/view/



とかどうですか?
ありがとうございます。