dotfiles/bin/,solarize

139 lines
4.2 KiB
Bash
Executable file

#!/bin/sh
# Solarize the terminal
# Useful terminal control sequences:
#
# Sequence Abbrev Description
# ----- ----- -----------------------------
# ESC P DCS Device Control String
# ESC [ CSI Control Sequence Introducer
# ESC \ ST String Terminator
# ESC ] OSC Operating System Control
#
#
# More info at:
# * https://www.xfree86.org/4.8.0/ctlseqs.html
# * https://vt100.net/
# * https://iterm2.com/utilities/imgcat
# In tmux, OSC sequences can be forwarded to the underlying terminal by:
# * Wrapping the sequence with `DCS tmux; <sequence> ST`
# * Replacing ESC in <sequence> with ESC ESC.
# Either ST or BEL (\a) can be used to end an OSC sequence. However, tmux
# requires ST to end its wrapping DCS.
if [ -n "$TMUX" ]; then
_OSC=$(printf '\ePtmux;\e\e]')
_ST=$(printf '\a\e\\')
else
_OSC=$(printf '\e]')
_ST=$(printf '\a')
fi
# Color definitions (from http://ethanschoonover.com/solarized)
Base03="002B36" Base02="073642" Base01="586E75" Base00="657B83"
Base0="839496" Base1="93A1A1" Base2="EEE8D5" Base3="FDF6E3"
Red="DC322F" Orange="CB4B16" Yellow="B58900" Green="859900"
Cyan="2AA198" Blue="268BD2" Violet="6C71C4" Magenta="D33682"
# 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
while read -r key rgb; do
printf "%s1337;SetColors=%s=%s%s" "$_OSC" "$key" "$rgb" "$_ST"
done <<EOF
black $Base02
red $Red
green $Green
yellow $Yellow
blue $Blue
magenta $Magenta
cyan $Cyan
white $Base2
br_black $Base03
br_red $Orange
br_green $Base01
br_yellow $Base00
br_blue $Base0
br_magenta $Violet
br_cyan $Base1
br_white $Base3
fg $1
bg $2
bold $1
selfg $Base2
selbg $Base01
curfg $3
curbg $3
link $Cyan
EOF
printf "%s1337;CursorShape=2%s" "$_OSC" "$_ST" # set cursor shape to underline
}
# 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
# 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
awk -v osc="$_OSC" -v st="$_ST" -v ORS="" \
'{print osc "4;" $1 ";rgb:" substr($2,1,2) "/" substr($2,3,2) "/" substr($2,5,2) st}' \
<<EOF
0 $Base02
1 $Red
2 $Green
3 $Yellow
4 $Blue
5 $Magenta
6 $Cyan
7 $Base2
8 $Base03
9 $Orange
10 $Base01
11 $Base00
12 $Base0
13 $Violet
14 $Base1
15 $Base3
EOF
# 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
awk -v osc="$_OSC" -v st="$_ST" -v ORS="" \
'{print osc $1 ";rgb:" substr($2,1,2) "/" substr($2,3,2) "/" substr($2,5,2) st}' \
<<EOF
10 $1
11 $2
12 $3
EOF
}
set_terminal_colors() { # 1: foreground, 2: background, 3: cursor
if [ -n "$ITERM_SESSION_ID" ]; then
set_iterm2_colors "$1" "$2" "$3" # iTerm2
else
set_xterm_colors "$1" "$2" "$3" # assume xterm
fi
}
main() {
if [ "${BACKGROUND:-dark}" = "light" ]; then
set_terminal_colors $Base01 $Base3 $Red # fg bg cursor
else
set_terminal_colors $Base1 $Base03 $Red # fg bg cursor
fi
if [ -n "$TMUX" ] && [ -f "$XDG_CONFIG_HOME/tmux/tmux-colors.conf" ]; then
tmux set-environment -g BACKGROUND "$BACKGROUND"
tmux source-file "$XDG_CONFIG_HOME/tmux/tmux-colors.conf"
fi
ls_colors="$HOME/.config/dircolors/solarized-$BACKGROUND"
if type dircolors >/dev/null 2>&1 && [ -f "$ls_colors" ]; then
eval "$(dircolors "$ls_colors")"
fi
}
main