Skip to content

Instantly share code, notes, and snippets.

@sinfu
Created October 29, 2010 06:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sinfu/653021 to your computer and use it in GitHub Desktop.
Save sinfu/653021 to your computer and use it in GitHub Desktop.
zshの右プロンプトにgitブランチと時刻表示するやつ
#------------------------------------------------------------------------------#
# 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}"
}
# 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