Files
dotfiles/config/scripts/.local/scripts/sixel-query
T
2026-06-19 11:21:31 +02:00

48 lines
820 B
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
# Ensure in a interactive terminal
[ ! -t 0 ] && exit 1
# Construct query
QUERY_CODE=$(printf "\033[c")
# Set terminal to raw mode with timeout as 0.5s
stty_orig=$(stty -g < /dev/tty)
trap 'stty "$stty_orig" < /dev/tty' EXIT INT TERM HUP
stty -echo -icanon min 1 time 0
printf "%s" "$QUERY_CODE" >/dev/tty
response=""
while true; do
IFS= read -r -N 1 -t 0.3 char < /dev/tty || {
[ -z "$char" ] && break
}
response+="$char"
if [[ "$char" = "c" ]]; then
break
fi
if [ ${#response} -gt 1024 ]; then
break
fi
done
if [[ "$response" =~ $'\x1b'\[\?([0-9;]*)c ]]; then
params="${BASH_REMATCH[1]}"
IFS=';' read -ra codes <<< "$params"
for code in "${codes[@]}"; do
if [[ "$code" == "4" ]]; then
exit 0
fi
done
fi
exit 1