Skip to content

Instantly share code, notes, and snippets.

@andrewnc
Created November 14, 2024 17:56
Show Gist options
  • Save andrewnc/71c95336659dc165c027f6a103912953 to your computer and use it in GitHub Desktop.
Save andrewnc/71c95336659dc165c027f6a103912953 to your computer and use it in GitHub Desktop.
function theme_precmd {
local TERMWIDTH=$(( COLUMNS - ${ZLE_RPROMPT_INDENT:-1} ))
PR_FILLBAR=""
PR_PWDLEN=""
local promptsize=${#${(%):---(%n@%m:%l)---()--}}
local rubypromptsize=${#${(%)$(ruby_prompt_info)}}
local pwdsize=${#${(%):-%~}}
local venvpromptsize=$((${#$(virtualenv_prompt_info)}))
# Truncate the path if it's too long.
if (( promptsize + rubypromptsize + pwdsize + venvpromptsize > TERMWIDTH )); then
(( PR_PWDLEN = TERMWIDTH - promptsize ))
elif [[ "${langinfo[CODESET]}" = UTF-8 ]]; then
PR_FILLBAR="\${(l:$(( TERMWIDTH - (promptsize + rubypromptsize + pwdsize + venvpromptsize ) ))::${PR_HBAR}:)}"
else
PR_FILLBAR="${PR_SHIFT_IN}\${(l:$(( TERMWIDTH - (promptsize + rubypromptsize + pwdsize + venvpromptsize ) ))::${altchar[q]:--}:)}${PR_SHIFT_OUT}"
fi
}
function theme_preexec {
setopt local_options extended_glob
if [[ "$TERM" = "screen" ]]; then
local CMD=${1[(wr)^(*=*|sudo|-*)]}
echo -n "\ek$CMD\e\\"
fi
}
autoload -U add-zsh-hook
add-zsh-hook precmd theme_precmd
add-zsh-hook preexec theme_preexec
# Set the prompt
setopt prompt_subst
# Nord color palette with direct ANSI escape sequences
# Polar Night
PR_NORD0=$'%{\e[38;2;46;52;64m%}' # #2E3440
PR_NORD1=$'%{\e[38;2;59;66;82m%}' # #3B4252
PR_NORD2=$'%{\e[38;2;67;76;94m%}' # #434C5E
PR_NORD3=$'%{\e[38;2;76;86;106m%}' # #4C566A
# Snow Storm
PR_NORD4=$'%{\e[38;2;216;222;233m%}' # #D8DEE9
PR_NORD5=$'%{\e[38;2;229;233;240m%}' # #E5E9F0
PR_NORD6=$'%{\e[38;2;236;239;244m%}' # #ECEFF4
# Frost
PR_NORD7=$'%{\e[38;2;143;188;187m%}' # #8FBCBB
PR_NORD8=$'%{\e[38;2;136;192;208m%}' # #88C0D0
PR_NORD9=$'%{\e[38;2;129;161;193m%}' # #81A1C1
PR_NORD10=$'%{\e[38;2;94;129;172m%}' # #5E81AC
# Aurora
PR_NORD11=$'%{\e[38;2;191;97;106m%}' # #BF616A
PR_NORD12=$'%{\e[38;2;208;135;112m%}' # #D08770
PR_NORD13=$'%{\e[38;2;235;203;139m%}' # #EBCB8B
PR_NORD14=$'%{\e[38;2;163;190;140m%}' # #A3BE8C
PR_NORD15=$'%{\e[38;2;180;142;173m%}' # #B48EAD
PR_NO_COLOUR=$'%{\e[0m%}'
# Modify Git prompt with Nord colors
ZSH_THEME_GIT_PROMPT_PREFIX=" on ${PR_NORD8}"
ZSH_THEME_GIT_PROMPT_SUFFIX="${PR_NO_COLOUR}"
ZSH_THEME_GIT_PROMPT_DIRTY=""
ZSH_THEME_GIT_PROMPT_CLEAN=""
ZSH_THEME_GIT_PROMPT_ADDED="${PR_NORD14} %{%G✚%}"
ZSH_THEME_GIT_PROMPT_MODIFIED="${PR_NORD9} %{%G✹%}"
ZSH_THEME_GIT_PROMPT_DELETED="${PR_NORD11} %{%G✖%}"
ZSH_THEME_GIT_PROMPT_RENAMED="${PR_NORD15} %{%G➜%}"
ZSH_THEME_GIT_PROMPT_UNMERGED="${PR_NORD13} %{%G═%}"
ZSH_THEME_GIT_PROMPT_UNTRACKED="${PR_NORD8} %{%G✭%}"
# Use extended characters to look nicer if supported.
if [[ "${langinfo[CODESET]}" = UTF-8 ]]; then
PR_SET_CHARSET=""
PR_HBAR="─"
PR_ULCORNER="┌"
PR_LLCORNER="└"
PR_LRCORNER="┘"
PR_URCORNER="┐"
else
typeset -g -A altchar
set -A altchar ${(s..)terminfo[acsc]}
PR_SET_CHARSET="%{$terminfo[enacs]%}"
PR_SHIFT_IN="%{$terminfo[smacs]%}"
PR_SHIFT_OUT="%{$terminfo[rmacs]%}"
PR_HBAR="${PR_SHIFT_IN}${altchar[q]:--}${PR_SHIFT_OUT}"
PR_ULCORNER="${PR_SHIFT_IN}${altchar[l]:--}${PR_SHIFT_OUT}"
PR_LLCORNER="${PR_SHIFT_IN}${altchar[m]:--}${PR_SHIFT_OUT}"
PR_LRCORNER="${PR_SHIFT_IN}${altchar[j]:--}${PR_SHIFT_OUT}"
PR_URCORNER="${PR_SHIFT_IN}${altchar[k]:--}${PR_SHIFT_OUT}"
fi
# Decide if we need to set titlebar text.
case $TERM in
xterm*)
PR_TITLEBAR=$'%{\e]0;%(!.-=*[ROOT]*=- | .)%n@%m:%~ | ${COLUMNS}x${LINES} | %y\a%}'
;;
screen)
PR_TITLEBAR=$'%{\e_screen \005 (\005t) | %(!.-=[ROOT]=- | .)%n@%m:%~ | ${COLUMNS}x${LINES} | %y\e\\%}'
;;
*)
PR_TITLEBAR=""
;;
esac
# Decide whether to set a screen title
if [[ "$TERM" = "screen" ]]; then
PR_STITLE=$'%{\ekzsh\e\\%}'
else
PR_STITLE=""
fi
# Finally, the prompt.
PROMPT='${PR_SET_CHARSET}${PR_STITLE}${(e)PR_TITLEBAR}\
${PR_NORD8}${PR_ULCORNER}${PR_HBAR}${PR_NORD3}(\
${PR_NORD14}%${PR_PWDLEN}<...<%~%<<\
${PR_NORD3})$(virtualenv_prompt_info)$(ruby_prompt_info)${PR_NORD8}${PR_HBAR}${PR_HBAR}${(e)PR_FILLBAR}${PR_HBAR}${PR_NORD3}(\
${PR_NORD8}%(!.%SROOT%s.%n)${PR_NORD3}@${PR_NORD14}%m:%l\
${PR_NORD3})${PR_NORD8}${PR_HBAR}${PR_URCORNER}\
${PR_NORD8}${PR_LLCORNER}${PR_NORD9}${PR_HBAR}(\
${PR_NORD13}%D{%H:%M:%S}\
${PR_NORD8}%{$reset_color%}$(git_prompt_info)$(git_prompt_status)${PR_NORD9})${PR_NORD8}${PR_HBAR}\
${PR_HBAR}\
>${PR_NO_COLOUR} '
# display exitcode on the right when > 0
return_code="%(?..${PR_NORD11}%? ↵ ${PR_NO_COLOUR})"
RPROMPT=' $return_code${PR_NORD8}${PR_HBAR}${PR_NORD9}${PR_HBAR}\
(${PR_NORD13}%D{%a,%b%d}${PR_NORD9})${PR_HBAR}${PR_NORD8}${PR_LRCORNER}${PR_NO_COLOUR}'
PS2='${PR_NORD8}${PR_HBAR}\
${PR_NORD9}${PR_HBAR}(\
${PR_NORD14}%_${PR_NORD9})${PR_HBAR}\
${PR_NORD8}${PR_HBAR}${PR_NO_COLOUR} '
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment