dotfiles/zsh/func/prompt_loquacious_setup

152 lines
3.1 KiB
Bash

#!/usr/bin/zsh
# vim:sw=4:sts=4:
#
# A wordy prompt theme.
#
# Eryn Wells <eryn@erynwells.me>
function prompt_loquacious_help
{
cat <<EOF
This prompt takes up two lines. At its most verbose, it looks like this:
<user> at <host> in <cwd> on <repo>(<branch>)
%
By default, <host> is only shown when the SSH_CONNECTION variable is set; in
other words, when this shell session was opened via an SSH connection. The
prompt will also show the current repository and branch using vcs_info if the
current directory is in a repo.
EOF
}
function prompt_loquacious_setup
{
typeset -ga precmd_functions
typeset -ga preexec_functions
autoload -U prompt_colorize
autoload -Uz vcs_info
autoload -U add-zsh-hook
add-zsh-hook precmd set_xterm_title
add-zsh-hook precmd print_newline
add-zsh-hook precmd set_prompt_info
add-zsh-hook precmd set_zle_mode_info
add-zsh-hook preexec print_newline
prompt_opts=(cr subst percent)
# Set up vcs_info
zstyle ':vcs_info:git:loquacious:*' formats \
'%F{cyan}%r%f(%F{blue}%b%f)'
PS1='${PS1_NAME}${PS1_HOST}${PS1_CWD}${PS1_REPO}${PS1_ZLE_MODE}
$PS1_LINE'
zle -N zle-keymap-select on_keymap_select
}
function prompt_loquacious_preview
{
# TODO: Implement prompt preview.
}
#
# HELPER FUNCTIONS
#
# First prompt flag. See precmd_newline.
is_first_prompt=1
function print_newline
{
# Don't print newlines the first time the prompt is displayed.
if [[ -n $is_first_prompt ]]; then
unset is_first_prompt
[[ -z $SSH_CONNECTION ]] && return
fi
echo
}
function set_prompt_info
{
PS1_HISTORY="`prompt_colorize -b -f 'green' '%h'`"
PS1_NAME="`prompt_colorize -f 'magenta' '%n'` "
PS1_CWD="in `prompt_colorize -f 'green' '%~'` "
if [[ -n "$SSH_CONNECTION" ]]; then
PS1_HOST="at `prompt_colorize -f 'red' '%m'` "
else
PS1_HOST=''
fi
# Get git repo information, if it exists. See setup function for
# configuration details.
vcs_info loquacious
if [[ -n "$vcs_info_msg_0_" ]]; then
PS1_REPO="on $vcs_info_msg_0_ "
else
PS1_REPO=""
fi
# Show background jobs, if any.
if [[ `jobs | wc -l` -ge 1 ]]; then
PS1_JOBS='%j'
fi
# Show background job count if any exist.
RPS1="%(1j.[`prompt_colorize -f 'magenta' '%j'`].)"
PS1_LINE='%# '
}
function set_xterm_title
{
local title=''
if [[ -n "$TMUX" ]]; then
title+="`tmux display-message -p '#S'`: "
fi
# Set xterm and screen titles
if [[ -n "$DISPLAY" || -n "$TERM_PROGRAM" ]]; then
title+="%n@%m"
fi
print -Pn "\e]2;${title}\a"
}
function set_zle_mode_info
{
if [[ -z "$1" || "$1" == 'viins' || "$1" == 'main' ]]; then
PS1_ZLE_MODE=`prompt_colorize -f 'black' '<INS>'`
elif [[ "$1" == 'vicmd' ]]; then
PS1_ZLE_MODE=`prompt_colorize -f 'black' '<CMD>'`
else
PS1_ZLE_MODE=''
fi
}
function on_keymap_select
{
# Regenerate the prompt with the new mode
set_zle_mode_info $KEYMAP
set_prompt_info
# Redraw the prompt
zle reset-prompt
}
# Finally, run setup to get everything going
prompt_loquacious_setup "$@"