Created
October 29, 2010 06:21
-
-
Save sinfu/653021 to your computer and use it in GitHub Desktop.
zshの右プロンプトにgitブランチと時刻表示するやつ
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
#------------------------------------------------------------------------------# | |
# Utility Functions | |
#------------------------------------------------------------------------------# | |
# For PROMPT. | |
color_prompt() | |
{ | |
typeset -A colors | |
colors=( 'K' 'black' 'R' 'red' 'G' 'green' 'Y' 'yellow' \ | |
'B' 'blue' 'M' 'magenta' 'C' 'cyan' 'W' 'white' ) | |
color=$colors[$1] | |
msg="$2" | |
echo -n "%F{${color}}${msg}%f" | |
} | |
# For usual echo. | |
color_text() | |
{ | |
typeset -A esc_codes | |
esc_codes=( 'K' 30 'R' 31 'G' 32 'Y' 33 \ | |
'B' 34 'M' 35 'C' 36 'W' 37 ) | |
esc=$esc_codes[$1] | |
msg="$2" | |
echo -n "[${esc}m${msg}[m" | |
} | |
# Enumerates ancestor directories including the working directory. | |
ancestor_directories() | |
{ | |
curdir="${1:-${PWD}}" | |
while true | |
do | |
echo "${curdir}" | |
curdir=$( dirname "${curdir}" ) | |
[[ "${curdir}" == / ]] && break | |
done | |
} | |
# Returns the current branch name of the local git repository. | |
git_current_branch() | |
{ | |
git branch | while read row | |
do | |
if [[ -z "${row:#\* *}" ]] | |
then | |
echo -n "${row#\* }" | |
break | |
fi | |
done | |
} | |
#------------------------------------------------------------------------------# | |
# Hooks | |
#------------------------------------------------------------------------------# | |
_sinfu_update_prompt() | |
{ | |
info='' | |
embed_git_branch() | |
{ | |
current_branch=$( git_current_branch 2> /dev/null ) || return | |
info+=$( color_prompt G "*${current_branch} " ) | |
} | |
ancestor_directories "${PWD}" \ | |
| while read dir | |
do | |
if [[ -d "${dir}/.git" ]] | |
then | |
embed_git_branch | |
break | |
fi | |
done | |
RPROMPT="${info}[%T]"; | |
} | |
precmd_functions=( _sinfu_update_prompt ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment