Skip to content

Instantly share code, notes, and snippets.

@tmccombs
Last active November 25, 2024 08:25
Show Gist options
  • Save tmccombs/d5b0cc52508769c6b30ebe240008bbff to your computer and use it in GitHub Desktop.
Save tmccombs/d5b0cc52508769c6b30ebe240008bbff to your computer and use it in GitHub Desktop.
Script to pretty-print the codes received for the kitty keyboard protocol
#!/usr/bin/env python3
'''
Program to Show what codes are sent by key presses using the kitty protocol
The flags for progressive-enhancement can be passed as an integer argument (hex is allowed).
Defaults to 1
'''
import sys
import termios
import tty
if len(sys.argv) >= 2:
# get the mode to use from the first argument
if sys.argv[1] == 'n':
flags = None
else:
flags = int(sys.argv[1], 0)
else:
flags = 1
buffer = bytearray(1)
def encoded(b):
'Encode a byte as printable text'
if 32 <= b < 127 or b == 0x0A:
return chr(b)
elif b == 0x1B:
return '\\e'
else:
return '\\{:02x}'.format(b)
def echo_next():
'Read the next byte from input, and print a representation of it to stdout'
if sys.stdin.buffer.readinto(buffer) != 1:
raise EOFError()
fwrite(encoded(buffer[0]))
if buffer == b'Q':
raise EOFError()
def fwrite(s):
'Write some output to stdout and flush'
print(s, end='', flush=True)
old_attrs = tty.setcbreak(sys.stdin)
try:
# Tell the terminal to use kitty protocol
# TODO: support using other modes?
if flags is not None:
fwrite(f'\x1B[>{flags}u')
while True:
# TODO: terminate somehow
echo_next()
except EOFError:
pass
finally:
termios.tcsetattr(sys.stdin, termios.TCSAFLUSH, old_attrs)
if flags is not None:
print('\x1B[<u', flush=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment