From e34caae816d9dfe16f43ced884d77a4274e59341 Mon Sep 17 00:00:00 2001 From: ADAM David Alan Martin Date: Sat, 4 Jun 2022 03:32:56 -0400 Subject: [PATCH] Add support for other SGR tokens to the ansi code builder. --- bin/__hex_to_ansi | 81 +++++++++++++++++++++++++++++++++++++---------- 1 file changed, 65 insertions(+), 16 deletions(-) diff --git a/bin/__hex_to_ansi b/bin/__hex_to_ansi index f485483..3332d50 100755 --- a/bin/__hex_to_ansi +++ b/bin/__hex_to_ansi @@ -3,6 +3,13 @@ # 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 # its colour value. +# +# After that, we can map a few other special named symbols. The idea is to +# permit shell variables such as `aaffcc bold italic` to indicate what you +# want a format to look like. +# +# TODO: Update the script to allow for `bg:ff00ff` style descriptions, instead +# of positional. debug=0 @@ -22,7 +29,14 @@ use_8_bit=0 use_4_bit=0 use_3_bit=0 -if [ ${1:0:5} == "ansi:" ] +skip_color=0 +if [ $1 == "none" ] +then + skip_color=1 +elif [ $1 == "reset" ] +then + output=1 +elif [ ${1:0:5} == "ansi:" ] then selection=${1:5} if (( ${selection} > 15 )) @@ -192,23 +206,58 @@ then fi fi -command_color=30 -if (( ${use_3_bit} )) +output="" + +if (( ! ${skip_color} )) then - basecolor=$(( ${command_color} )) - printf $(( ${basecolor} + ${legacy_3_bit} )) -elif (( ${use_4_bit} )) + command_color=30 + if (( ${use_3_bit} )) + then + basecolor=$(( ${command_color} )) + output="$(( ${basecolor} + ${legacy_3_bit} ))" + elif (( ${use_4_bit} )) + then + command_color=$(( ${command_color} + ${intensity_1_bit} * 60 )) + basecolor=$(( ${command_color} + ${background}*10 )) + output="$(( ${basecolor} + ${legacy_3_bit} ))" + elif (( ${use_8_bit} )) + then + basecolor=$(( 8 + ${command_color} + ${background}*10 )) + output="${basecolor};5;${ext_8_bit}" + else + basecolor=$(( 8 + ${command_color} + ${background}*10 )) + output="${basecolor};2;${red_dec};${green_dec};${blue_dec}" + fi +elif (( ${reset_sgr} )) then - command_color=$(( ${command_color} + ${intensity_1_bit} * 60 )) - basecolor=$(( ${command_color} + ${background}*10 )) - printf $(( ${basecolor} + ${legacy_3_bit} )) -elif (( ${use_8_bit} )) -then - basecolor=$(( 8 + ${command_color} + ${background}*10 )) - printf "${basecolor};5;${ext_8_bit}" -else - basecolor=$(( 8 + ${command_color} + ${background}*10 )) - printf "${basecolor};2;${red_dec};${green_dec};${blue_dec}" + output="0" fi +shift 1 +while [[ ! -z $1 ]] +do + case $1 in + bold) next=1 ;; + dim) next=2 ;; + italic) next=3 ;; + underline|under) next=4 ;; + blink) next=5 ;; + fastblink) next=6 ;; + reverse) next=7 ;; + hide|conceal) next=8 ;; + strike|strikethrough|strikethru) next=9 ;; + doubleunder) next=21 ;; + reveal) next=28 ;; + *) exit -1 ;; + esac + if [[ ! -z ${output} ]] + then + output="${output};" + fi + output="${output}${next}" + shift 1 +done + +printf ${output} + exit 0