Implement progressive enhancement of key event reporting

This commit is contained in:
Kovid Goyal
2021-01-11 17:21:07 +05:30
parent 295e8db04c
commit a30ea2b7f8
8 changed files with 227 additions and 93 deletions

View File

@@ -2,82 +2,11 @@
# vim:fileencoding=utf-8
# License: GPL v3 Copyright: 2016, Kovid Goyal <kovid at kovidgoyal.net>
from functools import partial
import kitty.fast_data_types as defines
from kitty.keys import (
interpret_key_event, modify_complex_key, modify_key_bytes, smkx_key_map
)
from . import BaseTest
class DummyWindow:
def __init__(self):
self.screen = self
self.extended_keyboard = False
self.cursor_key_mode = True
class TestParser(BaseTest):
def test_modify_complex_key(self):
self.ae(modify_complex_key('kcuu1', 4), b'\033[1;4A')
self.ae(modify_complex_key('kcuu1', 3), b'\033[1;3A')
self.ae(modify_complex_key('kf5', 3), b'\033[15;3~')
self.assertRaises(ValueError, modify_complex_key, 'kri', 3)
def test_interpret_key_event(self):
# test rmkx/smkx
w = DummyWindow()
def k(expected, key, mods=0):
actual = interpret_key_event(
getattr(defines, 'GLFW_KEY_' + key),
0,
mods,
w,
defines.GLFW_PRESS,
)
self.ae(b'\033' + expected.encode('ascii'), actual)
for ckm, mch in {True: 'O', False: '['}.items():
w.cursor_key_mode = ckm
for name, ch in {
'UP': 'A',
'DOWN': 'B',
'RIGHT': 'C',
'LEFT': 'D',
'HOME': 'H',
'END': 'F',
}.items():
k(mch + ch, name)
w.cursor_key_mode = True
# test remaining special keys
for key, num in zip('INSERT DELETE PAGE_UP PAGE_DOWN'.split(), '2356'):
k('[' + num + '~', key)
for key, num in zip('1234', 'PQRS'):
k('O' + num, 'F' + key)
for key, num in zip(range(5, 13), (15, 17, 18, 19, 20, 21, 23, 24)):
k('[' + str(num) + '~', 'F{}'.format(key))
# test modifiers
SPECIAL_KEYS = 'UP DOWN RIGHT LEFT HOME END INSERT DELETE PAGE_UP PAGE_DOWN '
for i in range(1, 13):
SPECIAL_KEYS += 'F{} '.format(i)
SPECIAL_KEYS = SPECIAL_KEYS.strip().split()
for mods, num in zip(('CONTROL', 'ALT', 'SHIFT+ALT'), '534'):
fmods = 0
num = int(num)
for m in mods.split('+'):
fmods |= getattr(defines, 'GLFW_MOD_' + m)
km = partial(k, mods=fmods)
for key in SPECIAL_KEYS:
keycode = getattr(defines, 'GLFW_KEY_' + key)
base_key = smkx_key_map[keycode]
km(modify_key_bytes(base_key, num).decode('ascii')[1:], key)
class TestKeys(BaseTest):
def test_encode_mouse_event(self):
NORMAL_PROTOCOL, UTF8_PROTOCOL, SGR_PROTOCOL, URXVT_PROTOCOL = range(4)

View File

@@ -728,6 +728,49 @@ class TestScreen(BaseTest):
self.ae(str(s.linebuf), '0\n5\n6\n7\n\n')
self.ae(str(s.historybuf), '')
def test_key_encoding_flags_stack(self):
s = self.create_screen()
c = s.callbacks
def w(code, p1='', p2=''):
p = f'{p1}'
if p2:
p += f';{p2}'
return parse_bytes(s, f'\033[{code}{p}u'.encode('ascii'))
def ac(flags):
parse_bytes(s, '\033[?u'.encode('ascii'))
self.ae(c.wtcbuf, f'\033[?{flags}u'.encode('ascii'))
c.clear()
ac(0)
w('=', 0b1001)
ac(0b1001)
w('=', 0b0011, 2)
ac(0b1011)
w('=', 0b0110, 3)
ac(0b1001)
s.reset()
ac(0)
w('>', 0b0011)
ac(0b0011)
w('=', 0b1111)
ac(0b1111)
w('>', 0b10)
ac(0b10)
w('<')
ac(0b1111)
for i in range(10):
w('<')
ac(0)
s.reset()
for i in range(1, 16):
w('>', i)
ac(15)
w('<'), ac(14), w('<'), ac(13)
def test_color_stack(self):
s = self.create_screen()
c = s.callbacks