1
0
dotfiles/.config/zsh/lib/git.zsh

55 lines
1.2 KiB
Bash
Raw Normal View History

2021-03-21 18:25:10 +03:00
#!/bin/zsh
## fancy and manageable PS1 for git
typeset -gA ZSHU_PS
ZSHU_PS[git]=0
2021-03-21 22:32:39 +03:00
__z_git_avail() { (( $+commands[git] )) ; }
2021-03-21 18:25:10 +03:00
2021-03-21 22:32:39 +03:00
__z_git() { GIT_OPTIONAL_LOCKS=0 command git "$@"; }
2021-03-21 18:25:10 +03:00
2021-03-21 22:32:39 +03:00
__z_git_is_repo() { __z_git rev-parse --git-dir &>/dev/null ; }
2021-03-21 18:25:10 +03:00
2021-03-21 22:32:39 +03:00
z-git-test() {
2021-03-21 18:25:10 +03:00
[ "${ZSHU_PS[git]}" = '1' ] || return 1
2021-03-21 22:32:39 +03:00
__z_git_avail || return 2
2021-03-21 18:25:10 +03:00
2021-03-21 22:32:39 +03:00
__z_git_is_repo || return 3
2021-03-21 18:25:10 +03:00
return 0
}
2021-03-21 22:32:39 +03:00
__z_git_pwd() {
z-git-test || return
2021-03-21 18:25:10 +03:00
local p=${(%):-%~}
[[ "$p" =~ '/.+' ]] || return
local s pfx last
2021-03-21 22:32:39 +03:00
s=$(__z_git rev-parse --show-prefix)
2021-03-21 18:25:10 +03:00
s="${s%%/}"
if [ -n "$s" ] ; then
p=${p%%/$s}
last="${s:t}"
pfx="${s%${last}}"
pfx="${pfx%/}"
pfx="/${pfx}${pfx:+/}"
else
last="/"
fi
ZSHU_PS[pwd]="%F{magenta}$p%F{cyan}${pfx}%B${last}%f%b"
}
2021-03-21 22:32:39 +03:00
z-git-enable() { ZSHU_PS[git]=1 ; }
z-git-disable() { ZSHU_PS[git]=0 ; }
2021-03-21 18:25:10 +03:00
2021-03-21 22:32:39 +03:00
z-git-status() {
__z_git_avail
2021-03-21 18:25:10 +03:00
echo "Git binary: "${(%):-%(?..NOT )}"found in PATH"
[ "${ZSHU_PS[git]}" = 1 ]
echo "Git prompt: "${(%):-%(?.enabled.disabled)}
2021-03-21 22:32:39 +03:00
__z_git_is_repo
2021-03-21 18:25:10 +03:00
echo "Git repo: "${(%):-%(?..NOT )}"present"
}
2021-03-21 22:32:39 +03:00
ZSHU[pwd_hook]="${ZSHU[pwd_hook]}${ZSHU[pwd_hook]:+ }__z_git_pwd"