DRYな備忘録

Don't Repeat Yourself.

zshのプロンプトにgitのブランチ名とステータスを色で表示したい【zsh】【git】【PROMPT】

つくった

f:id:otiai10:20131219122511p:plain

ソースコード

将来の自分のためにできるだけ何やってるか分かりやすく書いた

zshのプロンプトにブランチ名とかステータスとか出すアレ

背景

zshの設定で、プロンプトにgitのブランチ名とステータスを色で表示したいので、まあかつてプラグインを設定したりしたので今出てるんだけど、マシンが変わったり新しい環境で作業しなきゃならん時に、インターネッツ探して設定するのめんどくさいからもういいや自作しちゃえ勉強にもなるしと思ったのでつくった

追記 2014/03/14

gitのバージョンが上がってgit statusの出力が変わったぽくて「いちいち対応すんの嫌だなー」と思ってたらgit status --shortっていうオプションがあったことを知ったので、ちょっと変えた

@@ -30,14 +30,14 @@ function get-branch-name {
 }
 function get-branch-status {
     local res color
-        output=`git status 2> /dev/null`
-        if [[ -n `echo $output | grep "^nothing to commit"` ]]; then
+        output=`git status --short 2> /dev/null`
+        if [ -z "$output" ]; then
             res=':' # status Clean
             color='%{'${fg[green]}'%}'
-        elif [[ -n `echo $output | grep "^# Untracked files:"` ]]; then
+        elif [[ $output =~ "[\n]?\?\? " ]]; then
             res='?:' # Untracked
             color='%{'${fg[yellow]}'%}'
-        elif [[ -n `echo $output | grep "^# Changes not staged for commit:"` ]]; then
+        elif [[ $output =~ "[\n]? M " ]]; then
             res='M:' # Modified
             color='%{'${fg[red]}'%}'
         else

DRYな備忘録