parser.py 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  1. #!/usr/bin/env python
  2. # License: GPL v3 Copyright: 2016, Kovid Goyal <kovid at kovidgoyal.net>
  3. from binascii import hexlify
  4. from functools import partial
  5. from kitty.fast_data_types import (
  6. CURSOR_BLOCK,
  7. VT_PARSER_BUFFER_SIZE,
  8. base64_decode,
  9. base64_encode,
  10. has_avx2,
  11. has_sse4_2,
  12. test_find_either_of_two_bytes,
  13. test_utf8_decode_to_sentinel,
  14. )
  15. from . import BaseTest, parse_bytes
  16. def cnv(x):
  17. if isinstance(x, memoryview):
  18. x = str(x, 'utf-8')
  19. return x
  20. class CmdDump(list):
  21. def __call__(self, window_id, *a):
  22. if a and a[0] == 'bytes':
  23. return
  24. if a and a[0] == 'error':
  25. a = a[1:]
  26. self.append(tuple(map(cnv, a)))
  27. def get_result(self):
  28. current = ''
  29. q = []
  30. for args in self:
  31. if args[0] == 'draw':
  32. current += args[1]
  33. else:
  34. if current:
  35. q.append(('draw', current))
  36. current = ''
  37. q.append(args)
  38. if current:
  39. q.append(('draw', current))
  40. return tuple(q)
  41. class TestParser(BaseTest):
  42. def create_write_buffer(self, screen):
  43. return screen.test_create_write_buffer()
  44. def write_bytes(self, screen, write_buf, data):
  45. if isinstance(data, str):
  46. data = data.encode('utf-8')
  47. s = screen.test_commit_write_buffer(data, write_buf)
  48. return data[s:]
  49. def parse_written_data(self, screen, *cmds):
  50. cd = CmdDump()
  51. screen.test_parse_written_data(cd)
  52. cmds = tuple(('draw', x) if isinstance(x, str) else tuple(map(cnv, x)) for x in cmds)
  53. self.ae(cmds, cd.get_result())
  54. def parse_bytes_dump(self, s, x, *cmds):
  55. cd = CmdDump()
  56. if isinstance(x, str):
  57. x = x.encode('utf-8')
  58. cmds = tuple(('draw', x) if isinstance(x, str) else tuple(map(cnv, x)) for x in cmds)
  59. parse_bytes(s, x, cd)
  60. self.ae(cmds, cd.get_result())
  61. def test_charsets(self):
  62. s = self.create_screen()
  63. pb = partial(self.parse_bytes_dump, s)
  64. pb(b'\xc3')
  65. pb(b'\xa1', ('draw', b'\xc3\xa1'.decode('utf-8')))
  66. s = self.create_screen()
  67. pb = partial(self.parse_bytes_dump, s)
  68. pb('\033)0\x0e/_', ('screen_designate_charset', 1, ord('0')), ('screen_change_charset', 1), '/_')
  69. self.ae(str(s.line(0)), '/\xa0')
  70. s = self.create_screen()
  71. pb = partial(self.parse_bytes_dump, s)
  72. pb('\033(0/_', ('screen_designate_charset', 0, ord('0')), '/_')
  73. self.ae(str(s.line(0)), '/\xa0')
  74. def test_parser_threading(self):
  75. s = self.create_screen()
  76. self.assertFalse(self.write_bytes(s, self.create_write_buffer(s), 'a\x1b]2;some title'))
  77. b = self.create_write_buffer(s)
  78. self.parse_written_data(s, 'a')
  79. self.assertFalse(self.write_bytes(s, b, ' full\x1b\\'))
  80. self.parse_written_data(s, ('set_title', 'some title full'))
  81. self.assertFalse(self.write_bytes(s, self.create_write_buffer(s), 'a\x1b]'))
  82. b = self.create_write_buffer(s)
  83. self.parse_written_data(s, 'a')
  84. self.assertFalse(self.write_bytes(s, b, '2;title\x1b\\'))
  85. self.parse_written_data(s, ('set_title', 'title'))
  86. self.assertFalse(self.write_bytes(s, self.create_write_buffer(s), 'a\x1b'))
  87. b = self.create_write_buffer(s)
  88. self.parse_written_data(s, 'a')
  89. self.assertFalse(self.write_bytes(s, b, ']2;title\x1b\\'))
  90. self.parse_written_data(s, ('set_title', 'title'))
  91. self.assertFalse(self.write_bytes(s, self.create_write_buffer(s), 'a\x1b]2;some title\x1b'))
  92. b = self.create_write_buffer(s)
  93. self.parse_written_data(s, 'a')
  94. self.assertFalse(self.write_bytes(s, b, '\\b'))
  95. self.parse_written_data(s, ('set_title', 'some title'), 'b')
  96. self.assertFalse(self.write_bytes(s, self.create_write_buffer(s), '1\x1b'))
  97. self.parse_written_data(s, '1')
  98. self.assertFalse(self.write_bytes(s, self.create_write_buffer(s), 'E2'))
  99. self.parse_written_data(s, ('screen_nel',), ('draw', '2'))
  100. self.assertFalse(self.write_bytes(s, self.create_write_buffer(s), '1\x1b[2'))
  101. self.parse_written_data(s, '1')
  102. self.assertFalse(self.write_bytes(s, self.create_write_buffer(s), '3mx'))
  103. self.parse_written_data(s, ('select_graphic_rendition', '23'), 'x')
  104. self.assertFalse(self.write_bytes(s, self.create_write_buffer(s), '1\x1b'))
  105. self.parse_written_data(s, '1')
  106. self.assertFalse(self.write_bytes(s, self.create_write_buffer(s), '[23mx'))
  107. self.parse_written_data(s, ('select_graphic_rendition', '23'), 'x')
  108. self.assertFalse(self.write_bytes(s, self.create_write_buffer(s), '1\x1b['))
  109. self.parse_written_data(s, '1')
  110. self.assertFalse(self.write_bytes(s, self.create_write_buffer(s), '23mx'))
  111. self.parse_written_data(s, ('select_graphic_rendition', '23'), 'x')
  112. # test full write
  113. sz = VT_PARSER_BUFFER_SIZE // 3 + 7
  114. self.assertFalse(self.write_bytes(s, self.create_write_buffer(s), b'a' * sz))
  115. self.assertFalse(self.write_bytes(s, self.create_write_buffer(s), b'b' * sz))
  116. left = self.write_bytes(s, self.create_write_buffer(s), b'c' * sz)
  117. self.assertTrue(len(left), 3 * sz - VT_PARSER_BUFFER_SIZE)
  118. self.assertFalse(self.create_write_buffer(s))
  119. s.test_parse_written_data()
  120. b = self.create_write_buffer(s)
  121. self.assertTrue(b)
  122. self.write_bytes(s, b, b'')
  123. def test_base64(self):
  124. for src, expected in {
  125. 'bGlnaHQgdw==': 'light w',
  126. 'bGlnaHQgd28=': 'light wo',
  127. 'bGlnaHQgd29y': 'light wor',
  128. }.items():
  129. self.ae(base64_decode(src.encode()), expected.encode(), f'Decoding of {src} failed')
  130. self.ae(base64_decode(src.replace('=', '').encode()), expected.encode(), f'Decoding of {src} failed')
  131. self.ae(base64_encode(expected.encode()), src.replace('=', '').encode(), f'Encoding of {expected} failed')
  132. def test_simple_parsing(self):
  133. s = self.create_screen()
  134. pb = partial(self.parse_bytes_dump, s)
  135. pb('12', '12')
  136. self.ae(str(s.line(0)), '12')
  137. self.ae(s.cursor.x, 2)
  138. pb('3456', '3456')
  139. self.ae(str(s.line(0)), '12345')
  140. self.ae(str(s.line(1)), '6')
  141. pb(b'\n123\n\r45', ('screen_linefeed',), '123', ('screen_linefeed',), ('screen_carriage_return',), '45')
  142. self.ae(str(s.line(1)), '6')
  143. self.ae(str(s.line(2)), ' 123')
  144. self.ae(str(s.line(3)), '45')
  145. pb(b'\rabcde', ('screen_carriage_return',), 'abcde')
  146. self.ae(str(s.line(3)), 'abcde')
  147. pb('\rßxyz1', ('screen_carriage_return',), 'ßxyz1')
  148. self.ae(str(s.line(3)), 'ßxyz1')
  149. pb('ニチ ', 'ニチ ')
  150. self.ae(str(s.line(4)), 'ニチ ')
  151. s.reset()
  152. self.assertFalse(str(s.line(1)) + str(s.line(2)) + str(s.line(3)))
  153. c1_controls = '\x84\x85\x88\x8d\x8e\x8f\x90\x96\x97\x98\x9a\x9b\x9c\x9d\x9e\x9f'
  154. pb(c1_controls, c1_controls)
  155. self.assertFalse(str(s.line(1)) + str(s.line(2)) + str(s.line(3)))
  156. pb('😀'.encode()[:-1])
  157. pb('\x1b\x1b%a', '\ufffd', ('Unknown char after ESC: 0x1b',), ('draw', '%a'))
  158. def test_utf8_parsing(self):
  159. s = self.create_screen()
  160. pb = partial(self.parse_bytes_dump, s)
  161. pb(b'"\xbf"', '"\ufffd"')
  162. pb(b'"\x80"', '"\ufffd"')
  163. pb(b'"\x80\xbf"', '"\ufffd\ufffd"')
  164. pb(b'"\x80\xbf\x80"', '"\ufffd\ufffd\ufffd"')
  165. pb(b'"\xc0 "', '"\ufffd "')
  166. pb(b'"\xfe"', '"\ufffd"')
  167. pb(b'"\xff"', '"\ufffd"')
  168. pb(b'"\xff\xfe"', '"\ufffd\ufffd"')
  169. pb(b'"\xfe\xfe\xff\xff"', '"\ufffd\ufffd\ufffd\ufffd"')
  170. pb(b'"\xef\xbf"', '"\ufffd"')
  171. pb(b'"\xe0\xa0"', '"\ufffd"')
  172. pb(b'"\xf0\x9f\x98"', '"\ufffd"')
  173. def test_utf8_simd_decode(self):
  174. def unsupported(which):
  175. return (which == 2 and not has_sse4_2) or (which == 3 and not has_avx2)
  176. def reset_state():
  177. test_utf8_decode_to_sentinel(b'', -1)
  178. def asbytes(x):
  179. if isinstance(x, str):
  180. x = x.encode()
  181. return x
  182. def t(*a, which=2):
  183. if unsupported(which):
  184. return
  185. def parse_parts(which):
  186. total_consumed = 0
  187. esc_found = False
  188. parts = []
  189. for x in a:
  190. found_sentinel, x, num_consumed = test_utf8_decode_to_sentinel(asbytes(x), which)
  191. total_consumed += num_consumed
  192. if found_sentinel:
  193. esc_found = found_sentinel
  194. parts.append(x)
  195. return esc_found, ''.join(parts), total_consumed
  196. reset_state()
  197. actual = parse_parts(1)
  198. reset_state()
  199. expected = parse_parts(which)
  200. self.ae(expected, actual, msg=f'Failed for {a} with {which=}\n{expected!r} !=\n{actual!r}')
  201. return actual
  202. def double_test(x):
  203. for which in (2, 3):
  204. t(x, which=which)
  205. t(x*2, which=3)
  206. reset_state()
  207. # incomplete trailer at end of vector
  208. t("a"*10 + "😸😸" + "b"*15)
  209. x = double_test
  210. x('2:α3')
  211. x('2:α\x1b3')
  212. x('2:α3:≤4:😸|')
  213. x('abcd1234efgh5678')
  214. x('abc\x1bd1234efgh5678')
  215. x('abcd1234efgh5678ijklABCDmnopEFGH')
  216. for which in (2, 3):
  217. x = partial(t, which=which)
  218. x('abcdef', 'ghijk')
  219. x('2:α3', ':≤4:😸|')
  220. # trailing incomplete sequence
  221. for prefix in (b'abcd', '😸'.encode()):
  222. for suffix in (b'1234', '😸'.encode()):
  223. x(prefix + b'\xf0\x9f', b'\x98\xb8' + suffix)
  224. x(prefix + b'\xf0\x9f\x9b', b'\xb8' + suffix)
  225. x(prefix + b'\xf0', b'\x9f\x98\xb8' + suffix)
  226. x(prefix + b'\xc3', b'\xa4' + suffix)
  227. x(prefix + b'\xe2', b'\x89\xa4' + suffix)
  228. x(prefix + b'\xe2\x89', b'\xa4' + suffix)
  229. def test_expected(src, expected, which=2):
  230. if unsupported(which):
  231. return
  232. reset_state()
  233. _, actual, _ = t(b'filler' + asbytes(src), which=which)
  234. expected = 'filler' + expected
  235. self.ae(expected, actual, f'Failed for: {src!r} with {which=}')
  236. for which in (1, 2, 3):
  237. pb = partial(test_expected, which=which)
  238. pb('ニチ', 'ニチ')
  239. pb('\x84\x85', '\x84\x85')
  240. pb('\x84\x85', '\x84\x85')
  241. pb('\uf4df', '\uf4df')
  242. pb('\uffff', '\uffff')
  243. pb('\0', '\0')
  244. pb(chr(0x10ffff), chr(0x10ffff))
  245. # various invalid input
  246. pb(b'abcd\xf51234', 'abcd\ufffd1234') # bytes > 0xf4
  247. pb(b'abcd\xff1234', 'abcd\ufffd1234') # bytes > 0xf4
  248. pb(b'"\xbf"', '"\ufffd"')
  249. pb(b'"\x80"', '"\ufffd"')
  250. pb(b'"\x80\xbf"', '"\ufffd\ufffd"')
  251. pb(b'"\x80\xbf\x80"', '"\ufffd\ufffd\ufffd"')
  252. pb(b'"\xc0 "', '"\ufffd "')
  253. pb(b'"\xfe"', '"\ufffd"')
  254. pb(b'"\xff"', '"\ufffd"')
  255. pb(b'"\xff\xfe"', '"\ufffd\ufffd"')
  256. pb(b'"\xfe\xfe\xff\xff"', '"\ufffd\ufffd\ufffd\ufffd"')
  257. pb(b'"\xef\xbf"', '"\ufffd"')
  258. pb(b'"\xe0\xa0"', '"\ufffd"')
  259. pb(b'"\xf0\x9f\x98"', '"\ufffd"')
  260. pb(b'"\xef\x93\x94\x95"', '"\uf4d4\ufffd"')
  261. def test_find_either_of_two_bytes(self):
  262. sizes = []
  263. if has_sse4_2:
  264. sizes.append(2)
  265. if has_avx2:
  266. sizes.append(3)
  267. sizes.append(0)
  268. def test(buf, a, b, align_offset=0):
  269. a_, b_ = ord(a), ord(b)
  270. expected = test_find_either_of_two_bytes(buf, a_, b_, 1, 0)
  271. for sz in sizes:
  272. actual = test_find_either_of_two_bytes(buf, a_, b_, sz, align_offset)
  273. self.ae(expected, actual, f'Failed for: {buf!r} {a=} {b=} at {sz=} and {align_offset=}')
  274. q = 'abc'
  275. for off in range(32):
  276. test(q, '<', '>', off)
  277. test(q, ' ', 'b', off)
  278. test(q, '<', 'a', off)
  279. test(q, '<', 'b', off)
  280. test(q, 'c', '>', off)
  281. def tests(buf, a, b):
  282. for sz in (0, 16, 32, 64, 79):
  283. buf = (' ' * sz) + buf
  284. for align_offset in range(32):
  285. test(buf, a, b, align_offset)
  286. tests("", '<', '>')
  287. tests("a", '\0', '\0')
  288. tests("a", '<', '>')
  289. tests("dsdfsfa", '1', 'a')
  290. tests("xa", 'a', 'a')
  291. tests("bbb", 'a', '1')
  292. tests("bba", 'a', '<')
  293. tests("baa", '>', 'a')
  294. def test_esc_codes(self):
  295. s = self.create_screen()
  296. pb = partial(self.parse_bytes_dump, s)
  297. pb('12\033Da', '12', ('screen_index',), 'a')
  298. self.ae(str(s.line(0)), '12')
  299. self.ae(str(s.line(1)), ' a')
  300. pb('\033xa', ('Unknown char after ESC: 0x%x' % ord('x'),), 'a')
  301. pb('\033c123', ('screen_reset', ), '123')
  302. self.ae(str(s.line(0)), '123')
  303. pb('\033.\033a', ('Unhandled charset related escape code: 0x2e 0x1b',), 'a')
  304. def test_csi_codes(self):
  305. s = self.create_screen()
  306. pb = partial(self.parse_bytes_dump, s)
  307. pb('abcde', 'abcde')
  308. s.cursor_back(5)
  309. pb('x\033[2@y', 'x', ('screen_insert_characters', 2), 'y')
  310. self.ae(str(s.line(0)), 'xy bc')
  311. pb('x\033[2;7@y', 'x', ('CSI code @ has 2 > 1 parameters',), 'y')
  312. pb('x\033[2;-7@y', 'x', ('CSI code @ has 2 > 1 parameters',), 'y')
  313. pb('x\033[-0001234567890@y', 'x', ('CSI code @ is not allowed to have negative parameter (-1234567890)',), 'y')
  314. pb('x\033[2-3@y', 'x', ('Invalid character in CSI: 3 (0x33), ignoring the sequence',), '@y')
  315. pb('x\033[@y', 'x', ('screen_insert_characters', 1), 'y')
  316. pb('x\033[345@y', 'x', ('screen_insert_characters', 345), 'y')
  317. pb('x\033[345;@y', 'x', ('screen_insert_characters', 345), 'y')
  318. pb('\033[H', ('screen_cursor_position', 1, 1))
  319. self.ae(s.cursor.x, 0), self.ae(s.cursor.y, 0)
  320. pb('\033[4H', ('screen_cursor_position', 4, 1))
  321. pb('\033[4;0H', ('screen_cursor_position', 4, 0))
  322. pb('\033[3;2H', ('screen_cursor_position', 3, 2))
  323. pb('\033[3;2;H', ('screen_cursor_position', 3, 2))
  324. pb('\033[00000000003;0000000000000002H', ('screen_cursor_position', 3, 2))
  325. self.ae(s.cursor.x, 1), self.ae(s.cursor.y, 2)
  326. pb('\033[0001234567890H', ('screen_cursor_position', 1234567890, 1))
  327. pb('\033[J', ('screen_erase_in_display', 0, 0))
  328. pb('\033[?J', ('screen_erase_in_display', 0, 1))
  329. pb('\033[?2J', ('screen_erase_in_display', 2, 1))
  330. pb('\033[h')
  331. pb('\033[20;4h', ('screen_set_mode', 20, 0), ('screen_set_mode', 4, 0))
  332. pb('\033[?1000;1004h', ('screen_set_mode', 1000, 1), ('screen_set_mode', 1004, 1))
  333. pb('\033[20;4;20l', ('screen_reset_mode', 20, 0), ('screen_reset_mode', 4, 0), ('screen_reset_mode', 20, 0))
  334. pb('\033[=c', ('report_device_attributes', 0, 61))
  335. s.reset()
  336. def sgr(*params):
  337. return (('select_graphic_rendition', f'{x}') for x in params)
  338. pb('\033[1;2;3;4;7;9;34;44m', *sgr('1 2 3 4 7 9 34 44'))
  339. for attr in 'bold italic reverse strikethrough dim'.split():
  340. self.assertTrue(getattr(s.cursor, attr), attr)
  341. self.ae(s.cursor.decoration, 1)
  342. self.ae(s.cursor.fg, 4 << 8 | 1)
  343. self.ae(s.cursor.bg, 4 << 8 | 1)
  344. pb('\033[38;5;1;48;5;7m', ('select_graphic_rendition', '38:5:1'), ('select_graphic_rendition', '48:5:7'))
  345. self.ae(s.cursor.fg, 1 << 8 | 1)
  346. self.ae(s.cursor.bg, 7 << 8 | 1)
  347. pb('\033[38;2;1;2;3;48;2;7;8;9m', ('select_graphic_rendition', '38:2:1:2:3'), ('select_graphic_rendition', '48:2:7:8:9'))
  348. self.ae(s.cursor.fg, 1 << 24 | 2 << 16 | 3 << 8 | 2)
  349. self.ae(s.cursor.bg, 7 << 24 | 8 << 16 | 9 << 8 | 2)
  350. pb('\033[0;2m', *sgr('0 2'))
  351. pb('\033[;2m', *sgr('0 2'))
  352. pb('\033[m', *sgr('0'))
  353. pb('\033[1;;2m', *sgr('1 0 2'))
  354. pb('\033[38;5;1m', ('select_graphic_rendition', '38:5:1'))
  355. pb('\033[58;2;1;2;3m', ('select_graphic_rendition', '58:2:1:2:3'))
  356. pb('\033[38;2;1;2;3m', ('select_graphic_rendition', '38:2:1:2:3'))
  357. pb('\033[1001:2:1:2:3m', ('select_graphic_rendition', '1001:2:1:2:3'))
  358. pb('\033[38:2:1:2:3;48:5:9;58;5;7m', (
  359. 'select_graphic_rendition', '38:2:1:2:3'), ('select_graphic_rendition', '48:5:9'), ('select_graphic_rendition', '58:5:7'))
  360. s.reset()
  361. pb('\033[1;2;3;4:5;7;9;34;44m', *sgr('1 2 3', '4:5', '7 9 34 44'))
  362. for attr in 'bold italic reverse strikethrough dim'.split():
  363. self.assertTrue(getattr(s.cursor, attr), attr)
  364. self.ae(s.cursor.decoration, 5)
  365. c = s.callbacks
  366. pb('\033[5n', ('report_device_status', 5, 0))
  367. self.ae(c.wtcbuf, b'\033[0n')
  368. c.clear()
  369. pb('\033[6n', ('report_device_status', 6, 0))
  370. self.ae(c.wtcbuf, b'\033[1;1R')
  371. pb('12345', '12345')
  372. c.clear()
  373. pb('\033[6n', ('report_device_status', 6, 0))
  374. self.ae(c.wtcbuf, b'\033[2;1R')
  375. c.clear()
  376. s.cursor_key_mode = True
  377. pb('\033[?1$p', ('report_mode_status', 1, 1))
  378. self.ae(c.wtcbuf, b'\033[?1;1$y')
  379. pb('\033[?1l', ('screen_reset_mode', 1, 1))
  380. self.assertFalse(s.cursor_key_mode)
  381. c.clear()
  382. pb('\033[?1$p', ('report_mode_status', 1, 1))
  383. self.ae(c.wtcbuf, b'\033[?1;2$y')
  384. pb('\033[2;4r', ('screen_set_margins', 2, 4))
  385. c.clear()
  386. pb('\033[14t', ('screen_report_size', 14))
  387. self.ae(c.wtcbuf, b'\033[4;100;50t')
  388. self.ae(s.margin_top, 1), self.ae(s.margin_bottom, 3)
  389. pb('\033[r', ('screen_set_margins', 0, 0))
  390. self.ae(s.margin_top, 0), self.ae(s.margin_bottom, 4)
  391. pb('\033[1 q', ('screen_set_cursor', 1, ord(' ')))
  392. self.assertTrue(s.cursor.blink)
  393. self.ae(s.cursor.shape, CURSOR_BLOCK)
  394. s.reset()
  395. pb('\033[3 @', ('Shift left escape code not implemented',))
  396. pb('\033[3 A', ('Shift right escape code not implemented',))
  397. pb('\033[3;4 S', ('Select presentation directions escape code not implemented',))
  398. pb('\033[1T', ('screen_reverse_scroll', 1))
  399. pb('\033[T', ('screen_reverse_scroll', 1))
  400. pb('\033[+T', ('screen_reverse_scroll_and_fill_from_scrollback', 1))
  401. c.clear()
  402. pb('\033[?2026$p', ('report_mode_status', 2026, 1))
  403. self.ae(c.wtcbuf, b'\x1b[?2026;2$y')
  404. c.clear()
  405. pb('\033[?2026h', ('screen_set_mode', 2026, 1))
  406. pb('\033[?2026$p', ('report_mode_status', 2026, 1))
  407. self.ae(c.wtcbuf, b'\x1b[?2026;1$y')
  408. pb('\033[?2026l', ('screen_reset_mode', 2026, 1))
  409. c.clear()
  410. pb('\033[?2026$p', ('report_mode_status', 2026, 1))
  411. self.ae(c.wtcbuf, b'\x1b[?2026;2$y')
  412. def test_csi_code_rep(self):
  413. s = self.create_screen(8)
  414. pb = partial(self.parse_bytes_dump, s)
  415. pb('\033[1b', ('screen_repeat_character', 1))
  416. self.ae(str(s.line(0)), '')
  417. pb('x\033[7b', 'x', ('screen_repeat_character', 7))
  418. self.ae(str(s.line(0)), 'xxxxxxxx')
  419. pb('\033[1;3H', ('screen_cursor_position', 1, 3))
  420. pb('\033[byz\033[b', ('screen_repeat_character', 1), 'yz', ('screen_repeat_character', 1))
  421. # repeat 'x' at 3, then 'yz' at 4-5, then repeat 'z' at 6
  422. self.ae(str(s.line(0)), 'xxxyzzxx')
  423. s.reset()
  424. pb(' \033[3b', ' ', ('screen_repeat_character', 3))
  425. self.ae(str(s.line(0)), ' ')
  426. s.reset()
  427. pb('\t\033[b', ('screen_tab',), ('screen_repeat_character', 1))
  428. self.ae(str(s.line(0)), '\t')
  429. s.reset()
  430. b']]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]'
  431. def test_osc_codes(self):
  432. s = self.create_screen()
  433. pb = partial(self.parse_bytes_dump, s)
  434. c = s.callbacks
  435. pb('a\033]2;x\\ryz\033\\bcde', 'a', ('set_title', 'x\\ryz'), 'bcde')
  436. self.ae(str(s.line(0)), 'abcde')
  437. self.ae(c.titlebuf, ['x\\ryz'])
  438. c.clear()
  439. pb('\033]\x07', ('set_title', ''), ('set_icon', ''))
  440. self.ae(c.titlebuf, ['']), self.ae(c.iconbuf, '')
  441. pb('1\033]ab\x072', '1', ('set_title', 'ab'), ('set_icon', 'ab'), '2')
  442. self.ae(c.titlebuf, ['', 'ab']), self.ae(c.iconbuf, 'ab')
  443. c.clear()
  444. pb('\033]2;;;;\x07', ('set_title', ';;;'))
  445. self.ae(c.titlebuf, [';;;'])
  446. c.clear()
  447. pb('\033]2;\x07', ('set_title', ''))
  448. self.ae(c.titlebuf, [''])
  449. pb('\033]110\x07', ('set_dynamic_color', 110, ''))
  450. self.ae(c.colorbuf, '')
  451. c.clear()
  452. pb('\033]9;\x07', ('desktop_notify', 9, ''))
  453. pb('\033]9;test it with a nice long string\x07', ('desktop_notify', 9, 'test it with a nice long string'))
  454. pb('\033]99;moo=foo;test it\x07', ('desktop_notify', 99, 'moo=foo;test it'))
  455. self.ae(c.notifications, [(9, ''), (9, 'test it with a nice long string'), (99, 'moo=foo;test it')])
  456. c.clear()
  457. pb('\033]8;;\x07', ('set_active_hyperlink', None, None))
  458. pb('\033]8moo\x07', ('Ignoring malformed OSC 8 code',))
  459. pb('\033]8;moo\x07', ('Ignoring malformed OSC 8 code',))
  460. pb('\033]8;id=xyz;\x07', ('set_active_hyperlink', 'xyz', None))
  461. pb('\033]8;moo:x=z:id=xyz:id=abc;http://yay;.com\x07', ('set_active_hyperlink', 'xyz', 'http://yay;.com'))
  462. c.clear()
  463. payload = '1' * 1024
  464. pb(f'\033]52;p;{payload}\x07', ('clipboard_control', 52, f'p;{payload}'))
  465. c.clear()
  466. pb('\033]52;p;xyz\x07', ('clipboard_control', 52, 'p;xyz'))
  467. def test_dcs_codes(self):
  468. s = self.create_screen()
  469. c = s.callbacks
  470. pb = partial(self.parse_bytes_dump, s)
  471. q = hexlify(b'kind').decode('ascii')
  472. pb(f'a\033P+q{q}\033\\bcde', 'a', ('screen_request_capabilities', 43, q), 'bcde')
  473. self.ae(str(s.line(0)), 'abcde')
  474. self.ae(c.wtcbuf, '1+r{}={}'.format(q, '1b5b313b3242').encode('ascii'))
  475. c.clear()
  476. pb('\033P$q q\033\\', ('screen_request_capabilities', ord('$'), ' q'))
  477. self.ae(c.wtcbuf, b'\033P1$r1 q\033\\')
  478. c.clear()
  479. pb('\033P$qm\033\\', ('screen_request_capabilities', ord('$'), 'm'))
  480. self.ae(c.wtcbuf, b'\033P1$rm\033\\')
  481. for sgr in '0;34;102;1;2;3;4 0;38:5:200;58:2:10:11:12'.split():
  482. expected = set(sgr.split(';')) - {'0'}
  483. c.clear()
  484. parse_bytes(s, f'\033[{sgr}m\033P$qm\033\\'.encode('ascii'))
  485. r = c.wtcbuf.decode('ascii').partition('r')[2].partition('m')[0]
  486. self.ae(expected, set(r.split(';')))
  487. c.clear()
  488. pb('\033P$qr\033\\', ('screen_request_capabilities', ord('$'), 'r'))
  489. self.ae(c.wtcbuf, f'\033P1$r{s.margin_top + 1};{s.margin_bottom + 1}r\033\\'.encode('ascii'))
  490. pb('\033P@kitty-cmd{abc\033\\', ('handle_remote_cmd', '{abc'))
  491. p = base64_encode('abcd').decode()
  492. pb(f'\033P@kitty-print|{p}\033\\', ('handle_remote_print', p))
  493. self.ae(['abcd'], s.callbacks.printbuf)
  494. c.clear()
  495. pb('\033[?2026$p', ('report_mode_status', 2026, 1))
  496. self.ae(c.wtcbuf, b'\x1b[?2026;2$y')
  497. pb('\033P=1s\033\\', ('screen_start_pending_mode',))
  498. c.clear()
  499. pb('\033[?2026$p', ('report_mode_status', 2026, 1))
  500. self.ae(c.wtcbuf, b'\x1b[?2026;1$y')
  501. pb('\033P=2s\033\\', ('screen_stop_pending_mode',))
  502. c.clear()
  503. pb('\033[?2026$p', ('report_mode_status', 2026, 1))
  504. self.ae(c.wtcbuf, b'\x1b[?2026;2$y')
  505. def test_oth_codes(self):
  506. s = self.create_screen()
  507. pb = partial(self.parse_bytes_dump, s)
  508. pb('a\033_+\\+\033\\bcde', ('draw', 'a'), ('Unrecognized APC code: 0x2b',), ('draw', 'bcde'))
  509. pb('a\033^+\\+\033\\bcde', ('draw', 'a'), ('Unrecognized PM code: 0x2b',), ('draw', 'bcde'))
  510. pb('a\033X+\\+\033\\bcde', ('draw', 'a'), ('Unrecognized SOS code: 0x2b',), ('draw', 'bcde'))
  511. def test_graphics_command(self):
  512. from base64 import standard_b64encode
  513. def enc(x):
  514. return standard_b64encode(x.encode('utf-8') if isinstance(x, str) else x).decode('ascii')
  515. def c(**k):
  516. for p, v in tuple(k.items()):
  517. if isinstance(v, str) and p != 'payload':
  518. k[p] = v.encode('ascii')
  519. for f in 'action delete_action transmission_type compressed'.split():
  520. k.setdefault(f, b'\0')
  521. for f in ('format more id data_sz data_offset width height x_offset y_offset data_height data_width cursor_movement'
  522. ' num_cells num_lines cell_x_offset cell_y_offset z_index placement_id image_number quiet unicode_placement'
  523. ' parent_id parent_placement_id offset_from_parent_x offset_from_parent_y'
  524. ).split():
  525. k.setdefault(f, 0)
  526. p = k.pop('payload', '').encode('utf-8')
  527. k['payload_sz'] = len(p)
  528. return ('graphics_command', k, p)
  529. def t(cmd, **kw):
  530. pb('\033_G{};{}\033\\'.format(cmd, enc(kw.get('payload', ''))), c(**kw))
  531. def e(cmd, err):
  532. pb(f'\033_G{cmd}\033\\', (err,))
  533. s = self.create_screen()
  534. pb = partial(self.parse_bytes_dump, s)
  535. uint32_max = 2**32 - 1
  536. t('i=%d' % uint32_max, id=uint32_max)
  537. t('i=3,p=4', id=3, placement_id=4)
  538. e('i=%d' % (uint32_max + 1), 'Malformed GraphicsCommand control block, number is too large')
  539. pb('\033_Gi=12\033\\', c(id=12))
  540. t('a=t,t=d,s=100,z=-9', payload='X', action='t', transmission_type='d', data_width=100, z_index=-9, payload_sz=1)
  541. t('a=t,t=d,s=100,z=9', payload='payload', action='t', transmission_type='d', data_width=100, z_index=9, payload_sz=7)
  542. t('a=t,t=d,s=100,z=9,q=2', action='t', transmission_type='d', data_width=100, z_index=9, quiet=2)
  543. e(',s=1', 'Malformed GraphicsCommand control block, invalid key character: 0x2c')
  544. e('W=1', 'Malformed GraphicsCommand control block, invalid key character: 0x57')
  545. e('1=1', 'Malformed GraphicsCommand control block, invalid key character: 0x31')
  546. e('a=t,,w=2', 'Malformed GraphicsCommand control block, invalid key character: 0x2c')
  547. e('s', 'Malformed GraphicsCommand control block, no = after key')
  548. e('s=', 'Malformed GraphicsCommand control block, expecting an integer value')
  549. e('s==', 'Malformed GraphicsCommand control block, expecting an integer value for key: s')
  550. e('s=1=', 'Malformed GraphicsCommand control block, expecting a comma or semi-colon after a value, found: 0x3d')
  551. def test_deccara(self):
  552. s = self.create_screen()
  553. pb = partial(self.parse_bytes_dump, s)
  554. pb('\033[$r', ('deccara', '0 0 0 0 0'))
  555. pb('\033[;;;;4:3;38:5:10;48:2:1:2:3;1$r',
  556. ('deccara', '0 0 0 0 4:3'), ('deccara', '0 0 0 0 38:5:10'), ('deccara', '0 0 0 0 48:2:1:2:3'), ('deccara', '0 0 0 0 1'))
  557. for y in range(s.lines):
  558. line = s.line(y)
  559. for x in range(s.columns):
  560. c = line.cursor_from(x)
  561. self.ae(c.bold, True)
  562. self.ae(c.italic, False)
  563. self.ae(c.decoration, 3)
  564. self.ae(c.fg, (10 << 8) | 1)
  565. self.ae(c.bg, (1 << 24 | 2 << 16 | 3 << 8 | 2))
  566. self.ae(s.line(0).cursor_from(0).bold, True)
  567. pb('\033[1;2;2;3;22;39$r', ('deccara', '1 2 2 3 22 39'))
  568. self.ae(s.line(0).cursor_from(0).bold, True)
  569. line = s.line(0)
  570. for x in range(1, s.columns):
  571. c = line.cursor_from(x)
  572. self.ae(c.bold, False)
  573. self.ae(c.fg, 0)
  574. line = s.line(1)
  575. for x in range(0, 3):
  576. c = line.cursor_from(x)
  577. self.ae(c.bold, False)
  578. self.ae(line.cursor_from(3).bold, True)
  579. pb('\033[2*x\033[3;2;4;3;34$r\033[*x', ('screen_decsace', 2), ('deccara', '3 2 4 3 34'), ('screen_decsace', 0))
  580. for y in range(2, 4):
  581. line = s.line(y)
  582. for x in range(s.columns):
  583. self.ae(line.cursor_from(x).fg, (10 << 8 | 1) if x < 1 or x > 2 else (4 << 8) | 1)