dotfiles/.config/zsh/prompt.zsh

171 lines
5.0 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

autoload -Uz vcs_info
pwd="%B%F{blue}%3~%f%b"
# Show active python virtualenv in prompt
precmd_pyenv_info() {
# use nerd fonts for icons
_pyenv_icon=""
# Only update if we've changed directory, since pyenv can be slow
if [[ "$PWD" == "$_pyenv_old_pwd" ]]; then
return
else
_pyenv_old_pwd="$PWD"
fi
_pyenv_version=$(pyenv version-name 2>/dev/null)
if [[ $? -eq 0 ]] && [[ ${_pyenv_version} != "system" ]] && [[ ${_pyenv_version} != "personal" ]]; then
pyenv_info_msg_0_="%F{yellow} ${_pyenv_icon} ${_pyenv_version}%f"
elif [[ ${_pyenv_version} == "system" ]]; then
pyenv_info_msg_0_="%F{red} ${_pyenv_icon} ${_pyenv_version}%f"
else
pyenv_info_msg_0_=""
fi
}
precmd_kernel_info() {
# check to see if a kernel update has been installed
# since we need to reboot to use it
# if message has been set, don't check again, since a reboot
# is required to clear the message
if [[ -n ${kernel_info_msg_0_} ]]; then
return
fi
if [[ ${OS_ID} == "arch" ]] || [[ ${OS_ID_LIKE} == "arch" ]]; then
# Arch Linux removes the old kernel when updating, so we can
# just check to see if /usr/lib/modules/$(uname -r)/vmlinuz exists
if ! [[ -f /usr/lib/modules/$(uname -r)/vmlinuz ]]; then
# kernel update available
kernel_info_msg_0_="%F{red} reboot required %f"
fi
fi
}
precmd_vcs_info() {
vcs_info
}
# Reset precmd_functions, then add our functions. This way, regardless of
# whether this file is sourced multiple times, we don't end up with duplicate
# entries which will slow down the prompt.
precmd_functions=()
if [[ ${TERM} == "alacritty" ]]; then
precmd_functions+=(precmd_vcs_info precmd_pyenv_info _reset_window_name)
setopt prompt_subst
elif ! [[ ${TERM} == "dumb" ]]; then
precmd_functions+=(precmd_vcs_info precmd_pyenv_info)
setopt prompt_subst
fi
eval $(awk '{print "OS_" $0}' /etc/os-release)
eval $(awk '{print "MACHINE_" $0}' /etc/machine-info)
eval $(awk '{print "PERSONAL_" $0}' ${HOME}/.personal-info 2>/dev/null)
if [[ -z ${MACHINE_CHASSIS} ]]; then MACHINE_CHASSIS=$(hostnamectl chassis 2>/dev/null); fi
if [[ -z ${MACHINE_CHASSIS} ]]; then MACHINE_CHASSIS="desktop"; fi
if [[ ${MACHINE_CHASSIS} != "container" ]] && [[ ${OS_ID} == "arch" ]]; then
# Only check if a reboot is necessary if we're not in a container
precmd_functions+=(precmd_kernel_info)
fi
hostname=$PERSONAL_HOSTNAME
if [[ -z ${hostname} ]]; then # No personal hostname set
if [[ -n ${MACHINE_PRETTY_HOSTNAME} ]]; then
# Use MACHINE_PRETTY_HOSTNAME if set
hostname=${MACHINE_PRETTY_HOSTNAME}
elif [[ -f /etc/hostname ]]; then
# Fall back to system hostname
hostname=$(</etc/hostname)
else
# Ask the kernel for the hostname
hostname=$(uname -n)
fi
fi
show_os_icon=1
# use nerd fonts for icons
case $OS_ID in
"arch")
os_icon=""
show_os_icon=0 # Arch is my default, no need to show icon
;;
"debian")
os_icon=""
;;
"ubuntu")
os_icon=""
;;
*)
os_icon="?"
show_os_icon=0
;;
esac
prompt_char="%(?..%F{red}%f)"
host="%F{cyan}[ %F{green}${hostname}%(!.%F{red} as root.) %F{cyan}]%f"
localhost="%F{cyan}[ %F{magenta}%B${hostname}%(!.%F{red} as root.)%b %F{cyan}]%f"
serial_host="%F{cyan}[ %F{magenta}${hostname}%(!.%F{red} as root.) %F{cyan}] %F{red}%y%f"
container_host="%F{cyan}[ %F{blue}%B${hostname}%(!.%F{red} as root.)%b %F{cyan}]%f"
returncode="%(?..%F{red}  %?%f)"
prompt_line1='$kernel_info_msg_0_'
prompt_line3=" $prompt_char"
# Create conditional prompt line 2
if [[ ${TTY} =~ "tty" ]]; then
prompt_line2='${serial_host}$pyenv_info_msg_0_ ${pwd}'
history_line="${serial_host}"
elif [[ ${MACHINE_CHASSIS} == "container" ]] || [[ ${MACHINE_CHASSIS} == "vm" ]]; then
prompt_line2='${container_host}$pyenv_info_msg_0_ ${pwd}'
history_line="${container_host}"
elif [[ -v SSH_CLIENT ]]; then
prompt_line2='${host}$pyenv_info_msg_0_ ${pwd}'
history_line="${host}"
else
prompt_line2='${localhost}$pyenv_info_msg_0_ ${pwd}'
history_line="${localhost}"
fi
# Inject icon if available, with ANSI color code stored in $OS_ANSI_COLOR
if [[ ${show_os_icon} -eq 1 ]]; then
prompt_line2=$'%{\x1b[${OS_ANSI_COLOR}m${os_icon}\x1b[0m%}'" ${prompt_line2}"
fi
set-prompt() {
if [[ ${TERM} == "dumb" ]]; then
# Dumb terminal needs a dumb prompt, otherwise things like
# emacs TRAMP break
PROMPT='[%~] $ '
PROMPT2='> '
else
PROMPT="${prompt_line1}
${prompt_line2}
${prompt_line3} "
PROMPT2=" %F{yellow}%_ %f "
fi
}
set-prompt
rewrite-prompt-and-accept() {
if [[ ${TERM} == "dumb" ]]; then
return
fi
PROMPT='${history_line} %F{yellow}%T%f ${pwd}$pyenv_info_msg_0_ %F{green}${prompt_char}%f '
# Clear any suggestions; we've committed to this command
zle autosuggest-clear
zle reset-prompt
set-prompt
zle accept-line
}
zle -N rewrite-prompt-and-accept
bindkey '^M' rewrite-prompt-and-accept
RPROMPT='$vcs_info_msg_0_$returncode'
zstyle ':vcs_info:git:*' formats '%c %F{magenta} 󰊢 %B%r%%b %F{cyan} %b%f'
zstyle ':vcs_info:git:*' actionformats '%c%F{magenta} 󰊢 %B%r%%b %F{cyan} %b %F{red} %a%f'
zstyle ':vcs_info:git:*' stagedstr "%F{green}%f"
zstyle ':vcs_info:git:*' check-for-staged-changes true
zstyle ':vcs_info:*' enable git