Make sgr builder more portable.

I forgot a few BASHisms in the code.
This commit is contained in:
2026-06-24 02:05:21 -04:00
parent 5d12f2dc32
commit 2640c6cb25
+38 -29
View File
@@ -1,4 +1,4 @@
#!/usr/bin/env bash
#!/bin/sh
# This script will take an HTML compatible hex colour string and turn it into
# a terminal sequence for true colour and other formats which best approximate
@@ -85,7 +85,7 @@ map vga-brightwhite "ffffff"
enable_greyscale_cheat=0
nocsi=0
if [[ $1 == "no-csi" ]]
if [ "$1" = "no-csi" ]
then
nocsi=1
shift 1
@@ -103,17 +103,17 @@ dump_colors()
*black*) text_color="ffffff" ;;
*) text_color="000000" ;;
esac
printf "%-24s${map[${key}]} |" ${key}
printf "%-24s$(map ${key} ) |" ${key}
printf "`$0 reset reverse bg:${text_color} fg:${key}`native-sample`$0 reset`|"
if [[ ! -v CSHENV_TERMINAL_COLORS ]] || (( ${CSHENV_TERMINAL_COLORS} >= 256 ))
if [ -z "${CSHENV_TERMINAL_COLORS+x}" ] || [ ${CSHENV_TERMINAL_COLORS} -ge 256 ]
then
printf "`env CSHENV_TERMINAL_COLORS=256 $0 reset reverse bg:${text_color} fg:${key}`8-bit sample`$0 reset`|"
fi
if [[ ! -v CSHENV_TERMINAL_COLORS ]] || (( ${CSHENV_TERMINAL_COLORS} >= 16 ))
if [ -z "${CSHENV_TERMINAL_COLORS+x}" ] || [ ${CSHENV_TERMINAL_COLORS} -ge 16 ]
then
printf "`env CSHENV_TERMINAL_COLORS=16 $0 reset reverse bg:${text_color} fg:${key}`4-bit sample`$0 reset`|"
fi
if [[ ! -v CSHENV_TERMINAL_COLORS ]] || (( ${CSHENV_TERMINAL_COLORS} >= 8 ))
if [ -z "${CSHENV_TERMINAL_COLORS+x}" ] || [ ${CSHENV_TERMINAL_COLORS} -ge 8 ]
then
printf "`env CSHENV_TERMINAL_COLORS=8 $0 reset reverse bg:${text_color} fg:${key}`3-bit sample`$0 reset`|"
fi
@@ -127,15 +127,15 @@ cube_row()
{
r_base=${1}
g=0
while (( g < 6 ))
while [ ${g} -lt 6 ]
do
u=0
while (( u < 3 ))
while [ ${u} -lt 3 ]
do
r=$(( r_base + u ))
b=0
while (( b < 6 ))
while [ ${b} -lt 6 ]
do
i=$(( 16 + ( r * 36 ) + ( g * 6 ) + ( b ) ))
@@ -163,7 +163,7 @@ greyscale_chart()
[ ${i} -ge 244 ] && txt=";30"
local k=$(( i + 12 ))
while (( i < k ))
while [ ${i} -lt ${k} ]
do
printf "[48;5;${i};37;1${txt}m%3i " ${i}
i=$(( i + 1 ))
@@ -176,7 +176,7 @@ color_chart()
i=0
# Base 16
while (( i < 16 ))
while [ ${i} -lt 16 ]
do
[ ${i} -gt 0 ] && txt=";30"
@@ -213,7 +213,7 @@ setup_color_depth()
then
use_3_bit=1
else
echo "ERROR!" 1>&2 ; exit -1
echo "ERROR!" 1>&2 ; exit 255
fi
fi
}
@@ -250,7 +250,7 @@ ansi_color()
{
if [ ${1} -gt 15 ]
then
echo "ERROR!" 1>&2 ; exit -1
echo "ERROR!" 1>&2 ; exit 255
elif [ ${1} -ge 8 ]
then
use_4_bit=1
@@ -269,17 +269,18 @@ ext_grey()
grey_val=${1}
if [ ${grey_val} -le 0 - ${grey_val} -gt 23 ]
then
echo "ERROR!" 1>&2 ; exit -1
echo "ERROR!" 1>&2 ; exit 255
fi
ext_8_bit=$(( 232 + ${grey_val} ))
}
function ext_rgb()
ext_rgb()
{
use_8_bit=1
red_ext_val=$((6#${1:0:1}))
green_ext_val=$((6#${1:1:1}))
blue_ext_val=$((6#${1:2:1}))
red_ext_val="${1%??*}"
tmp="${1#?}"
green_ext_val=${tmp%?*}
blue_ext_val=${tmp#?*}
ext_8_bit=$(( 16 + 36 * ${red_ext_val} + 6 * ${green_ext_val} + ${blue_ext_val} ))
}
@@ -288,7 +289,7 @@ ext_color()
use_8_bit=1
if [ ${1} -gt 255 ]
then
echo "ERROR!" 1>&2 ; exit -1
echo "ERROR!" 1>&2 ; exit 255
else
ext_8_bit=${1}
fi
@@ -298,17 +299,23 @@ rgb_color()
{
color=$1
case $1 in
[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]) color=${1:0:1}${1:0:1}${1:1:1}${1:1:1}${1:2:1}${1:2:1} ;;
[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F])
tmp="${color}"
color="${tmp%??*}${tmp%??*}"
tmp="${tmp#?}"
color="${color}${tmp%?*}${tmp%?*}${tmp#?}${tmp#?}"
;;
esac
# First split off the red, green, and blue components...
red_hex=${color:0:2}
green_hex=${color:2:2}
blue_hex=${color:4:2}
red_hex="0x${color%????}"
color="${color#??}"
green_hex="0x${color%??}"
blue_hex="0x${color#??}"
# Convert to decimal...
red_dec=$((16#${red_hex}))
green_dec=$((16#${green_hex}))
blue_dec=$((16#${blue_hex}))
red_dec=$((${red_hex}))
green_dec=$((${green_hex}))
blue_dec=$((${blue_hex}))
if [ ${debug} -ne 0 ]
then
@@ -468,7 +475,7 @@ named_color()
debug_print "${name} -> ${mapped}"
if [ -z "${mapped}" ]
then
echo "ERROR!" 1>&2 ; exit -1
echo "ERROR!" 1>&2 ; exit 255
fi
rgb_color ${mapped}
@@ -506,8 +513,8 @@ build_sgr_code()
fg:ext:rgb*) make_color 0 ext_rgb ${1#??????????} ;;
fg:ext:*) make_color 0 ext_color ${1#???????} ;;
# 12-bit color also supported
fg:[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]) make_color 0 rgb_color ${1:???} ;;
fg:[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]) make_color 0 rgb_color ${1:???} ;;
fg:[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]) make_color 0 rgb_color ${1#???} ;;
fg:[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]) make_color 0 rgb_color ${1#???} ;;
bg:ansi:*) make_color 1 ansi_color ${1#????????} ;;
bg:ext:grey*) make_color 1 ext_grey ${1#???????????} ;;
@@ -517,6 +524,8 @@ build_sgr_code()
bg:[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]) make_color 1 rgb_color ${1#???} ;;
bg:[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]) make_color 1 rgb_color ${1#???} ;;
ul:ext:grey*) make_color 2 ext_grey ${1#???????????} ;;
ul:ext:rgb*) make_color 2 ext_rgb ${1#??????????} ;;
ul:ext:*) make_color 2 ext_color ${1#???????} ;;
# 12-bit color also supported
ul:[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]) make_color 2 rgb_color ${1#???} ;;