Skip to content

Instantly share code, notes, and snippets.

@izabera
Last active January 2, 2025 12:06
Show Gist options
  • Save izabera/3d1e5dfabbe80b3f5f2e50ec6f56eadb to your computer and use it in GitHub Desktop.
Save izabera/3d1e5dfabbe80b3f5f2e50ec6f56eadb to your computer and use it in GitHub Desktop.
query certain useful terminal attributes
#!/bin/bash
printf %b%.b \
'\e7' 'save cursor position' \
'\e[9999;9999H' 'move to bottom right' \
'\e[6n' 'get cursor position' \
'\e8' 'restore cursor' \
'\e[?u' 'kitty kbd proto' \
'\e[?2026$p' 'synchronised output' \
'\e[m' 'reset colours' \
'\e[38;5;123m' '256 colour fg' \
'\e[38;2;45;67;89m' 'truecolor fg' \
'\eP$qm\x1b\\' 'decrqm' \
'\e[c' 'da1' \
'\e[m' 'reset colours again'
read -rsdc
IFS=$'\e[;'
size=(${REPLY%%R*})
LINES=${size[2]} COLUMNS=${size[3]}
! [[ $REPLY = *u* ]]; kitty=$?
! [[ $REPLY = *'2026;2'* ]]; sync=$?
# for some reason some terminals (xterm, ghostty, vte-based ones) reply 38:2:: even if they don't support any extra fields
# contour just replies 382456789;49m which is honestly fucked up
! [[ $COLORTERM = *@(24bit|truecolor)* || $REPLY = *38*2*45*67*89*m* ]]; truecolor=$?
if [[ $DISPLAY ]]; then
IFS=' ' read -r _ _ _ _ self _ < <(xprop -root _NET_ACTIVE_WINDOW)
# most terminals also set an instance-specific wm class, but rio doesn't, so use the generic one
IFS='"' read -r _ _ _ class _ < <(xprop -id "$self" WM_CLASS)
else
class=unknown
fi
tf=(false true)
gb=(bad good)
info () { printf '%s: \e[38;5;51m%s\e[m\e[K\n' "$@"; }
good () { printf '%s: \e[38;5;46m%s\e[m\e[K\n' "$@"; }
bad () { printf '%s: \e[38;5;196m%s\e[m\e[K\n' "$@"; }
info '$TERM' "$TERM"
info 'wm class' "$class"
info 'size' "${COLUMNS}x$LINES"
info '$COLORTERM' "$COLORTERM"
info 'raw reply' "${REPLY@Q}"
"${gb[kitty]}" 'kitty keyboard proto support' "${tf[kitty]}"
"${gb[sync]}" 'synchronised output support' "${tf[sync]}"
"${gb[truecolor]}" '24bit colour support' "${tf[truecolor]}"
# r | g | b | rdx | gdx | bdx
# ------------+-----------+-----------+-----+-----+----
# max | 0->max | 0 | 0 | 1 | 0
# max->0 | max | 0 | -1 | 0 | 0
# 0 | max | 0->max | 0 | 0 | 1
# 0 | max->0 | max | 0 | -1 | 0
# 0->max | 0 | max | 1 | 0 | 0
# max | 0 | max->0 | 0 | 0 | -1
# walk around an rgb cube on the edges that don't include black or white
walkcube () {
local max=$(($1-1)) fmt=$2
local rdx=4 gdx=2 bdx=0
local r=max g=0 b=0
local i j
for (( i = 0; i < 6; i++ )) do
for (( j = 0; j < max; j++ )) do
printf -v 'hues[i*max+j]' "$fmt" \
"$(( r += (rdx%6==2)-(rdx%6==5) ))" \
"$(( g += (gdx%6==2)-(gdx%6==5) ))" \
"$(( b += (bdx%6==2)-(bdx%6==5) ))"
done
(( rdx = (rdx+1) % 6, gdx = (gdx+1) % 6, bdx = (bdx+1) % 6 ))
done
}
declare -ai hues
walkcube 6 '16 + %d*6*6 + %d*6 + %d'
printf '\e[38;5;%s;48;5;%sm▌' "${hues[@]}"
printf '\e[m 256 colour test\n'
unset hues
walkcube 256 '%d;%d;%d'
h=${#hues[@]}
for (( i = 0; i < h; i++ )) do
(( i % (h/COLUMNS) )) && unset 'hues[i]'
done
printf '\e[38;2;%s;48;2;%sm▌' "${hues[@]}"
printf '\e[m 24bit colour test\n'
@izabera
Copy link
Author

izabera commented Jan 2, 2025

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment