From af5327d088be290798b3c08a03916a1cd3bdc69f Mon Sep 17 00:00:00 2001 From: ADAM David Alan Martin Date: Wed, 23 Jul 2025 03:27:17 -0400 Subject: [PATCH] I've got a basic button selector working. I have to add actions for when "pressed", but for my current project I'm aiming to have a colour selector. I'll draw the current colour in a nice little box. I'll also eventually permit dynamic updates to specific colours in the table. This will permit me to dynamically adjust the colour space. --- bin/vulgar.sh | 220 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 218 insertions(+), 2 deletions(-) diff --git a/bin/vulgar.sh b/bin/vulgar.sh index 71a78d5..bba4d49 100755 --- a/bin/vulgar.sh +++ b/bin/vulgar.sh @@ -37,11 +37,36 @@ send_csi_command() echo -n "${_suffix}" } +##### START DECCRA Block ############ + +# These two functions expect DECCRA (DEC Copy Rectangular Area) +# to work. It also requires a second page to copy to. +# I haven't gotten them working, it seems. + +save_block() +{ + send_csi_command '$v' $* 0 $* 1 +} + +restore_block() +{ + send_csi_command '$v' $* 1 $* 0 +} + +##### END DECCRA Block ############## + erase_block() { + send_csi_command '*x' 2 + send_csi_command '$r' ${*} 0 send_csi_command '${' ${*} } +fill_block_attributes() +{ + send_csi_command '$r' ${*} +} + fill_block_raw() { send_csi_command '$x' ${*} @@ -114,9 +139,200 @@ build_box() draw_box $* } + +# Read a single keypress in raw terminal mode. +# It will read all codes from the input. +# +# Due to read timeouts, it may give odd +# results on non-interactive terminals. +read_key() +{ + seq="" + read -sn 1 key + code=`printf "%x" "'$key"` + seq=${code} + while read -t 0 + do + read -sn 1 key + code=`printf "%x" "'$key"` + seq="${seq} ${code}" + done + + eval $1=\"${seq}\" +} + +translate_key() +{ + _codes=${1} + case "${codes}" in + "1b 5b 41") + echo "Up" + ;; + + "1b 5b 42") + echo "Down" + ;; + + "1b 5b 44") + echo "Left" + ;; + + "1b 5b 43") + echo "Right" + ;; + + "1b") + echo "Escape" + ;; + + *) + echo "${codes}" + ;; + esac +} + + + #erase_block 20 20 30 30 #fill_block X 21 21 29 29 #write_char Y 25 25 -build_box 8 24 19 99 -write_string "Hello World" 10 26 +#build_box 8 24 19 99 +#write_string "Hello World" 10 26 + +#read -sn 1 keyA +#read -sn 1 keyB +#read -sn 1 keyC + +declare -A buttons + +create_button() +{ + _name=$1 + _r=$2 + _c=$3 + _text=$4 + _color=$5 + + shift 1 + eval button_${_name}=\"$*\" + + _T=$_r + _B=$(( $_r + 2 )) + + _L=$_c + _R=$(( $_c + 3 + ${#_text} )) + + erase_block $_T $_L $_B $_R + build_box $_T $_L $_B $_R + write_string "$_text" $(( $_T + 1 )) $(( $_L + 2 )) + if [ -n "$_color" ] + then + fill_block_attributes $_T $_L $_B $_R "3${_color}" + fi +} + +button_stat() +{ + _r=$1 + _c=$2 + _text=$3 + _color=$4 +} + + +highlight_button() +{ + _name=$1 + eval _stat="\${button_${_name}}" + button_stat ${_stat} + + _T=$_r + _B=$(( $_r + 2 )) + + _L=$_c + _R=$(( $_c + 3 + ${#_text} )) + + fill_block_attributes $_T $_L $_B $_R 0 + if [ -n "$_color" ] + then + fill_block_attributes $_T $_L $_B $_R "4${_color}" + else + fill_block_attributes $_T $_L $_B $_R '7' + fi +} + +unhighlight_button() +{ + _name=$1 + eval _stat="\${button_${_name}}" + button_stat ${_stat} + + _T=$_r + _B=$(( $_r + 2 )) + + _L=$_c + _R=$(( $_c + 3 + ${#_text} )) + + + fill_block_attributes $_T $_L $_B $_R 0 + if [ -n "$_color" ] + then + fill_block_attributes $_T $_L $_B $_R "3${_color}" + fi +} + + + + + +erase_block 11 1 15 32 + +create_button Red 12 2 Red 1 +highlight_button Red +#build_box 12 2 14 8 +#write_string "Red" 13 4 +#fill_block_attributes 12 2 14 8 41 + +create_button Green 12 12 Green 2 +#build_box 12 12 14 20 +#write_string "Green" 13 14 + +create_button Blue 12 24 Blue 4 +#build_box 12 24 14 31 +#write_string "Blue" 13 26 + +Buttons=("Red" "Green" "Blue") +Index=0 + +while true +do + read_key codes + sym=`translate_key ${codes}` + + OldIndex=${Index} + + if [ "${sym}" == "Right" ] + then + if (( ${Index} < $(( ${#Buttons} - 1 )) )) + then + Index=$(( ${Index} + 1 )) + + unhighlight_button ${Buttons[${OldIndex}]} + highlight_button ${Buttons[${Index}]} + fi + elif [ "${sym}" == "Left" ] + then + if (( ${Index} > 0 )) + then + Index=$(( ${Index} - 1 )) + + unhighlight_button ${Buttons[${OldIndex}]} + highlight_button ${Buttons[${Index}]} + fi + elif [ "${sym}" == "Escape" ] + then + #erase_block 11 1 15 32 + break + fi +done