update k(itty-t)gp-query script

This commit is contained in:
2026-02-06 13:20:18 +01:00
parent f3a6bd1ddb
commit 53b33f6422
4 changed files with 55 additions and 91 deletions
+1 -1
View File
@@ -1,7 +1,7 @@
screenshot-path "~/Pictures/Screenshots/niri_screenshot_%Y-%m-%d_%H-%M-%S.png"
debug {
render-drm-device "/dev/dri/renderD128"
render-drm-device "/dev/dri/renderD129"
}
// gestures {
+52
View File
@@ -0,0 +1,52 @@
#!/usr/bin/env bash
# Ref: https://sw.kovidgoyal.net/kitty/graphics-protocol/#querying-support-and-available-transmission-mediums
set -euo pipefail
# Ensure in a interactive terminal
[ ! -t 0 ] && exit 1
# Construct query
QUERY_ID=$RANDOM
QUERY_CODE=$(printf "\033_Gi=%d,s=1,v=1,a=q,t=d,f=24;AAAA\033\\" "$QUERY_ID")
EXPECTED_RESPONSE=$(printf "\033_Gi=%d;OK\033\\" "$QUERY_ID")
# 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 with timeout as 0.5s
stty_orig=$(stty -g)
trap 'stty "$stty_orig"' EXIT
stty -echo -icanon min 0 time 5
printf "%s%s" "$QUERY_CODE" "$FENCE_CODE" > /dev/tty
response=""
ret=1
while true; do
char=$(dd bs=1 count=1 2>/dev/null) || break
if [ -z "$char" ]; then
break
fi
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
@@ -1,88 +0,0 @@
#!/usr/bin/env python3
import re
import sys
import os
import termios
from select import select
from time import time_ns
QUERY_ID = time_ns() & 0xFFFFFF
QUERY_CODE = f"\033_Gi={QUERY_ID},s=1,v=1,a=q,t=d,f=24;AAAA\033\\"
EXPECTED_RESPONSE = f"\033_Gi={QUERY_ID};OK\033\\"
# Device Attributes request that most terminals respond to
FENCE_CODE = "\033[c"
FENCE_PATTERN = re.compile(r"\033\[\?.*?c")
TIMEOUT = 0.05 # seconds
# Terminals that are known to support
SUPPORTED_TERMINALS = {
"xterm-kitty",
"xterm-ghostty"
}
# and those who don't
UNSUPPORTED_TERMINALS = {
"alacritty",
"linux",
"kmscon"
}
# the rest will be tested dynamically
def check_terminal_graphics():
if os.environ.get("TERM") in UNSUPPORTED_TERMINALS:
return False
if os.environ.get("TERM") in SUPPORTED_TERMINALS:
return True
code = QUERY_CODE + FENCE_CODE
fd = sys.stdin.fileno()
if not os.isatty(fd):
return False
old_settings = termios.tcgetattr(fd)
response = ""
try:
new_settings = termios.tcgetattr(fd)
# Disable canonical mode and echo
new_settings[3] = new_settings[3] & ~termios.ICANON & ~termios.ECHO
termios.tcsetattr(fd, termios.TCSANOW, new_settings)
sys.stdout.write(code)
sys.stdout.flush()
while True:
# Set a timeout to prevent blocking indefinitely
r, w, e = select([fd], [], [], TIMEOUT)
if not r:
break
char = os.read(fd, 1)
if not char:
break
response += char.decode('utf-8', errors='ignore')
# If received expected response, return True
if EXPECTED_RESPONSE in response:
return True
# If received fence response before any expected response, return False
if FENCE_PATTERN.search(response):
return False
except Exception:
pass
finally:
termios.tcsetattr(fd, termios.TCSANOW, old_settings)
return False
if __name__ == "__main__":
if check_terminal_graphics():
sys.exit(0)
else:
sys.exit(1)
@@ -1,7 +1,7 @@
if not set -q fetch_logo_type
if type -q kitty-tgp-query; and kitty-tgp-query
if type -q kgp-query; and kgp-query
set -g fetch_logo_type kitty
else if type -q kitty-tgp-query
else if type -q kgp-query
set -g fetch_logo_type logo
else
set -g fetch_logo_type auto