zsh: display some info about vcs in current directory

This commit is contained in:
Thomas Preisner 2022-09-18 18:01:32 +02:00
parent cfa621b864
commit 13f5ecd846
3 changed files with 91 additions and 6 deletions

View file

@ -18,6 +18,9 @@ setopt transient_rprompt # only show the rprompt on the current prompt
# ===== set the prompt
zshrc_prompt_precmd() {
# call vcs_info before every prompt
vcs_info
# regex to remove all zerospace elements. Used to calculate the width of
# the top prompt.
local zero='%([BSUBfksu]|([FB]|){*})'
@ -52,6 +55,14 @@ zshrc_prompt_precmd() {
# username, green for normal users, red for root
local user="%(!.%F{red}.%F{green})%n%f"
# information about unstaged/staged changes in VCS in current directory
local vcs="${vcs_info_msg_0_}"
local vcs_width=${#${(S%%%)vcs//$~zero/}}
if [[ $vcs_width -ne 0 ]]; then
# add brackets if vcs is non-empty
vcs="${bracket_open}${vcs}${bracket_close}"
fi
# hostname, underlined if running on a remote system through SSH
local host="%F{green}%m%f"
if [[ -n $SSH_CONNECTION ]]; then
@ -95,6 +106,7 @@ zshrc_prompt_precmd() {
# combine them to create the prompt
local top_left=""
local top_right="${bracket_open}${time}${bracket_close}"
local top_middle="${vcs}"
local bottom_left="${bracket_open}${user}%F{green}@%f${host}${bracket_close}${exitcode}${separator}${symbol}"
local bottom_right=
@ -109,15 +121,16 @@ zshrc_prompt_precmd() {
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_middle=${#${(S%%%)top_middle//$~zero/}}
local width_top_right=${#${(S%%%)top_right//$~zero/}}
# calculate the maximum width of ${top_left}:
# (-4 for brackets, -1 as spacing between top_left and top_right)
# (-4 for brackets and as spacing between top_left, top_middle and top_right)
local top_left_width_max=$((
COLUMNS - $width_top_prefix
- $width_top_left - 4
- 1
- $width_top_right
- $width_top_left - 2
- $width_top_middle
- $width_top_right - 2
- $width_top_suffix
))
@ -129,12 +142,14 @@ zshrc_prompt_precmd() {
local width=$((
COLUMNS - width_top_prefix
- width_top_left
- width_top_middle
- width_top_right
- width_top_suffix
))
local top_separator="%F{cyan}${(pl:${width}::$dash:)}%f"
local top_left_separator="%F{cyan}${(pl:$((width / 2))::$dash:)}%f"
local top_right_separator="%F{cyan}${(pl:$((width / 2 + width % 2))::$dash:)}%f"
PROMPT="${top_prefix}${top_left}${top_separator}${top_right}${top_suffix}
PROMPT="${top_prefix}${top_left}${top_left_separator}${top_middle}${top_right_separator}${top_right}${top_suffix}
${bottom_prefix}${bottom_left} "
RPROMPT="${bottom_right}${bottom_suffix}"