zsh: add icons to prompt

This commit is contained in:
Fernando Schauenburg 2024-07-28 22:52:19 +02:00
parent 745b5b2fd5
commit dbac417322

View file

@ -16,6 +16,17 @@ typeset -gA zfg=(
[faded]='%{%F{240}%}'
)
typeset -gA icons=(
[user]=" "
[folder]=" "
[branch]=" "
[tag]="⚑ "
[detached]=" "
[python]=" "
[background]=" "
[clock]=" "
)
render_prompt() {
setopt localoptions shortloops
@ -29,7 +40,7 @@ render_prompt() {
"$(render_exec_time)"
)
local separator=" ${zfg[faded]} "
local separator="${zfg[faded]} "
echo "${(@pj.$separator.)sections:#}${zfg[gray]}"
(($SHLVL > 1)) && printf '󰅂%.0s' {2..$SHLVL}
echo -n " ${zfg[rst]}"
@ -50,9 +61,9 @@ render_user_host() {
# username in red if root, yellow if otherwise relevant
if [[ $UID == 0 ]]; then
parts+="${zfg[red]}%n"
parts+="${zfg[red]}${icons[user]}%n"
elif [[ $LOGNAME != $USER ]] || [[ -n $SSH_CONNECTION ]]; then
parts+="${zfg[yellow]}%n"
parts+="${zfg[yellow]}${icons[user]}%n"
fi
# hostname in yellow if relevant
@ -65,7 +76,7 @@ render_user_host() {
}
render_pwd() {
print "${zfg[cyan]}%~"
print "${zfg[cyan]}${icons[folder]}%~"
}
render_git() {
@ -92,30 +103,26 @@ render_git() {
local branch='' upstream='' ahead=0 behind=0
{
local fields=(${(s:...:)${branch_line#\#\# }})
branch=$fields[1]
branch="${icons[branch]}$fields[1]"
local tracking=$fields[2]
if [[ $branch == *'Initial commit on'* ]] \
|| [[ $branch == *'No commits yet on'* ]]; then
if [[ $branch == *'Initial commit'* ]] || [[ $branch == *'No commits'* ]]; then
# Branch name is last word in these possible branch lines:
# ## Initial commit on <branch>
# ## No commits yet on <branch>
branch=${${(s: :)branch}[-1]}
branch="${icons[branch]}${${(s: :)branch}[-1]}"
elif [[ $branch == *'no branch'* ]]; then
# Dettached HEAD (also if a tag is checked out), branch line:
# ## HEAD (no branch)
local tag=$(command git describe --tags --exact-match HEAD 2>/dev/null)
if [[ -n $tag ]]; then
branch="$tag"
else
tag=$(command git describe --tags --long HEAD 2>/dev/null)
if [[ -n $tag ]]; then
branch="$tag"
else
branch="#$(command git rev-parse --short HEAD 2>/dev/null)"
fi
local icon="${icons[tag]}"
local ref=$(command git describe --tags --exact-match HEAD 2>/dev/null)
if [[ -z $ref ]]; then
icon="${icons[detached]}"
ref=$(command git describe --tags --long HEAD 2>/dev/null)
[[ -n $ref ]] || ref=$(command git rev-parse --short HEAD 2>/dev/null)
fi
branch="${icon}%{%B%}${ref}%{%b%}"
elif (($#fields > 1)); then
# There is a tracking branch. Possibilites:
@ -153,11 +160,11 @@ render_git() {
}
render_venv() {
[[ -n "$VIRTUAL_ENV" ]] && print "${zfg[green]}${VIRTUAL_ENV:t}"
[[ -n "$VIRTUAL_ENV" ]] && print "${zfg[green]}${icons[python]}${VIRTUAL_ENV:t}"
}
render_jobs() {
(($PROMPT_JOB_COUNT > 0)) && print "${zfg[magenta]}%j bg"
(($PROMPT_JOB_COUNT > 0)) && print "${zfg[magenta]}${icons[background]}%j"
}
render_exec_time() {
@ -169,7 +176,7 @@ render_exec_time() {
"$((PROMPT_EXEC_TIME / 60 % 60))m" # minutes
"$((PROMPT_EXEC_TIME % 60))s" # seconds
)
print ${zfg[gray]}${parts:#0*} # only keep non-zero parts
print ${zfg[gray]}${icons[clock]}${parts:#0*} # only keep non-zero parts
}
# Hook triggered when a command is about to be executed.