Now __build_sgr_code supports a simple language
This makes it easy in configs to build control codes for colour, etc.
This commit is contained in:
@ -13,57 +13,92 @@
|
|||||||
|
|
||||||
debug=0
|
debug=0
|
||||||
|
|
||||||
if [ ${1} == "fg" ]
|
nocsi=0
|
||||||
|
if [[ $1 == "no-csi" ]]
|
||||||
then
|
then
|
||||||
background=0
|
nocsi=1
|
||||||
elif [ ${1} == "bg" ]
|
shift 1
|
||||||
then
|
|
||||||
background=1
|
|
||||||
else
|
|
||||||
exit -1
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
shift 1
|
function setup_color_depth()
|
||||||
|
{
|
||||||
use_8_bit=0
|
use_8_bit=0
|
||||||
use_4_bit=0
|
use_4_bit=0
|
||||||
use_3_bit=0
|
use_3_bit=0
|
||||||
|
|
||||||
skip_color=0
|
# Must only be set if terminal colors are reduced...
|
||||||
if [ $1 == "none" ]
|
if [[ -v CSHENV_TERMINAL_COLORS ]]
|
||||||
then
|
then
|
||||||
skip_color=1
|
if (( ${CSHENV_TERMINAL_COLORS} == 256 ))
|
||||||
elif [ $1 == "reset" ]
|
|
||||||
then
|
then
|
||||||
output=1
|
use_8_bit=1
|
||||||
elif [ ${1:0:5} == "ansi:" ]
|
elif (( ${CSHENV_TERMINAL_COLORS} == 16 ))
|
||||||
then
|
then
|
||||||
selection=${1:5}
|
use_4_bit=1
|
||||||
if (( ${selection} > 15 ))
|
elif (( ${CSHENV_TERMINAL_COLORS} == 8 ))
|
||||||
|
then
|
||||||
|
use_3_bit=1
|
||||||
|
else
|
||||||
|
exit -1
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
function render_color()
|
||||||
|
{
|
||||||
|
command_color=30
|
||||||
|
if (( ${use_3_bit} ))
|
||||||
|
then
|
||||||
|
basecolor=$(( ${command_color} ))
|
||||||
|
next="$(( ${basecolor} + ${legacy_3_bit} ))"
|
||||||
|
elif (( ${use_4_bit} ))
|
||||||
|
then
|
||||||
|
command_color=$(( ${command_color} + ${intensity_1_bit} * 60 ))
|
||||||
|
basecolor=$(( ${command_color} + ${background}*10 ))
|
||||||
|
next="$(( ${basecolor} + ${legacy_3_bit} ))"
|
||||||
|
elif (( ${use_8_bit} ))
|
||||||
|
then
|
||||||
|
basecolor=$(( 8 + ${command_color} + ${background}*10 ))
|
||||||
|
next="${basecolor};5;${ext_8_bit}"
|
||||||
|
else
|
||||||
|
basecolor=$(( 8 + ${command_color} + ${background}*10 ))
|
||||||
|
next="${basecolor};2;${red_dec};${green_dec};${blue_dec}"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
function ansi_color()
|
||||||
|
{
|
||||||
|
if (( ${1} > 15 ))
|
||||||
then
|
then
|
||||||
exit -1
|
exit -1
|
||||||
elif (( ${selection} >= 8 ))
|
elif (( ${1} >= 8 ))
|
||||||
then
|
then
|
||||||
use_4_bit=1
|
use_4_bit=1
|
||||||
intensity_1_bit=1
|
intensity_1_bit=1
|
||||||
legacy_3_bit=$(( ${selection} - 8 ))
|
legacy_3_bit=$(( ${1} - 8 ))
|
||||||
else
|
else
|
||||||
use_3_bit=1
|
use_3_bit=1
|
||||||
intensity_1_bit=0
|
intensity_1_bit=0
|
||||||
legacy_3_bit=$(( ${selection} ))
|
legacy_3_bit=$(( ${1} ))
|
||||||
fi
|
fi
|
||||||
elif [ ${1:0:4} == "ext:" ]
|
}
|
||||||
then
|
|
||||||
|
function ext_color()
|
||||||
|
{
|
||||||
use_8_bit=1
|
use_8_bit=1
|
||||||
selection=${1:4}
|
selection=${1:4}
|
||||||
if (( ${selection} > 255 ))
|
if (( ${1} > 255 ))
|
||||||
then
|
then
|
||||||
exit -1
|
exit -1
|
||||||
else
|
else
|
||||||
ext_8_bit=${selection}
|
ext_8_bit=${1}
|
||||||
fi
|
fi
|
||||||
else # Parse the hex and do the thing...
|
}
|
||||||
|
|
||||||
|
function rgb_color()
|
||||||
|
{
|
||||||
# First split off the red, green, and blue components...
|
# First split off the red, green, and blue components...
|
||||||
red_hex=${1:0:2}
|
red_hex=${1:0:2}
|
||||||
green_hex=${1:2:2}
|
green_hex=${1:2:2}
|
||||||
@ -187,56 +222,26 @@ else # Parse the hex and do the thing...
|
|||||||
# We do NOT check for the base colours precisely. Since those 16 can be custom mapped by your terminal emulator, you just pass `ansi:0` thru
|
# We do NOT check for the base colours precisely. Since those 16 can be custom mapped by your terminal emulator, you just pass `ansi:0` thru
|
||||||
# `ansi:15` to select them by ID. Or use `ext:0` thru `ext:255` to select a specific extended colour. Otherwise, we now generate the true-colour
|
# `ansi:15` to select them by ID. Or use `ext:0` thru `ext:255` to select a specific extended colour. Otherwise, we now generate the true-colour
|
||||||
# result:
|
# result:
|
||||||
fi
|
}
|
||||||
|
|
||||||
# Must only be set if terminal colors are reduced...
|
function make_color()
|
||||||
if [[ -v CSHENV_TERMINAL_COLORS ]]
|
{
|
||||||
then
|
setup_color_depth
|
||||||
if (( ${CSHENV_TERMINAL_COLORS} == 256 ))
|
background=$1 # $1 is the background bit
|
||||||
then
|
$2 $3 # $2 is the color function
|
||||||
use_8_bit=1
|
render_color
|
||||||
elif (( ${CSHENV_TERMINAL_COLORS} == 16 ))
|
}
|
||||||
then
|
|
||||||
use_4_bit=1
|
|
||||||
elif (( ${CSHENV_TERMINAL_COLORS} == 8 ))
|
|
||||||
then
|
|
||||||
use_3_bit=1
|
|
||||||
else
|
|
||||||
exit -1
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
|
|
||||||
output=""
|
output=""
|
||||||
|
if (( ! ${nocsi} ))
|
||||||
if (( ! ${skip_color} ))
|
|
||||||
then
|
then
|
||||||
command_color=30
|
output="["
|
||||||
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
|
|
||||||
output="0"
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
shift 1
|
|
||||||
while [[ ! -z $1 ]]
|
while [[ ! -z $1 ]]
|
||||||
do
|
do
|
||||||
case $1 in
|
case $1 in
|
||||||
|
reset) next=0 ;;
|
||||||
bold) next=1 ;;
|
bold) next=1 ;;
|
||||||
dim) next=2 ;;
|
dim) next=2 ;;
|
||||||
italic) next=3 ;;
|
italic) next=3 ;;
|
||||||
@ -248,7 +253,20 @@ do
|
|||||||
strike|strikethrough|strikethru) next=9 ;;
|
strike|strikethrough|strikethru) next=9 ;;
|
||||||
doubleunder) next=21 ;;
|
doubleunder) next=21 ;;
|
||||||
reveal) next=28 ;;
|
reveal) next=28 ;;
|
||||||
*) exit -1 ;;
|
|
||||||
|
ansi:*) make_color 0 ansi_color ${1:5} ;;
|
||||||
|
ext:*) make_color 0 ext_color ${1:4} ;;
|
||||||
|
[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:ansi:*) make_color 0 ansi_color ${1:8} ;;
|
||||||
|
fg:ext:*) make_color 0 ext_color ${1:7} ;;
|
||||||
|
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:3} ;;
|
||||||
|
|
||||||
|
bg:ansi:*) make_color 1 ansi_color ${1:8} ;;
|
||||||
|
bg:ext:*) make_color 1 ext_color ${1:7} ;;
|
||||||
|
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:3} ;;
|
||||||
|
|
||||||
|
*) echo "ERROR!" 1>&2 ; exit -1 ;;
|
||||||
esac
|
esac
|
||||||
if [[ ! -z ${output} ]]
|
if [[ ! -z ${output} ]]
|
||||||
then
|
then
|
||||||
@ -258,6 +276,11 @@ do
|
|||||||
shift 1
|
shift 1
|
||||||
done
|
done
|
||||||
|
|
||||||
|
if (( ! ${nocsi} ))
|
||||||
|
then
|
||||||
|
output="${output}m"
|
||||||
|
fi
|
||||||
|
|
||||||
printf ${output}
|
printf ${output}
|
||||||
|
|
||||||
exit 0
|
exit 0
|
||||||
Reference in New Issue
Block a user