bash: factor out terminal setup into a separate script

This commit is contained in:
Fernando Schauenburg 2020-12-26 03:37:55 +01:00
parent 59cff72b54
commit b009800b31
5 changed files with 203 additions and 227 deletions

View file

@ -11,6 +11,7 @@ export XDG_CACHE_HOME="${XDG_CACHE_HOME:-$HOME/.cache}"
export XDG_CONFIG_HOME="${XDG_CONFIG_HOME:-$HOME/.config}" export XDG_CONFIG_HOME="${XDG_CONFIG_HOME:-$HOME/.config}"
export XDG_DATA_HOME="${XDG_DATA_HOME:-$HOME/.local/share}" export XDG_DATA_HOME="${XDG_DATA_HOME:-$HOME/.local/share}"
export BACKGROUND="dark"
export EDITOR="vim" export EDITOR="vim"
export INPUTRC="$XDG_CONFIG_HOME/readline/inputrc" export INPUTRC="$XDG_CONFIG_HOME/readline/inputrc"
export LANG="en_US.UTF-8" export LANG="en_US.UTF-8"
@ -77,6 +78,9 @@ EOL
unset dir unset dir
stty -ixon # disable ctrl-s and ctrl-q
command -v solarize >/dev/null && solarize
############################################################################## ##############################################################################
# Customize shell options & variables # Customize shell options & variables
############################################################################## ##############################################################################
@ -91,53 +95,25 @@ HISTSIZE=
HISTTIMEFORMAT="[%F %T] " HISTTIMEFORMAT="[%F %T] "
HISTFILE="$XDG_DATA_HOME/bash/history" HISTFILE="$XDG_DATA_HOME/bash/history"
_tput_wrapper() { Reset=$(tput sgr0)
local fmt="$1"; shift Base03=8 Base02=0 Base01=10 Base00=11 Base0=12 Base1=14 Base2=7 Base3=15
printf "$fmt" "$@" | tput -S | sed -E "s/\x1B\[/ \x1B\[/g" Red=1 Orange=9 Yellow=3 Green=2 Cyan=6 Blue=4 Violet=13 Magenta=5
}
# shellcheck disable=SC2034 # these variable are meant for use in shell only PS1_EXIT=$(tput setaf "$Red" ) # color for last exit code if non-zero
{ PS1_ROOT=$(tput setaf "$Orange" ) # logged in as root
Reset="$(tput sgr0)" PS1_SSH=$(tput setaf "$Yellow" ) # hostname for SSH sessions
read -a _FG <<<$(_tput_wrapper 'setaf %d\n' {0..15}) PS1_PWD=$(tput setaf "$Cyan" ) # PWD color
read -a _BG <<<$(_tput_wrapper 'setab %d\n' {0..15}) PS1_GIT=$(tput setaf "$Blue" ) # color for git branch
PS1_VENV=$(tput setaf "$Violet" ) # color for python virtual env
# Color definitions (from http://ethanschoonover.com/solarized) PS1_JOBS=$(tput setaf "$Magenta") # color for background jobs
# CSI foreground CSI background RGB HEX ANSI Index ANSI Codes PS1_SEP_LIGHT=$(tput setaf "$Base1" )
# ---------------- --------------------- -------------------- ----------- ------------ PS1_SEP_DARK=$(tput setaf "$Base01" )
Base03=${_FG[ 8]} Base03_BG=${_BG[ 8]} Base03_RGB="002B36" Base03_N=8 # 1;30 brblack
Base02=${_FG[ 0]} Base02_BG=${_BG[ 0]} Base02_RGB="073642" Base02_N=0 # 0;30 black
Base01=${_FG[10]} Base01_BG=${_BG[10]} Base01_RGB="586E75" Base01_N=10 # 1;32 brgreen
Base00=${_FG[11]} Base00_BG=${_BG[11]} Base00_RGB="657B83" Base00_N=11 # 1;33 bryellow
Base0=${_FG[12]} Base0_BG=${_BG[12]} Base0_RGB="839496" Base0_N=12 # 1;34 brblue
Base1=${_FG[14]} Base1_BG=${_BG[14]} Base1_RGB="93A1A1" Base1_N=14 # 1;36 brcyan
Base2=${_FG[ 7]} Base2_BG=${_BG[ 7]} Base2_RGB="EEE8D5" Base2_N=7 # 0;37 white
Base3=${_FG[15]} Base3_BG=${_BG[15]} Base3_RGB="FDF6E3" Base3_N=15 # 1;37 brwhite
Red=${_FG[ 1]} Red_BG=${_BG[ 1]} Red_RGB="DC322F" Red_N=1 # 0;31 red
Orange=${_FG[ 9]} Orange_BG=${_BG[ 9]} Orange_RGB="CB4B16" Orange_N=9 # 1;31 brred
Yellow=${_FG[ 3]} Yellow_BG=${_BG[ 3]} Yellow_RGB="B58900" Yellow_N=3 # 0;33 yellow
Green=${_FG[ 2]} Green_BG=${_BG[ 2]} Green_RGB="859900" Green_N=2 # 0;32 green
Cyan=${_FG[ 6]} Cyan_BG=${_BG[ 6]} Cyan_RGB="2AA198" Cyan_N=6 # 0;36 cyan
Blue=${_FG[ 4]} Blue_BG=${_BG[ 4]} Blue_RGB="268BD2" Blue_N=4 # 0;34 blue
Violet=${_FG[13]} Violet_BG=${_BG[13]} Violet_RGB="6C71C4" Violet_N=13 # 1;35 brmagenta
Magenta=${_FG[ 5]} Magenta_BG=${_BG[ 5]} Magenta_RGB="D33682" Magenta_N=5 # 0;35 magenta
PS1_EXIT="$Red" # color for last exit code if non-zero
PS1_ROOT="$Orange" # logged in as root
PS1_SSH="$Yellow" # hostname for SSH sessions
PS1_PWD="$Cyan" # PWD color
PS1_GIT="$Blue" # color for git branch
PS1_VENV="$Violet" # color for python virtual env
PS1_JOBS="$Magenta" # color for background jobs
PS1_SEP=" > " # separator between prompt parts PS1_SEP=" > " # separator between prompt parts
PS1_SEP_LIGHT="$Base1"
PS1_SEP_DARK="$Base01"
GIT_PS1_SHOWDIRTYSTATE=1 GIT_PS1_SHOWDIRTYSTATE=1
GIT_PS1_SHOWSTASHSTATE=1 GIT_PS1_SHOWSTASHSTATE=1
GIT_PS1_SHOWUNTRACKEDFILES=1 GIT_PS1_SHOWUNTRACKEDFILES=1
GIT_PS1_SHOWUPSTREAM=verbose GIT_PS1_SHOWUPSTREAM=verbose
}
PROMPT_COMMAND=__ps1_set PROMPT_COMMAND=__ps1_set
PS2="... " PS2="... "
@ -160,7 +136,7 @@ __ps1_set() {
local extra="" local extra=""
[ ${#ps[@]} -gt 1 ] && printf -v extra "$sep%s" "${ps[@]:1}" [ ${#ps[@]} -gt 1 ] && printf -v extra "$sep%s" "${ps[@]:1}"
PS1="\n${ps[0]}$extra$Reset\n${prompt:0:$SHLVL} " PS1="${ps[0]}$extra$Reset ${prompt:0:$SHLVL} "
} &>/dev/null } &>/dev/null
############################################################################## ##############################################################################
@ -217,146 +193,8 @@ alias myip="curl -s https://ifconfig.co"
#alias myip="curl -s https://ifconfig.me" #alias myip="curl -s https://ifconfig.me"
#alias myip="dig +short myip.opendns.com @resolver1.opendns.com" #alias myip="dig +short myip.opendns.com @resolver1.opendns.com"
alias light='_update_colors light' alias dark='change_bg dark'
alias dark='_update_colors dark' alias light='change_bg light'
##############################################################################
# Terminal color management
##############################################################################
# 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
_update_colors() {
export BACKGROUND="$1"
if [ "$BACKGROUND" = "light" ]; then
_set_terminal_colors $Base01_RGB $Base3_RGB $Red_RGB # fg bg cursor
else
_set_terminal_colors $Base1_RGB $Base03_RGB $Red_RGB # 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
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
_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 key rgb
while read -r key rgb; do
printf "%s1337;SetColors=%s=%s%s" "$_OSC" "$key" "${rgb}" "$_ST"
done <<EOL
black $Base02_RGB
red $Red_RGB
green $Green_RGB
yellow $Yellow_RGB
blue $Blue_RGB
magenta $Magenta_RGB
cyan $Cyan_RGB
white $Base2_RGB
br_black $Base03_RGB
br_red $Orange_RGB
br_green $Base01_RGB
br_yellow $Base00_RGB
br_blue $Base0_RGB
br_magenta $Violet_RGB
br_cyan $Base1_RGB
br_white $Base3_RGB
fg $fg
bg $bg
bold $fg
selfg $Base2_RGB
selbg $Base01_RGB
curfg $cursor
curbg $cursor
link $Cyan_RGB
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
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
printf "%s4;%s;rgb:%s/%s/%s%s" "$_OSC" "$c" "${rgb:0:2}" "${rgb:2:2}" "${rgb:4:2}" "$_ST"
done <<EOL
0 $Base02_RGB
1 $Red_RGB
2 $Green_RGB
3 $Yellow_RGB
4 $Blue_RGB
5 $Magenta_RGB
6 $Cyan_RGB
7 $Base2_RGB
8 $Base03_RGB
9 $Orange_RGB
10 $Base01_RGB
11 $Base00_RGB
12 $Base0_RGB
13 $Violet_RGB
14 $Base1_RGB
15 $Base3_RGB
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 $fg
11 $bg
12 $cursor
EOL
}
############################################################################## ##############################################################################
# Add shell functions # Add shell functions
@ -368,13 +206,12 @@ ltree() { tree -C "$@" | less -R; }
mkcd() { mkdir -p -- "$1" && cd -P -- "$1" || return; } mkcd() { mkdir -p -- "$1" && cd -P -- "$1" || return; }
change_bg() { BACKGROUND="$1"; command -v solarize >/dev/null && solarize; }
############################################################################## ##############################################################################
# Run external customizations # Run external customizations
############################################################################## ##############################################################################
stty -ixon # disable ctrl-s and ctrl-q
_update_colors "${BACKGROUND:-dark}"
_source_extra_configs() { _source_extra_configs() {
local configs=( local configs=(
/usr/local/etc/profile.d/bash_completion.sh /usr/local/etc/profile.d/bash_completion.sh

View file

@ -1,11 +1,30 @@
#!/bin/sh #!/bin/sh
# Print all 256 colors
for i in $(seq 0 15); do print_color() {
for j in $(seq 0 15); do printf '%s%6s%s %s#%s %2s %s%s\n' "$(tput setab "$3")" \
n=$(( 16 * i + j )) '' \
printf '%s %3d ' "$(tput setab $n)" "$n" "$(tput sgr0)" \
done "$(tput setaf "$3")" \
printf '%s\n' "$(tput sgr0)" "$2" \
done "$3" \
"$1" \
"$(tput sgr0)"
}
print_color Base03 "002B36" 8
print_color Base02 "073642" 0
print_color Base01 "586E75" 10
print_color Base00 "657B83" 11
print_color Base0 "839496" 12
print_color Base1 "93A1A1" 14
print_color Base2 "EEE8D5" 7
print_color Base3 "FDF6E3" 15
print_color Red "DC322F" 1
print_color Orange "CB4B16" 9
print_color Yellow "B58900" 3
print_color Green "859900" 2
print_color Cyan "2AA198" 6
print_color Blue "268BD2" 4
print_color Violet "6C71C4" 13
print_color Magenta "D33682" 5

View file

@ -0,0 +1,11 @@
#!/bin/sh
# Print all 256 colors
for i in $(seq 0 15); do
for j in $(seq 0 15); do
n=$(( 16 * i + j ))
printf '%s %3d ' "$(tput setab $n)" "$n"
done
printf '%s\n' "$(tput sgr0)"
done

139
roles/dotfiles/files/bin/solarize Executable file
View file

@ -0,0 +1,139 @@
#!/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
ls_colors="$HOME/.config/dircolors/solarized-$BACKGROUND"
if type dircolors >/dev/null && [ -f "$ls_colors" ]; then
eval "$(dircolors "$ls_colors")"
fi
}
main

View file

@ -1,30 +0,0 @@
#!/bin/sh
print_color() {
printf '%s%6s%s %s#%s %2s %s%s\n' "$(tput setab "$3")" \
'' \
"$(tput sgr0)" \
"$(tput setaf "$3")" \
"$2" \
"$3" \
"$1" \
"$(tput sgr0)"
}
print_color Base03 "002B36" 8
print_color Base02 "073642" 0
print_color Base01 "586E75" 10
print_color Base00 "657B83" 11
print_color Base0 "839496" 12
print_color Base1 "93A1A1" 14
print_color Base2 "EEE8D5" 7
print_color Base3 "FDF6E3" 15
print_color Red "DC322F" 1
print_color Orange "CB4B16" 9
print_color Yellow "B58900" 3
print_color Green "859900" 2
print_color Cyan "2AA198" 6
print_color Blue "268BD2" 4
print_color Violet "6C71C4" 13
print_color Magenta "D33682" 5