refactor(bash): improve handling of terminal colors

This commit cleans up the code a bit and uses rgb:/RR/GG/BB over the not
recommended #RRGGBB format to specify color values.
This commit is contained in:
Fernando Schauenburg 2020-12-04 14:31:24 +01:00
parent f2311bddc1
commit 46936dffa9

View file

@ -220,40 +220,68 @@ alias dark='_update_colors dark'
# * https://vt100.net/ # * https://vt100.net/
# * https://iterm2.com/utilities/imgcat # * https://iterm2.com/utilities/imgcat
_send_osc() { # In tmux, OSC sequences can be forwarded to the underlying terminal by:
local OSC ST # * Wrapping the sequence with `DCS tmux; <sequence> ST`
# * Replacing ESC in <sequence> with ESC ESC.
_osc_start() {
if [ -n "$TMUX" ]; then if [ -n "$TMUX" ]; then
OSC=$'\ePtmux;\e\e]' printf "\ePtmux;\e\e]"
ST=$'\e\e\\\e\\';
else else
OSC=$'\e]' printf "\e]"
ST=$'\e\\' fi
}
# Either ST or BEL (\a) can be used to end an OSC sequence. However, tmux
# requires ST to end its wrapping DCS.
_osc_end() {
if [ -n "$TMUX" ]; then
printf "\a\e\\"
else
printf "\a"
fi fi
echo -n "$OSC$1$ST"
} }
_update_colors() { _update_colors() {
local cursor fg bg n rgb
export BACKGROUND="$1" export BACKGROUND="$1"
if [ "$BACKGROUND" = "light" ]; then if [ "$BACKGROUND" = "light" ]; then
_set_terminal_colors $Base01_RGB $Base3_RGB $Red_RGB # fg bg cursor
PS1_SEP_COLOR=$(tput setaf $Base1) PS1_SEP_COLOR=$(tput setaf $Base1)
else else
_set_terminal_colors $Base1_RGB $Base03_RGB $Red_RGB # fg bg cursor
PS1_SEP_COLOR=$(tput setaf $Base01) PS1_SEP_COLOR=$(tput setaf $Base01)
fi fi
cursor=$Red_RGB if [ -n "$TMUX" ] && [ -f "$HOME/.tmux.conf" ]; then
if [ "$BACKGROUND" = "light" ]; then tmux set-environment -g BACKGROUND "$BACKGROUND"
fg=$Base01_RGB tmux source-file "$HOME/.tmux.conf"
bg=$Base3_RGB
else
fg=$Base1_RGB
bg=$Base03_RGB
fi fi
local ls_colors="$HOME/.config/dircolors/solarized-$BACKGROUND"
if type dircolors &>/dev/null && [ -f "$ls_colors" ]; then
eval "$(dircolors "$ls_colors")"
fi
}
_set_terminal_colors() { # 1: foreground, 2: background, 3: cursor
if [ -n "$ITERM_SESSION_ID" ]; then # iTerm2 if [ -n "$ITERM_SESSION_ID" ]; then # iTerm2
while read -r n rgb; do _send_osc "P$n$rgb"; done <<EOL _set_iterm2_colors "$1" "$2" "$3"
else # assume xterm
_set_xterm_colors "$1" "$2" "$3"
fi
}
# Documentation for iTerm2 at
# https://iterm2.com/documentation-escape-codes.html
# under the heading "Change the color palette".
_set_iterm2_colors() { # 1: foreground, 2: background, 3: cursor
local fg="$1" bg="$2" cursor="$3"
local n rgb
while read -r n rgb; do
_osc_start
printf "P%s%s" "$n" "$rgb"
_osc_end
done <<EOL
0 $Base02_RGB 0 $Base02_RGB
1 $Red_RGB 1 $Red_RGB
2 $Green_RGB 2 $Green_RGB
@ -278,8 +306,22 @@ _update_colors() {
l $cursor l $cursor
m $cursor m $cursor
EOL EOL
else # assume xterm }
while read -r n rgb; do _send_osc "4;$n;#$rgb"; done <<EOL
# Documentation for xterm at https://www.xfree86.org/4.8.0/ctlseqs.html
# under the heading "Operating System Controls".
_set_xterm_colors() { # $1 foreground, $2 background, $3 cursor
local fg="$1" bg="$2" cursor="$3"
local c rgb Ps
while read -r c rgb; do
# Send sequence OSC Ps BEL
# where Ps -> 4;c;spec
# c -> index of the ANSI color to change [0..15]
# spec -> RGB value of color as rgb:RR/GG/BB
_osc_start
printf "4;%s;rgb:%s/%s/%s" "$c" "${rgb:0:2}" "${rgb:2:2}" "${rgb:4:2}"
_osc_end
done <<EOL
0 $Base02_RGB 0 $Base02_RGB
1 $Red_RGB 1 $Red_RGB
2 $Green_RGB 2 $Green_RGB
@ -297,22 +339,18 @@ EOL
14 $Base1_RGB 14 $Base1_RGB
15 $Base3_RGB 15 $Base3_RGB
EOL EOL
while read -r n rgb; do _send_osc "$n;#$rgb"; done <<EOL while read -r Ps rgb; do
# Send sequence OSC Ps ; Pt BEL
# where Ps -> foreground (10), background (11), or cursor (12)
# Pt -> RGB value of color as rgb:RR/GG/BB
_osc_start
printf "%s;rgb:%s/%s/%s" "$Ps" "${rgb:0:2}" "${rgb:2:2}" "${rgb:4:2}"
_osc_end
done <<EOL
10 $fg 10 $fg
11 $bg 11 $bg
12 $cursor 12 $cursor
EOL EOL
fi
if [ -n "$TMUX" ] && [ -f "$HOME/.tmux.conf" ]; then
tmux set-environment -g BACKGROUND "$BACKGROUND"
tmux source-file "$HOME/.tmux.conf"
fi
local ls_colors="$HOME/.config/dircolors/solarized-$BACKGROUND"
if type dircolors &>/dev/null && [ -f "$ls_colors" ]; then
eval "$(dircolors "$ls_colors")"
fi
} }
############################################################################## ##############################################################################