Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
### Random terminal colors
# Assign _PS1, _PS2, and _PS3 arrays
_PS1=(
# Orange
'\[\e[38;5;208m\] \w $(__git_ps1 "\[\e[38;5;166m\]%s ")\[\e[38;5;202m\]❱ \[\e[0m\]'
# Blue
'\[\e[38;5;45m\] \w $(__git_ps1 "\[\e[38;5;39m\]%s ")\[\e[38;5;33m\]❱ \[\e[0m\]'
# Green
'\[\e[38;5;46m\] \w $(__git_ps1 "\[\e[38;5;34m\]%s ")\[\e[38;5;28m\]❱ \[\e[0m\]'
# Yellow
'\[\e[38;5;226m\] \w $(__git_ps1 "\[\e[38;5;220m\]%s ")\[\e[38;5;214m\]❱ \[\e[0m\]'
)
_PS2=(
# Orange
'\[\e[38;5;202m\] ❱ \[\e[0m\]'
# Blue
'\[\e[38;5;33m\] ❱ \[\e[0m\]'
# Green
'\[\e[38;5;28m\] ❱ \[\e[0m\]'
# Yellow
'\[\e[38;5;214m\] ❱ \[\e[0m\]'
)
_PS4=(
# Orange
' \e[38;5;202m▎ \e[0m'
# Blue
' \e[38;5;33m▎ \e[38;5;231m'
# Green
' \e[38;5;28m▎ \e[38;5;231m'
# Yellow
' \e[38;5;214m▎ \e[38;5;231m'
)
# Set PS1, PS2, and PS3 to a random selection.
random_terminal_color() {
local length=`expr ${#_PS1[@]} - 1`
# Make sure none of the colors are used twice in a row
if [[ $_TERMINAL_COLOR ]]; then
local rand=-1
until [ $rand != $_TERMINAL_COLOR -a $rand -gt -1 ]; do
local rand=$((RANDOM%$length+1))
#local rand=`shuf -i 0-$length -n1`
done
export _TERMINAL_COLOR=$rand
else
export _TERMINAL_COLOR=$((RANDOM%$length))
#export _TERMINAL_COLOR=`shuf -i 0-$length -n1`
fi
export PS1="${_PS1[$_TERMINAL_COLOR]}"
export PS2="${_PS2[$_TERMINAL_COLOR]}"
export PS4="${_PS4[$_TERMINAL_COLOR]}"
}
_JUST_OPENED=true;
terminal_stuff() {
if [[ $_JUST_OPENED = true ]]; then
_JUST_OPENED=false
fi
}
clear_terminal() {
# Emulate the terminal just being opened, so we don't append a newline.
_JUST_OPENED=true
tput clear
}
# Alias clear to our clear_terminal function
alias clear=`echo clear_terminal`
# Use a new color every time, and run any custom stuff.
export PROMPT_COMMAND="random_terminal_color; terminal_stuff"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.