Last active
January 8, 2019 01:08
-
-
Save otiai10/8034038 to your computer and use it in GitHub Desktop.
zshのプロンプトにブランチ名とかステータスとか出すアレ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ----- PROMPT ----- | |
## PROMPT | |
PROMPT=$'[%*] → ' | |
## RPROMPT | |
RPROMPT=$'`branch-status-check` %~' # %~はpwd | |
setopt prompt_subst #表示毎にPROMPTで設定されている文字列を評価する | |
# {{{ methods for RPROMPT | |
# fg[color]表記と$reset_colorを使いたい | |
# @see https://wiki.archlinux.org/index.php/zsh | |
autoload -U colors; colors | |
function branch-status-check { | |
local prefix branchname suffix | |
# .gitの中だから除外 | |
if [[ "$PWD" =~ '/\.git(/.*)?$' ]]; then | |
return | |
fi | |
branchname=`get-branch-name` | |
# ブランチ名が無いので除外 | |
if [[ -z $branchname ]]; then | |
return | |
fi | |
prefix=`get-branch-status` #色だけ返ってくる | |
suffix='%{'${reset_color}'%}' | |
echo ${prefix}${branchname}${suffix} | |
} | |
function get-branch-name { | |
# gitディレクトリじゃない場合のエラーは捨てます | |
echo `git rev-parse --abbrev-ref HEAD 2> /dev/null` | |
} | |
function get-branch-status { | |
local res color | |
output=`git status --short 2> /dev/null` | |
if [ -z "$output" ]; then | |
res=':' # status Clean | |
color='%{'${fg[green]}'%}' | |
elif [[ $output =~ "[\n]?\?\? " ]]; then | |
res='?:' # Untracked | |
color='%{'${fg[yellow]}'%}' | |
elif [[ $output =~ "[\n]? M " ]]; then | |
res='M:' # Modified | |
color='%{'${fg[red]}'%}' | |
else | |
res='A:' # Added to commit | |
color='%{'${fg[cyan]}'%}' | |
fi | |
# echo ${color}${res}'%{'${reset_color}'%}' | |
echo ${color} # 色だけ返す | |
} | |
# }}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment