Refactor clipboard management and graphics query scripts

This commit is contained in:
2026-02-07 16:18:18 +01:00
parent 0863da0f47
commit 2a5b2fd9c2
12 changed files with 472 additions and 11 deletions
+47
View File
@@ -0,0 +1,47 @@
#!/usr/bin/env bash
set -euo pipefail
# Ensure in a interactive terminal
[ ! -t 0 ] && exit 1
# Construct query
QUERY_CODE=$(printf "\033]1337;ReportCellSize\a")
EXPECTED_RESPONSE=$(printf "\033]1337;") # followed by "ReportCellSize=...", but only the prefix is enough
# Construct fence code that most terminals will respond to
# to ensure that at least something will be sent back and
# to detect the end of the response
FENCE_CODE=$(printf "\033[c")
# Set terminal to raw mode
stty_orig=$(stty -g)
trap 'stty "$stty_orig"' EXIT
stty -echo -icanon min 1 time 0
printf "%s%s" "$QUERY_CODE" "$FENCE_CODE" > /dev/tty
response=""
ret=1
while true; do
IFS= read -r -N 1 -t 0.3 char || {
[ -z "$char" ] && break
}
response+="$char"
if [[ "$response" == *"$EXPECTED_RESPONSE"* ]]; then
ret=0
# Keep reading until response is complete to
# avoid leaving unread data in the terminal
fi
if [[ "$response" == *$'\033['*'c' ]]; then
break
fi
if [ ${#response} -gt 1024 ]; then
break
fi
done
exit $ret