dotfiles/.config/zsh/05-prompt-pre.zsh

179 lines
5.5 KiB
Bash
Raw Permalink 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.

#
# Initial setup for prompt generation.
#
# Defines the arrays for prompt elements ($prompt_list), rprompt elements ($rprompt_list), and history prompt elements ($history_prompt, the prompt is rewritten using this when a command is submitted).
#
# Prompt elements are strings. Some special prefixes are understood:
# - Strings prefixed with 'func:' will run the function given, using the contents of ${prompt_element_contents} as the element. Argument splitting is performed, but variable interpolation is not (you should be able to do that yourself).
# - Strings prefixed with 'var:' will expand the given variable specifier when the prompt is generated, within curly braces. This allows specifying that the given variable be evaluated at generation time.
# - Strings prefixed with 'literal:' will use the remaining string literally. This allows a static component which starts with "func:" or "var:" if you want to for whatever reason.
#
# We set the prompt_subst option, so variables can be included in prompt elements (e.g. in single quotes), and changes to them will be reflected the next time the prompt renders.
#
# We also set precmd_functions to an empty array in this file, so it should be extended.
declare -a prompt_list
declare -a rprompt_list
declare -a history_prompt
declare -A prompt_elements
setopt prompt_subst
precmd_functions=()
prompt_elements[privilege]='%B%0(#.%F{red} as root.%(!.%F{red} with privileges.))%b'
prompt-element-host() {
# Adds host info to the prompt.
if [[ ${SYSTEM_INFO[Chassis]} == "container" ]] || [[ ${SYSTEM_INFO[Chassis]} == "vm" ]]; then
prompt_element_contents='%F{blue}%B${SYSTEM_INFO[PrettyHostname]:-${SYSTEM_INFO[Hostname]}}%b%f'
elif [[ -v SSH_CLIENT ]]; then
prompt_element_contents='%F{blue}%BRemote Console%b%f @ %F{magenta}%B${SYSTEM_INFO[PrettyHostname]:-${SYSTEM_INFO[Hostname]}}%b%f'
elif [[ -o login ]]; then
prompt_element_contents='%F{green}%BLogin Console%b%f @ %F{magenta}%B${SYSTEM_INFO[PrettyHostname]:-${SYSTEM_INFO[Hostname]}}%b%f'
else
prompt_element_contents='%F{magenta}%B${SYSTEM_INFO[PrettyHostname]:-${SYSTEM_INFO[Hostname]}}%f%b'
fi
}
prompt_elements[host]="func:prompt-element-host"
prompt-element-console() {
if [[ ${TTY} =~ "tty" ]]; then
prompt_element_contents=" %F{red}%y%f"
else
prompt_element_contents=""
fi
}
prompt_elements[console]="func:prompt-element-console"
prompt-element-deployment() {
if [[ ${SYSTEM_INFO[Deployment]+yes} == "yes" ]]; then
case ${(L)SYSTEM_INFO[Deployment]} in
*prod|production)
prompt_element_contents='%F{red}%BPROD%b%f '
;;
*devl|dev|devel|development)
prompt_element_contents='%F{green}%BDEV%b%f '
;;
*pprd|preprod|preproduction)
prompt_element_contents='%F{cyan}%BPRE-PROD%b%f '
;;
*)
prompt_element_contents='%F{blue}%B${(U)SYSTEM_INFO[Deployment]}%b%f '
;;
esac
fi
}
prompt_elements[deployment]="func:prompt-element-deployment"
prompt_elements[retcode]="%(?..%F{red}  %?%f)"
prompt_elements[prompt]="%(?..%F{red}%f) "
prompt_elements[pwd]="%B%F{blue}%3~%f%b"
prompt_elements[pyenv]='$pyenv_info_msg_0_'
prompt_elements[vcs]='$vcs_info_msg_0_'
prompt_elements[clock]='%F{yellow}%T%f'
prompt-element-groups() {
# Produces newlines at start and end.
local ambient_groups defined_groups ambient_map defined_map
declare -A ambient_map
declare -A defined_map
ambient_groups=($(id -Gn))
defined_groups=($(id -Gn ${USER}))
prompt_element_contents=$'\n'
# yay 3 loops :/
for group in "${ambient_groups[@]}"; do
ambient_map[$group]=1
done
for group in "${defined_groups[@]}"; do
defined_map[$group]=1
if [ ${ambient_map[$group]:-0} -eq 0 ]; then
prompt_element_contents+=" %F{red}$group%f"$'\n'
fi
done
for group in "${ambient_groups[@]}"; do
if [ ${defined_map[${group}]:-0} -eq 0 ]; then
prompt_element_contents+=" %F{green}$group%f"$'\n'
fi
done
}
prompt_elements[groups]='func:prompt-element-groups'
_build-prompt() {
PROMPT=""
for el in "${prompt_list[@]}"; do
case $el in
func:*)
# Clear the variable
prompt_element_contents=''
# Run the function
${=el/func:/}
# Add the variable to the prompt
PROMPT+="${prompt_element_contents}"
;;
var:*)
# eval the variable.
eval "PROMPT+=\"\${${el/var:/}}\""
;;
literal:*)
# strip the leading 'literal:' and add the string to the prompt
PROMPT+="${el/literal:/}"
;;
*)
# not a known prefix, so just add it as a string
PROMPT+="${el}"
;;
esac
done
RPROMPT=""
for el in "${rprompt_list[@]}"; do
case $el in
func:*)
# Clear the variable
prompt_element_contents=''
# Run the function
${=el/func:/}
# Add the variable to the prompt
RPROMPT+="${prompt_element_contents}"
;;
var:*)
# eval the variable.
eval "RPROMPT+=\"\${${el/var:/}}\""
;;
literal:*)
# strip the leading 'literal:' and add the string to the prompt
RPROMPT+="${el/literal:/}"
;;
*)
# not a known prefix, so just add it as a string
RPROMPT+="${el}"
;;
esac
done
HISTORY_UNPROMPT=""
for el in "${history_prompt[@]}"; do
case $el in
func:*)
# Clear the variable
prompt_element_contents=''
# Run the function
${=el/func:/}
# Add the variable to the prompt
HISTORY_UNPROMPT+="${prompt_element_contents}"
;;
var:*)
# eval the variable.
eval "HISTORY_UNPROMPT+=\"\${${el/var:/}}\""
;;
literal:*)
# strip the leading 'literal:' and add the string to the prompt
HISTORY_UNPROMPT+="${el/literal:/}"
;;
*)
# not a known prefix, so just add it as a string
HISTORY_UNPROMPT+="${el}"
;;
esac
done
}