dotfiles/homedir/.config/zsh.d/modules/prompt
fbt c78cb78ff0 init
Signed-off-by: fbt <fbt@fleshless.org>
2017-05-03 16:10:13 +03:00

129 lines
2.2 KiB
Bash

# Prompt
# vim: ft=zsh
autoload -U colors && colors
nullexec() { "$@" &>/dev/null; }
precmd.title() {
printf '%b' "\e]2;$1\a"
}
precmd.svn() {
if [[ -f .novcsprompt ]]; then
return 0
fi
if nullexec svn info; then
while IFS= read -r line; do
case $line in
(M*|A*|D*) svn_unstaged=1;;
(\!*) svn_missing=1;;
(\?*) svn_untracked=1;;
esac
done < <( svn st )
(( svn_unstaged )) && {
svn_st+='c'
svn_st_col='red'
}
(( svn_missing )) && {
svn_st+='m'
svn_st_col='red'
}
(( svn_untracked )) && {
svn_st+='f'
svn_st_col='red'
}
svn_st_col=${svn_st_col:-"green"}
svn_st=${svn_st:-"ok"}
printf '%s' "(%F{cyan}svn%f:%F{$svn_st_col}$svn_st%f) "
fi
}
precmd.git() {
declare git_unstaged git_untracked git_status_short git_status_colour git_prompt_msg
if [[ -f .novcsprompt ]]; then
return 0
fi
if git rev-parse --git-dir &>/dev/null; then
git_current_branch=$(git rev-parse --abbrev-ref HEAD)
while IFS= read -r line; do
case $line in
' M'*|A*|D*) git_unstaged=1;;
\?\?*) git_untracked=1;;
esac
done < <( git status --short )
(( git_unstaged )) && {
git_status_short+='c'
git_status_colour='red'
}
(( git_untracked )) && {
git_status_short+='f'
git_status_colour='red'
}
git_status_short=${git_status_short:-"ok"}
git_status_colour=${git_status_colour:-"green"}
git_prompt_msg="(%F{cyan}$git_current_branch%f[%F{$git_status_colour}${git_status_short}%f]) "
printf '%s' $git_prompt_msg
fi
}
rprompt() {
{ precmd.svn; precmd.git } > "$XDG_RUNTIME_DIR/zsh_rprompt_$$.tmp"
kill -s USR1 $$
}
TRAPUSR1() {
prompt_async_data=$(<"$XDG_RUNTIME_DIR/zsh_rprompt_$$.tmp")
draw_prompt
zle && zle reset-prompt
}
draw_prompt() {
prompt_custom=(
"%F{$cmd_colour}$cmd_msg%f"
"%F{$user_colour}$USER%f@$HOST"
"${PWD//$HOME/~}"
"$prompt_async_data%F{$user_colour}>%f"
)
PROMPT=" $prompt_custom "
}
precmd() {
declare last_exit_code=$?
declare -g cmd_colour cmd_msg
if (( last_exit_code )); then
cmd_colour='red'
cmd_msg='!'
else
cmd_colour='green'
cmd_msg='.'
fi
if (( UID )); then
user_colour='green'
else
user_colour='red'
fi
precmd.title "$USER@$HOST ${PWD//$HOME/~}"
draw_prompt
rprompt &!
}