dotfiles/bin/solarize
Fernando Schauenburg 4e29ff39e7 bash: fix LS_COLORS
The solarize script evalss the output of the dircolors(1) but the
LS_COLORS variable gets exported in its environmnent, not the calling
shell where we actually need it so ls(1) and other programs wiill
inherit it.

Therefore, the evaluation of the dircolors(1) output is moved to the
change_bg() function in the shell, so that the LS_COLORS variable is
available to any children of the shell.

The reason why I had missed this is that in most systems I have ls
aliased to exa, which does not care about LS_COLORS and has its own
coloring system. On cygwin, however, exa is not available and I noticed
that the colors were missind; and indeed, on systems with exa the colors
are also missing if I run ls as \ls.
2021-01-05 18:02:40 +01:00

134 lines
4 KiB
Bash
Executable file

#!/bin/bash
# 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 <<EOL
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
EOL
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
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
printf "%s4;%s;rgb:%s/%s/%s%s" "$_OSC" "$c" "${rgb:0:2}" "${rgb:2:2}" "${rgb:4:2}" "$_ST"
done <<EOL
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
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
printf "%s%s;rgb:%s/%s/%s%s" "$_OSC" "$Ps" "${rgb:0:2}" "${rgb:2:2}" "${rgb:4:2}" "$_ST"
done <<EOL
10 $1
11 $2
12 $3
EOL
}
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
}
main