1
0
Fork 0
mirror of synced 2024-11-26 17:25:35 -05:00

Rewrite minimal theme using git-info

As previous migrations of themes to git-info, this does not use its
verbose mode, so a repo with just untracked files is not considered
dirty.

It uses the newly-introduced `diverged` context and `coalesce` function
from git-info.

Closes #152
This commit is contained in:
Eric Nielsen 2017-03-15 09:43:35 -05:00 committed by Matt Hamilton
parent d56bddf4b1
commit ae771c878f

View file

@ -2,97 +2,87 @@
# Minimal theme # Minimal theme
# https://github.com/S1cK94/minimal # https://github.com/S1cK94/minimal
# #
# Requires the `git-info` zmodule to be included in the .zimrc file.
minimal_user() { # Global variables
print "%(!.$on_color.$off_color)$prompt_char%f" function {
PROMPT_CHAR=''
ON_COLOR='%F{green}'
OFF_COLOR='%f'
ERR_COLOR='%F{red}'
} }
minimal_jobs() { prompt_minimal_user() {
print "%(1j.$on_color.$off_color)$prompt_char%f" print -n '%(!.${ON_COLOR}.${OFF_COLOR})${PROMPT_CHAR}'
} }
minimal_vimode(){ prompt_minimal_jobs() {
local ret="" print -n '%(1j.${ON_COLOR}.${OFF_COLOR})${PROMPT_CHAR}'
}
case $KEYMAP in prompt_minimal_vimode() {
local color
case ${KEYMAP} in
main|viins) main|viins)
ret+="$on_color" color=${ON_COLOR}
;; ;;
vicmd) *)
ret+="$off_color" color=${OFF_COLOR}
;; ;;
esac esac
ret+="$prompt_char%f" print -n "${color}${PROMPT_CHAR}"
print "$ret"
} }
minimal_status() { prompt_minimal_status() {
print "%(0?.$on_color.$err_color)$prompt_char%f" print -n '%(0?.${ON_COLOR}.${ERR_COLOR})${PROMPT_CHAR}'
} }
minimal_path() { prompt_minimal_path() {
local path_color="%F{244}" local path_color='%F{244}'
local rsc="%f" print -n "${path_color}${$(short_pwd)//\//%f\/${path_color}}%f"
local sep="$rsc/$path_color"
print "$path_color$(sed s_/_${sep}_g <<< $(short_pwd))$rsc"
} }
git_branch_name() { prompt_minimal_git() {
local branch_name="$(command git rev-parse --abbrev-ref HEAD 2> /dev/null)" if [[ -n ${git_info} ]]; then
[[ -n $branch_name ]] && print "$branch_name" print -n " ${(e)git_info[color]}${(e)git_info[prompt]}"
}
git_repo_status(){
local rs="$(command git status --porcelain -b)"
if $(print "$rs" | grep -v '^##' &> /dev/null); then # is dirty
print "%F{red}"
elif $(print "$rs" | grep '^## .*diverged' &> /dev/null); then # has diverged
print "%F{red}"
elif $(print "$rs" | grep '^## .*behind' &> /dev/null); then # is behind
print "%F{11}"
elif $(print "$rs" | grep '^## .*ahead' &> /dev/null); then # is ahead
print "%f"
else # is clean
print "%F{green}"
fi fi
} }
minimal_git() { function zle-line-init zle-keymap-select {
local bname=$(git_branch_name)
if [[ -n ${bname} ]]; then
local infos="$(git_repo_status)${bname}%f"
print " $infos"
fi
}
function zle-line-init zle-line-finish zle-keymap-select {
zle reset-prompt zle reset-prompt
zle -R zle -R
} }
prompt_minimal_precmd() { prompt_minimal_precmd() {
zle -N zle-line-init (( ${+functions[git-info]} )) && git-info
zle -N zle-keymap-select
zle -N zle-line-finish
PROMPT='$(minimal_user)$(minimal_jobs)$(minimal_vimode)$(minimal_status) '
RPROMPT='$(minimal_path)$(minimal_git)'
} }
prompt_minimal_setup() { prompt_minimal_setup() {
prompt_char="" zle -N zle-line-init
on_color="%F{green}" zle -N zle-keymap-select
off_color="%f"
err_color="%F{red}"
autoload -Uz colors && colors
autoload -Uz add-zsh-hook autoload -Uz add-zsh-hook
prompt_opts=(cr percent subst)
add-zsh-hook precmd prompt_minimal_precmd add-zsh-hook precmd prompt_minimal_precmd
prompt_opts=(cr subst percent)
zstyle ':zim:git-info:branch' format '%b'
zstyle ':zim:git-info:commit' format '%c'
zstyle ':zim:git-info:dirty' format '${ERR_COLOR}'
zstyle ':zim:git-info:diverged' format '${ERR_COLOR}'
zstyle ':zim:git-info:behind' format '%F{11}'
zstyle ':zim:git-info:ahead' format '${OFF_COLOR}'
zstyle ':zim:git-info:keys' format \
'prompt' '%b%c' \
'color' '$(coalesce "%D" "%V" "%B" "%A" "${ON_COLOR}")'
PROMPT="$(prompt_minimal_user)$(prompt_minimal_jobs)\$(prompt_minimal_vimode)$(prompt_minimal_status)%f "
RPROMPT='$(prompt_minimal_path)$(prompt_minimal_git)'
} }
prompt_minimal_setup "$@" prompt_minimal_setup "$@"