Make solarized8.sh sh compatible

The script contained bashisms like substring expansions and a double
quoted tests but still had a /bin/sh shebang, which broke execution on
distros where sh is pointing to `dash` or something other than bash.
Modern Debian is one such example.

Additionally, as the regex pattern was quoted in the original test it
was interpreted literally and not as a regex pattern so the original
logic did not work as intended, even if executed in bash.

The new version keeps the same logic (although without the regex issue)
but implemented in a way that should be compliant with most sh shells,
and perhaps a bit easier to extend with other shells.

The script now depends on sed to replace the bash substring expansion
logic, but sed should be available on almost any system.
This commit is contained in:
Hans-Kristian Bakke
2021-03-09 17:17:08 +01:00
parent 2dd890729e
commit dab034b32e

View File

@@ -4,26 +4,33 @@
set -o errexit set -o errexit
hex2rgb() { hex2rgb() {
local s=$1 echo "${1##\#}" | sed 's/.\{2\}/&\//g;s/\/$//'
local r=${s:1:2}
local g=${s:3:2}
local b=${s:5:2}
echo "$r/$g/$b"
} }
if [ "${TERM%%-*}" = 'linux' ]; then tmux_term="no"
# This script doesn't support linux console supported="yes"
# Terminal classification
case "$TERM" in
screen*|tmux*)
tmux_term="yes"
;;
linux*)
supported="no"
;;
esac
if [ "$supported" = "no" ]; then
return 2>/dev/null || exit 0 return 2>/dev/null || exit 0
fi fi
if [[ "$TERM" =~ "^(tmux|screen).*" && -n "$TMUX" ]]; then if [ -n "$TMUX" ] && [ "$tmux_term" = "yes" ]; then
# tell tmux to pass the escape sequences through # tell tmux to pass the escape sequences through
# (Source: http://permalink.gmane.org/gmane.comp.terminal-emulators.tmux.user/1324) # (Source: http://permalink.gmane.org/gmane.comp.terminal-emulators.tmux.user/1324)
printf_template="\033Ptmux;\033\033]4;%d;rgb:%s\007\033\\" printf_template="\033Ptmux;\033\033]4;%d;rgb:%s\007\033\\"
printf_template_var="\033Ptmux;\033\033]%d;rgb:%s\007\033\\" printf_template_var="\033Ptmux;\033\033]%d;rgb:%s\007\033\\"
printf_template_custom="\033Ptmux;\033\033]%s%s\007\033\\" printf_template_custom="\033Ptmux;\033\033]%s%s\007\033\\"
elif [ "${TERM%%-*}" = "screen" ]; then elif [ "${TERM%%-*}" = "screen" ]; then
# GNU screen (screen, screen-256color, screen-256color-bce)
printf_template="\033P\033]4;%d;rgb:%s\007\033\\" printf_template="\033P\033]4;%d;rgb:%s\007\033\\"
printf_template_var="\033P\033]%d;rgb:%s\007\033\\" printf_template_var="\033P\033]%d;rgb:%s\007\033\\"
printf_template_custom="\033P\033]%s%s\007\033\\" printf_template_custom="\033P\033]%s%s\007\033\\"
@@ -69,3 +76,5 @@ printf $printf_template 106 $(hex2rgb "#859900")
unset printf_template unset printf_template
unset printf_template_var unset printf_template_var
unset printf_template_custom unset printf_template_custom
unset tmux_term
unset supported