Skip to content

Instantly share code, notes, and snippets.

@princefishthrower
Created September 30, 2021 21:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save princefishthrower/a72ab2bf70b904bc1f4caee0ca92b9a5 to your computer and use it in GitHub Desktop.
Save princefishthrower/a72ab2bf70b904bc1f4caee0ca92b9a5 to your computer and use it in GitHub Desktop.
Transform your zsh shell prompt into a letter-level alternating color kaleidoscope!
function buildColorPrompt() {
# I always like showing what directory I am in
directory=$(pwd)
# Modify these to whatever you'd like!
PROMPT_TEXT="youruser@yourmachine [$directory]"
# Comma seperated colors - as many or as few as you'd like
PROMPT_COLORS="15"
# This will be the color of everything in the input part of the prompt (here set to 15 = white)
PROMPT_INPUT_COLOR="15"
# split PROMPT_COLORS into array
colors_array=("${(@s/,/)PROMPT_COLORS}") # @ modifier
# break PROMPT_TEXT into character array
letters=()
for (( i=1 ; i < ${#PROMPT_TEXT}+1 ; i++ )) {
letters[$i]=${PROMPT_TEXT:$i-1:1}
}
# build prompt with colors
color_index=1
ps1=""
for (( i=1 ; i < ${#letters[@]}+1 ; i++ )) {
# Determine color in this giant case statement
color="${colors_array[color_index]}"
# add to ps1 var - color, then letter, then the end formatter
ps1+="%F{$color}${letters[$i]}"
# reset color index if we are at the end of the color array, otherwise increment it
if (( $color_index == ${#colors_array[@]} ))
then
color_index=1
else
((color_index=color_index+1))
fi
}
# end color formating
ps1+="%F{$PROMPT_INPUT_COLOR} %# "
# Finally: set the PROMPT variable
PROMPT=$ps1
}
# set the precmd() hook to our custom function
precmd() {
buildColorPrompt;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment