zsh: change prompt color based on current fronter

This commit is contained in:
Erin 2023-07-14 15:47:52 -04:00
parent 9a730a128b
commit 336b99ac32
3 changed files with 20 additions and 1 deletions

View file

@ -0,0 +1,14 @@
# fetches the color of the currently fronting PK member
function fetch_fronter_color {
[ -n "$PLURALKIT_API_TOKEN" ] || return 1
[ -n "$PLURALKIT_SYSTEM_ID" ] || return 1
endpoint="https://api.pluralkit.me/v2/systems/$PLURALKIT_SYSTEM_ID/fronters"
color=$(curl -sH "Authorization: $PLURALKIT_API_TOKEN" "$endpoint" | jq -r ".members[0].color // empty")
[ -n "$color" ] && echo "#$color"
}
# fetch the fronter color every precmd and export it for the prompt
precmd_functions+="set_fronter_color"
function set_fronter_color {
export fronter_color="$(eval_with_secrets fetch_fronter_color)"
}

View file

@ -0,0 +1,5 @@
function eval_with_secrets {
PLURALKIT_API_TOKEN="" \
PLURALKIT_SYSTEM_ID="" \
eval $@
}

View file

@ -9,7 +9,7 @@ autoload -Uz vcs_info
precmd_functions+="vcs_info"
# Prompt shows hostname if connected to a remote or if root
PROMPT="%B$([ -n "$is_remote" ] || [ "$EUID" = 0 ] && echo "%F{magenta}%m%f ")%F{$([ "$EUID" = 0 ] && echo "red" || echo "cyan")}%n%f %F{blue}%~%f%b \${vcs_info_msg_0_}%B%#%b "
PROMPT="%B$([ -n "$is_remote" ] || [ "$EUID" = 0 ] && echo "%F{magenta}%m%f ")%F{$([ "$EUID" = 0 ] && echo "red" || echo "\${fronter_color:-cyan}")}%n%f %F{blue}%~%f%b \${vcs_info_msg_0_}%B%#%b "
# The line before the prompt is used by RPROMPT for information about the previous command - leave space
NEWLINE=$'\n'