Last active
June 20, 2024 00:40
-
-
Save tomgidden/f6e11571d4f6e52faa45ebecc4d18a7b to your computer and use it in GitHub Desktop.
.zshrc file (or /etc/zshrc) to automatically change terminal colour and prompt for different tabs/windows or remote hosts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### ------------------------------------------------------------------------ | |
### Terminal window colour from context | |
### https://gist.github.com/tomgidden/f6e11571d4f6e52faa45ebecc4d18a7b | |
# https://www.xfree86.org/current/ctlseqs.html | |
# Italic: "\x1b[38;2;3m" | |
# Bright: "\x1b[38;2;0m" | |
tty_color () { | |
local C=$1 | |
# Set this to empty to exclude the current dir snippet | |
local INCLUDE_DIR='%8>…>%1~%<< ' | |
if [[ -z "$C" ]]; then | |
# No parameter so override to white-on-black, old-style | |
local BG="rgb:0/0/0" | |
local FG="rgb:f/f/f" | |
PS1=$'%B%(!.%S.)%n%s@%m%u %! %0(?.%?.%F{red}%?%f) '$INCLUDE_DIR$'%#%b ' | |
else | |
local PALETTE=( | |
rgb:0d/0d/0d rgb:80/80/80 | |
rgb:19/0b/0e rgb:ff/66/85 | |
rgb:0f/19/0b rgb:99/ff/66 | |
rgb:0b/0f/19 rgb:66/99/ff | |
rgb:1a/1a/09 rgb:ff/ff/66 | |
rgb:0c/16/18 rgb:66/e5/ff | |
rgb:12/0b/19 rgb:b2/66/ff | |
rgb:1a/0f/09 rgb:ff/99/66 | |
rgb:17/17/17 rgb:80/80/80 | |
rgb:23/1d/1e rgb:c6/9f/a7 | |
rgb:1f/23/1d rgb:ac/c6/9f | |
rgb:1d/1f/23 rgb:9f/ac/c6 | |
rgb:24/24/1c rgb:c9/c9/9c | |
rgb:1e/22/23 rgb:a3/bd/c2 | |
rgb:20/1d/23 rgb:b2/9f/c6 | |
rgb:24/1f/1c rgb:c9/ab/9c | |
) | |
[[ -f ~/.tty_color_palette ]] && . ~/.tty_color_palette | |
local BG=$PALETTE[$((1+$C*2))] | |
local FG=$PALETTE[$((2+$C*2))] | |
# We use colour 231 (a duplicate white) and redefine it using \033]4;231;rgb\007 | |
# If this is a problem, we could expand PALETTE to include appropriate codes in | |
# the 255 color palette. This is because simple inline text colour can't be arbitrary | |
# RGB, as far as I know. | |
PS1=$'%B%(!.%S.)%{\033[38;5;231m%}%n%s@%m%{\033[39m%}%u %! %0(?.%?.%S%?%s) '$INCLUDE_DIR$'%#%b ' | |
fi | |
[[ -n $BG ]] && PS1=$'%{\033]11;'$BG$'\007%}'$PS1 | |
[[ -n $FG ]] && PS1=$'%{\033]12;'$FG$'\007\033]4;231;'$FG$'\007%}'$PS1 | |
} | |
function () { | |
local C='' | |
local S=$HOST | |
if [[ -r ~/.tty_color.$HOST ]]; then | |
tty_color "$(< ~/.tty_color.$HOST)" | |
elif [[ -r ~/.tty_color ]]; then | |
tty_color "$(< ~/.tty_color)" | |
else | |
if [[ -z $SSH_TTY ]]; then | |
# Local: color by tty; use the offset palette (8-15) | |
C=$(( 8 + ${$(tty)##*[!0-9.]} % 8 )) | |
else | |
# Remote host; use the base palette (0-7)) | |
if [[ $HOST =~ [0-9]+$ ]]; then | |
# Color by digits in hostname | |
C=$(( ${HOST##*[!0-9]} % 8 )) | |
else | |
# No digits: "hash" the hostname. Yuck. | |
while [[ -n "$S" ]]; do | |
C=$((C>>1 + $(printf "%d" "'${S[1]}'") )) | |
S=${S#?} | |
done | |
C=$(( C % 8 )) | |
fi | |
fi | |
tty_color $C | |
fi | |
} | |
# Set terminal title to $HOST | |
PS1=$'%{\033]0;'$USER@$HOST$'\007%}'$PS1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment