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