From dab034b32e1613b384427da145a1e873a254f008 Mon Sep 17 00:00:00 2001 From: Hans-Kristian Bakke Date: Tue, 9 Mar 2021 17:17:08 +0100 Subject: [PATCH] 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. --- scripts/solarized8.sh | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/scripts/solarized8.sh b/scripts/solarized8.sh index 3393ff7..80cc9c1 100755 --- a/scripts/solarized8.sh +++ b/scripts/solarized8.sh @@ -4,26 +4,33 @@ set -o errexit hex2rgb() { - local s=$1 - local r=${s:1:2} - local g=${s:3:2} - local b=${s:5:2} - echo "$r/$g/$b" + echo "${1##\#}" | sed 's/.\{2\}/&\//g;s/\/$//' } -if [ "${TERM%%-*}" = 'linux' ]; then - # This script doesn't support linux console +tmux_term="no" +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 fi -if [[ "$TERM" =~ "^(tmux|screen).*" && -n "$TMUX" ]]; then +if [ -n "$TMUX" ] && [ "$tmux_term" = "yes" ]; then # tell tmux to pass the escape sequences through # (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_var="\033Ptmux;\033\033]%d;rgb:%s\007\033\\" printf_template_custom="\033Ptmux;\033\033]%s%s\007\033\\" elif [ "${TERM%%-*}" = "screen" ]; then - # GNU screen (screen, screen-256color, screen-256color-bce) printf_template="\033P\033]4;%d;rgb:%s\007\033\\" printf_template_var="\033P\033]%d;rgb:%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_var unset printf_template_custom +unset tmux_term +unset supported