zsh: prompt: complete internal redesign + improvements

The prompt now no longer requires "setopt promptsubst" and even has
a bit more functionality:
    - show exitcode if not equal zero
    - underlined hostname if remote-connected via ssh
    - extra prompt symbol for root and different color username
    - improved battery status (color + charge detection)

Furthermore time and username@hostname have been swapped.
This commit is contained in:
Thomas Preisner 2017-07-10 02:40:38 +02:00
parent cb93e83215
commit 2b7107a84a
2 changed files with 116 additions and 59 deletions

View file

@ -1,70 +1,131 @@
function precmd { # ===== setopts and misc
# only show RPROMPT on the current prompt
setopt transient_rprompt # only show the rprompt on the current prompt
local TERMWIDTH # set RPROMPT_INDENT to zero in order to remove the useless space on the bottom
(( TERMWIDTH = ${COLUMNS} - 1 )) # right of the prompt.
ZLE_RPROMPT_INDENT=0
### # ===== set the prompt
# Truncate the path if it's too long. zshrc_prompt_precmd() {
PR_FILLBAR="" # regex to remove all zerospace elements. Used to calculate the width of
PR_PWDLEN="" # the top prompt.
local zero='%([BSUBfksu]|([FB]|){*})'
local promptsize=${#${(%):---(%n@%m)---()--}} # linedrawing characters
local pwdsize=${#${(%):-%~}} local top_left_corner="╭"
local top_right_corner="╮"
local bottom_left_corner="╰"
local bottom_right_corner="╯"
local dash="─"
if [[ "$promptsize + $pwdsize" -gt $TERMWIDTH ]]; then # check $TERM and use non unicode symbols if necessary
((PR_PWDLEN=$TERMWIDTH - $promptsize)) if [[ $TERM == "linux" ]]; then
top_left_corner="+"
top_right_corner="+"
bottom_left_corner="+"
bottom_right_corner="+"
dash="-"
fi
# commonly used design elements
local bracket_open="%F{blue}${dash}(%f"
local bracket_close="%F{blue}%)${dash}%f"
local separator="%F{cyan}${dash}%f"
# current directory, truncated if necessary (WIDTH is replaced later)
local directory="%F{magenta}%WIDTH<..<%~%<<%f"
# current time (HH:MM)
local time="%F{yellow}%D{%H:%M}%f"
# username, green for normal users, red for root
local user="%(!.%F{red}.%F{green})%n%f"
# hostname, underlined if running on a remote system through SSH
local host="%F{green}%m%f"
if [[ -n $SSH_CONNECTION ]]; then
host="%U${host}%u"
fi
# exitcode in parentheses if not zero
local exitcode="%(?..[%F{red}%?%f])"
# prompt symbol, $ in cyan for normal users, # in red for root
local symbol="%(!.%F{red}#%f.%F{cyan}>%f)"
# battery percentage, if acpi is available
local battery_pct=
if type acpi &> /dev/null ; then
battery_pct="$(acpi -b | cut -d, -f2 | sed -e 's/[^0-9]//g')"
fi
# colorize battery percentage or display status
if [[ $(acpi -a | cut -d':' -f2) == " on-line" ]]; then
battery_pct="charging"
elif [[ $battery_pct -le 20 ]]; then
battery_pct="%F{red}${battery_pct}%%%f"
elif [[ $battery_pct -le 65 ]]; then
battery_pct="%F{yellow}${battery_pct}%%%f"
else else
PR_FILLBAR="\${(l.(($TERMWIDTH - ($promptsize + $pwdsize)))..${PR_HBAR}.)}" battery_pct="%F{green}${battery_pct}%%%f"
fi fi
}
function show_battery { # prefix characters in first and second line
if [[ $HAS_PMSET -eq 1 ]]; then local top_prefix="%F{cyan}${top_left_corner}%f"
~/.zsh/scripts/battery_osx.sh local bottom_prefix="%F{cyan}${bottom_left_corner}%f"
fi
}
function setprompt { # suffix characters in first and second line
PR_HBAR="─" local top_suffix="%F{cyan}${top_right_corner}%f"
local bottom_suffix="%F{cyan}${bottom_right_corner}%f"
if [[ "$TERM" != "xterm" ]]; then # combine them to create the prompt
PR_ULCORNER="╭" local top_left=""
PR_LLCORNER="╰" local top_right="${bracket_open}${time}${bracket_close}"
PR_LRCORNER="╯" local bottom_left="${bracket_open}${user}%F{green}@%f${host}${bracket_close}${exitcode}${separator}${symbol}"
PR_URCORNER="╮" local bottom_right=
# if $battery_pct is empty, just add a vertical line
if [[ -n $battery_pct ]]; then
bottom_right="${separator}${bracket_open}${battery_pct}${bracket_close}"
else else
PR_ULCORNER="+" bottom_right="%F{blue}${dash}%f"
PR_LLCORNER="+"
PR_LRCORNER="+"
PR_URCORNER="+"
fi fi
PR_RESET="%{$terminfo[sgr0]%}" # calculate width of prompt components
local width_top_prefix=${#${(S%%%)top_prefix//$~zero/}}
local width_top_suffix=${#${(S%%%)top_suffix//$~zero/}}
local width_top_left=${#${(S%%%)top_left//$~zero/}}
local width_top_right=${#${(S%%%)top_right//$~zero/}}
### # calculate the maximum width of ${top_left}:
# Finally, the prompt. # (-4 for brackets, -1 as spacing between top_left and top_right)
local top_left_width_max=$((
COLUMNS - $width_top_prefix
- $width_top_left - 4
- 1
- $width_top_right
- $width_top_suffix
))
PROMPT='%F{cyan}$PR_ULCORNER%F{blue}$PR_HBAR(\ # truncated directory if necessary
%F{magenta}%$PR_PWDLEN<...<%~%<<\ top_left="${bracket_open}${directory/WIDTH/${top_left_width_max}}${bracket_close}${top_left}"
%F{blue})$PR_HBAR%F{cyan}$PR_HBAR${(e)PR_FILLBAR}%F{blue}$PR_HBAR(\ width_top_left=${#${(S%%)top_left//$~zero/}}
%F{green}%n@%m%F{blue}\
%F{blue})$PR_HBAR%F{cyan}$PR_URCORNER\
%F{cyan}$PR_LLCORNER%F{blue}$PR_HBAR(\ # calculate the width of the top prompt to fill the middle with separators
%F{yellow}%D{%H:%M}\ local width=$((
%F{blue})$PR_HBAR%F{cyan}$PR_HBAR\ COLUMNS - width_top_prefix
%f ' - width_top_left
- width_top_right
- width_top_suffix
))
local top_separator="%F{cyan}${(pl:${width}::$dash:)}%f"
RPROMPT='%F{cyan}$PR_HBAR%F{blue}$PR_HBAR(\ PROMPT="${top_prefix}${top_left}${top_separator}${top_right}${top_suffix}
%F{yellow}$(show_battery)\ ${bottom_prefix}${bottom_left} "
%F{blue})$PR_HBAR%F{cyan}$PR_LRCORNER\
%f'
PS2='%F{cyan}$PR_HBAR%F{blue}$PR_HBAR(\ RPROMPT="${bottom_right}${bottom_suffix}"
%F{green}%_
%F{blue})$PR_HBAR%F{cyan}$PR_HBAR\
%f '
} }
setprompt precmd_functions+=(zshrc_prompt_precmd)

View file

@ -34,9 +34,5 @@ unsetopt menu_complete # do not autoselect the first completion entry
# setopt correct # spelling correction for commands # setopt correct # spelling correction for commands
# setopt correctall # spelling correction for arguments # setopt correctall # spelling correction for arguments
# ===== Prompt
setopt prompt_subst # Enable parameter expansion, command substitution, and arithmetic expansion in the prompt
setopt transient_rprompt # only show the rprompt on the current prompt
# ===== Scripts and Functions # ===== Scripts and Functions
setopt multios # perform implicit tees or cats when multiple redirections are attempted setopt multios # perform implicit tees or cats when multiple redirections are attempted