solarize: fix substring error when /bin/sh is not bash

The substring expansion ${parameter:offset:length} is a bashism and does
not work when /bin/sh points to a shell other than bash, e.g. on Ubuntu.

This commit replaces this expansion with a call to awk, inspired by the
solution at:

        https://wiki.ubuntu.com/DashAsBinSh
This commit is contained in:
Fernando Schauenburg 2021-03-09 17:04:11 +01:00
parent 9c4c579938
commit a5322715f6

View file

@ -40,8 +40,8 @@ fi
# under the heading "Change the color palette". # under the heading "Change the color palette".
set_iterm2_colors() { # 1: foreground, 2: background, 3: cursor set_iterm2_colors() { # 1: foreground, 2: background, 3: cursor
while read -r key rgb; do while read -r key rgb; do
printf "%s1337;SetColors=%s=%s%s" "$_OSC" "$key" "${rgb}" "$_ST" printf "%s1337;SetColors=%s=%s%s" "$_OSC" "$key" "$rgb" "$_ST"
done <<EOL done <<EOF
black $Base02 black $Base02
red $Red red $Red
green $Green green $Green
@ -66,20 +66,20 @@ set_iterm2_colors() { # 1: foreground, 2: background, 3: cursor
curfg $3 curfg $3
curbg $3 curbg $3
link $Cyan link $Cyan
EOL EOF
printf "%s1337;CursorShape=2%s" "$_OSC" "$_ST" # set cursor shape to underline 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 # Documentation for xterm at https://www.xfree86.org/4.8.0/ctlseqs.html
# under the heading "Operating System Controls". # under the heading "Operating System Controls".
set_xterm_colors() { # $1 foreground, $2 background, $3 cursor set_xterm_colors() { # $1 foreground, $2 background, $3 cursor
while read -r c rgb; do
# Send sequence OSC Ps BEL # Send sequence OSC Ps BEL
# where Ps -> 4;c;spec # where Ps -> 4;c;spec
# c -> index of the ANSI color to change [0..15] # c -> index of the ANSI color to change [0..15]
# spec -> RGB value of color as rgb:RR/GG/BB # 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" awk -v osc="$_OSC" -v st="$_ST" -v ORS="" \
done <<EOL '{print osc "4;" $1 ";rgb:" substr($2,1,2) "/" substr($2,3,2) "/" substr($2,5,2) st}' \
<<EOF
0 $Base02 0 $Base02
1 $Red 1 $Red
2 $Green 2 $Green
@ -96,17 +96,17 @@ set_xterm_colors() { # $1 foreground, $2 background, $3 cursor
13 $Violet 13 $Violet
14 $Base1 14 $Base1
15 $Base3 15 $Base3
EOL EOF
while read -r Ps rgb; do
# Send sequence OSC Ps ; Pt BEL # Send sequence OSC Ps ; Pt BEL
# where Ps -> foreground (10), background (11), or cursor (12) # where Ps -> foreground (10), background (11), or cursor (12)
# Pt -> RGB value of color as rgb:RR/GG/BB # 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" awk -v osc="$_OSC" -v st="$_ST" -v ORS="" \
done <<EOL '{print osc $1 ";rgb:" substr($2,1,2) "/" substr($2,3,2) "/" substr($2,5,2) st}' \
<<EOF
10 $1 10 $1
11 $2 11 $2
12 $3 12 $3
EOL EOF
} }
set_terminal_colors() { # 1: foreground, 2: background, 3: cursor set_terminal_colors() { # 1: foreground, 2: background, 3: cursor