2016-11-14 17:40:01 +03:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# vim:fileencoding=utf-8
|
|
|
|
# License: GPL v3 Copyright: 2016, Kovid Goyal <kovid at kovidgoyal.net>
|
|
|
|
|
2017-01-10 11:22:15 +03:00
|
|
|
import os
|
2016-11-16 06:37:15 +03:00
|
|
|
from functools import partial
|
2017-01-10 11:22:15 +03:00
|
|
|
from unittest import skipIf
|
2016-11-16 06:37:15 +03:00
|
|
|
|
2016-11-14 17:40:01 +03:00
|
|
|
from . import BaseTest
|
2016-11-17 11:20:20 +03:00
|
|
|
from kitty.fast_data_types import parse_bytes, parse_bytes_dump, CURSOR_BLOCK
|
2016-11-14 18:10:54 +03:00
|
|
|
|
|
|
|
|
|
|
|
class CmdDump(list):
|
|
|
|
|
|
|
|
def __call__(self, *a):
|
|
|
|
self.append(a)
|
2016-11-14 17:40:01 +03:00
|
|
|
|
|
|
|
|
2016-11-23 13:03:08 +03:00
|
|
|
class TestParser(BaseTest):
|
2016-11-14 17:40:01 +03:00
|
|
|
|
2016-11-17 11:20:20 +03:00
|
|
|
def parse_bytes_dump(self, s, x, *cmds):
|
2016-11-16 06:37:15 +03:00
|
|
|
cd = CmdDump()
|
|
|
|
if isinstance(x, str):
|
|
|
|
x = x.encode('utf-8')
|
2016-11-23 13:17:22 +03:00
|
|
|
cmds = tuple(('draw', x) if isinstance(x, str) else x for x in cmds)
|
2016-11-18 13:00:59 +03:00
|
|
|
parse_bytes_dump(cd, s, x)
|
2016-11-23 13:17:22 +03:00
|
|
|
current = ''
|
|
|
|
q = []
|
|
|
|
for args in cd:
|
|
|
|
if args[0] == 'draw':
|
2016-11-24 11:13:28 +03:00
|
|
|
if args[1] is not None:
|
|
|
|
current += args[1]
|
2016-11-23 13:17:22 +03:00
|
|
|
else:
|
|
|
|
if current:
|
|
|
|
q.append(('draw', current))
|
|
|
|
current = ''
|
|
|
|
q.append(args)
|
|
|
|
if current:
|
|
|
|
q.append(('draw', current))
|
|
|
|
self.ae(tuple(q), cmds)
|
2016-11-16 06:37:15 +03:00
|
|
|
|
2017-01-10 11:22:15 +03:00
|
|
|
@skipIf('ANCIENT_WCWIDTH' in os.environ, 'wcwidth() is too old')
|
2016-11-14 17:40:01 +03:00
|
|
|
def test_simple_parsing(self):
|
|
|
|
s = self.create_screen()
|
2016-11-17 11:20:20 +03:00
|
|
|
pb = partial(self.parse_bytes_dump, s)
|
2016-11-14 18:10:54 +03:00
|
|
|
|
2016-11-18 13:59:15 +03:00
|
|
|
pb('12', '12')
|
2017-09-09 07:55:03 +03:00
|
|
|
self.ae(str(s.line(0)), '12')
|
2016-11-18 14:16:04 +03:00
|
|
|
self.ae(s.cursor.x, 2)
|
2016-11-18 13:59:15 +03:00
|
|
|
pb('3456', '3456')
|
2016-11-14 17:40:01 +03:00
|
|
|
self.ae(str(s.line(0)), '12345')
|
2017-09-09 07:55:03 +03:00
|
|
|
self.ae(str(s.line(1)), '6')
|
2016-11-30 12:36:17 +03:00
|
|
|
pb(b'\n123\n\r45', ('screen_linefeed',), '123', ('screen_linefeed',), ('screen_carriage_return',), '45')
|
2017-09-09 07:55:03 +03:00
|
|
|
self.ae(str(s.line(1)), '6')
|
|
|
|
self.ae(str(s.line(2)), ' 123')
|
|
|
|
self.ae(str(s.line(3)), '45')
|
2016-11-14 17:40:01 +03:00
|
|
|
parse_bytes(s, b'\rabcde')
|
|
|
|
self.ae(str(s.line(3)), 'abcde')
|
2016-11-30 12:36:17 +03:00
|
|
|
pb('\rßxyz1', ('screen_carriage_return',), 'ßxyz1')
|
2016-11-14 17:40:01 +03:00
|
|
|
self.ae(str(s.line(3)), 'ßxyz1')
|
2016-11-18 13:59:15 +03:00
|
|
|
pb('ニチ ', 'ニチ ')
|
2016-11-14 17:40:01 +03:00
|
|
|
self.ae(str(s.line(4)), 'ニチ ')
|
2016-11-16 06:37:15 +03:00
|
|
|
|
|
|
|
def test_esc_codes(self):
|
|
|
|
s = self.create_screen()
|
2016-11-17 11:20:20 +03:00
|
|
|
pb = partial(self.parse_bytes_dump, s)
|
2016-11-30 15:18:52 +03:00
|
|
|
pb('12\033Da', '12', ('screen_index',), 'a')
|
2017-09-09 07:55:03 +03:00
|
|
|
self.ae(str(s.line(0)), '12')
|
|
|
|
self.ae(str(s.line(1)), ' a')
|
2016-11-23 13:17:22 +03:00
|
|
|
pb('\033x', ('Unknown char after ESC: 0x%x' % ord('x'),))
|
2016-11-30 15:18:52 +03:00
|
|
|
pb('\033c123', ('screen_reset', ), '123')
|
2017-09-09 07:55:03 +03:00
|
|
|
self.ae(str(s.line(0)), '123')
|
2016-11-16 11:10:38 +03:00
|
|
|
|
2016-11-30 17:14:41 +03:00
|
|
|
def test_charsets(self):
|
2017-04-28 06:01:07 +03:00
|
|
|
s = self.create_screen()
|
|
|
|
pb = partial(self.parse_bytes_dump, s)
|
|
|
|
pb(b'\xc3')
|
|
|
|
pb(b'\xa1', ('draw', b'\xc3\xa1'.decode('utf-8')))
|
2016-11-30 17:14:41 +03:00
|
|
|
s = self.create_screen()
|
|
|
|
pb = partial(self.parse_bytes_dump, s)
|
2016-11-30 21:06:10 +03:00
|
|
|
pb('\033)0\x0e/_', ('screen_designate_charset', 1, ord('0')), ('screen_change_charset', 1), '/_')
|
2017-09-09 07:55:03 +03:00
|
|
|
self.ae(str(s.line(0)), '/\xa0')
|
2017-04-28 07:11:47 +03:00
|
|
|
self.assertTrue(s.callbacks.iutf8)
|
|
|
|
pb('\033%@_', ('screen_use_latin1', 1), '_')
|
|
|
|
self.assertFalse(s.callbacks.iutf8)
|
2016-12-13 10:13:20 +03:00
|
|
|
s = self.create_screen()
|
|
|
|
pb = partial(self.parse_bytes_dump, s)
|
|
|
|
pb('\033(0/_', ('screen_designate_charset', 0, ord('0')), '/_')
|
2017-09-09 07:55:03 +03:00
|
|
|
self.ae(str(s.line(0)), '/\xa0')
|
2016-11-30 17:14:41 +03:00
|
|
|
|
2016-11-16 11:10:38 +03:00
|
|
|
def test_csi_codes(self):
|
|
|
|
s = self.create_screen()
|
2016-11-17 11:20:20 +03:00
|
|
|
pb = partial(self.parse_bytes_dump, s)
|
2016-11-18 13:59:15 +03:00
|
|
|
pb('abcde', 'abcde')
|
2016-11-16 11:10:38 +03:00
|
|
|
s.cursor_back(5)
|
2016-11-18 13:59:15 +03:00
|
|
|
pb('x\033[2@y', 'x', ('screen_insert_characters', 2), 'y')
|
2016-11-16 11:10:38 +03:00
|
|
|
self.ae(str(s.line(0)), 'xy bc')
|
2016-11-18 13:59:15 +03:00
|
|
|
pb('x\033[2;7@y', 'x', ('screen_insert_characters', 2), 'y')
|
|
|
|
pb('x\033[@y', 'x', ('screen_insert_characters', 1), 'y')
|
|
|
|
pb('x\033[345@y', 'x', ('screen_insert_characters', 345), 'y')
|
|
|
|
pb('x\033[345;@y', 'x', ('screen_insert_characters', 345), 'y')
|
2016-11-16 17:23:38 +03:00
|
|
|
pb('\033[H', ('screen_cursor_position', 1, 1))
|
|
|
|
self.ae(s.cursor.x, 0), self.ae(s.cursor.y, 0)
|
|
|
|
pb('\033[4H', ('screen_cursor_position', 4, 1))
|
2016-11-30 06:22:32 +03:00
|
|
|
pb('\033[4;0H', ('screen_cursor_position', 4, 0))
|
2016-11-16 17:23:38 +03:00
|
|
|
pb('\033[3;2H', ('screen_cursor_position', 3, 2))
|
|
|
|
pb('\033[3;2;H', ('screen_cursor_position', 3, 2))
|
2016-11-30 06:20:55 +03:00
|
|
|
pb('\033[00000000003;0000000000000002H', ('screen_cursor_position', 3, 2))
|
2016-11-16 17:23:38 +03:00
|
|
|
self.ae(s.cursor.x, 1), self.ae(s.cursor.y, 2)
|
2016-11-16 17:49:31 +03:00
|
|
|
pb('\033[J', ('screen_erase_in_display', 0, 0))
|
|
|
|
pb('\033[?J', ('screen_erase_in_display', 0, 1))
|
|
|
|
pb('\033[?2J', ('screen_erase_in_display', 2, 1))
|
2016-11-17 06:04:55 +03:00
|
|
|
pb('\033[h')
|
2016-11-30 06:34:59 +03:00
|
|
|
pb('\033[20;4h', ('screen_set_mode', 20, 0), ('screen_set_mode', 4, 0))
|
|
|
|
pb('\033[?1000;1004h', ('screen_set_mode', 1000, 1), ('screen_set_mode', 1004, 1))
|
|
|
|
pb('\033[20;4;20l', ('screen_reset_mode', 20, 0), ('screen_reset_mode', 4, 0), ('screen_reset_mode', 20, 0))
|
2016-11-16 11:10:38 +03:00
|
|
|
s.reset()
|
2016-11-30 05:45:40 +03:00
|
|
|
pb('\033[1;3;4;7;9;34;44m', ('select_graphic_rendition', '1 3 4 7 9 34 44 '))
|
2016-11-17 06:04:55 +03:00
|
|
|
for attr in 'bold italic reverse strikethrough'.split():
|
|
|
|
self.assertTrue(getattr(s.cursor, attr))
|
|
|
|
self.ae(s.cursor.decoration, 1)
|
2016-11-24 12:58:52 +03:00
|
|
|
self.ae(s.cursor.fg, 4 << 8 | 1)
|
|
|
|
self.ae(s.cursor.bg, 4 << 8 | 1)
|
2016-11-30 05:45:40 +03:00
|
|
|
pb('\033[38;5;1;48;5;7m', ('select_graphic_rendition', '38 5 1 48 5 7 '))
|
2016-11-24 12:58:52 +03:00
|
|
|
self.ae(s.cursor.fg, 1 << 8 | 1)
|
|
|
|
self.ae(s.cursor.bg, 7 << 8 | 1)
|
2016-11-30 05:45:40 +03:00
|
|
|
pb('\033[38;2;1;2;3;48;2;7;8;9m', ('select_graphic_rendition', '38 2 1 2 3 48 2 7 8 9 '))
|
2016-11-24 12:58:52 +03:00
|
|
|
self.ae(s.cursor.fg, 1 << 24 | 2 << 16 | 3 << 8 | 2)
|
|
|
|
self.ae(s.cursor.bg, 7 << 24 | 8 << 16 | 9 << 8 | 2)
|
2016-11-30 13:33:45 +03:00
|
|
|
pb('\033[;2m', ('select_graphic_rendition', '0 2 '))
|
2016-11-24 13:58:08 +03:00
|
|
|
c = s.callbacks
|
2016-11-17 08:29:15 +03:00
|
|
|
pb('\033[5n', ('report_device_status', 5, 0))
|
|
|
|
self.ae(c.wtcbuf, b'\033[0n')
|
|
|
|
c.clear()
|
|
|
|
pb('\033[6n', ('report_device_status', 6, 0))
|
|
|
|
self.ae(c.wtcbuf, b'\033[1;1R')
|
2016-11-18 13:59:15 +03:00
|
|
|
pb('12345', '12345')
|
2016-11-17 08:29:15 +03:00
|
|
|
c.clear()
|
|
|
|
pb('\033[6n', ('report_device_status', 6, 0))
|
|
|
|
self.ae(c.wtcbuf, b'\033[2;1R')
|
2017-02-05 13:50:50 +03:00
|
|
|
c.clear()
|
2017-02-05 14:09:40 +03:00
|
|
|
s.cursor_key_mode = True
|
2017-02-05 13:50:50 +03:00
|
|
|
pb('\033[?1$p', ('report_mode_status', 1, 1))
|
|
|
|
self.ae(c.wtcbuf, b'\033[?1;1$y')
|
|
|
|
pb('\033[?1l', ('screen_reset_mode', 1, 1))
|
2017-02-05 14:09:40 +03:00
|
|
|
self.assertFalse(s.cursor_key_mode)
|
2017-02-05 13:50:50 +03:00
|
|
|
c.clear()
|
2017-02-10 13:30:51 +03:00
|
|
|
pb('\033[?2016$p', ('report_mode_status', 2016, 1))
|
|
|
|
self.ae(c.wtcbuf, b'\033[?2016;3$y')
|
|
|
|
c.clear()
|
2017-02-05 13:50:50 +03:00
|
|
|
pb('\033[?1$p', ('report_mode_status', 1, 1))
|
|
|
|
self.ae(c.wtcbuf, b'\033[?1;2$y')
|
2016-11-17 08:43:28 +03:00
|
|
|
pb('\033[2;4r', ('screen_set_margins', 2, 4))
|
|
|
|
self.ae(s.margin_top, 1), self.ae(s.margin_bottom, 3)
|
|
|
|
pb('\033[r', ('screen_set_margins', 0, 0))
|
|
|
|
self.ae(s.margin_top, 0), self.ae(s.margin_bottom, 4)
|
2016-11-17 11:20:20 +03:00
|
|
|
pb('\033[1 q', ('screen_set_cursor', 1, ord(' ')))
|
|
|
|
self.assertTrue(s.cursor.blink)
|
|
|
|
self.ae(s.cursor.shape, CURSOR_BLOCK)
|
2016-11-18 08:46:17 +03:00
|
|
|
|
|
|
|
def test_osc_codes(self):
|
|
|
|
s = self.create_screen()
|
|
|
|
pb = partial(self.parse_bytes_dump, s)
|
2016-11-24 13:58:08 +03:00
|
|
|
c = s.callbacks
|
2017-04-05 06:37:55 +03:00
|
|
|
pb('a\033]2;x\\ryz\x9cbcde', 'a', ('set_title', 'x\\ryz'), 'bcde')
|
2016-11-18 08:46:17 +03:00
|
|
|
self.ae(str(s.line(0)), 'abcde')
|
2017-04-05 06:37:55 +03:00
|
|
|
self.ae(c.titlebuf, 'x\\ryz')
|
2016-11-18 08:46:17 +03:00
|
|
|
c.clear()
|
2016-11-23 14:55:38 +03:00
|
|
|
pb('\033]\x07', ('set_title', ''), ('set_icon', ''))
|
|
|
|
self.ae(c.titlebuf, ''), self.ae(c.iconbuf, '')
|
|
|
|
pb('\033]ab\x07', ('set_title', 'ab'), ('set_icon', 'ab'))
|
|
|
|
self.ae(c.titlebuf, 'ab'), self.ae(c.iconbuf, 'ab')
|
2016-11-18 08:46:17 +03:00
|
|
|
c.clear()
|
2016-11-23 14:55:38 +03:00
|
|
|
pb('\033]2;;;;\x07', ('set_title', ';;;'))
|
|
|
|
self.ae(c.titlebuf, ';;;')
|
2017-05-19 13:24:34 +03:00
|
|
|
pb('\033]110\x07', ('set_dynamic_color', 110, ''))
|
2016-11-23 14:55:38 +03:00
|
|
|
self.ae(c.colorbuf, '')
|
2016-11-18 09:01:48 +03:00
|
|
|
|
2016-11-23 17:52:52 +03:00
|
|
|
def test_dcs_codes(self):
|
|
|
|
s = self.create_screen()
|
|
|
|
pb = partial(self.parse_bytes_dump, s)
|
2016-11-24 05:44:29 +03:00
|
|
|
pb('a\033P+q436f\x9cbcde', 'a', ('screen_request_capabilities', '436f'), 'bcde')
|
2016-11-23 17:52:52 +03:00
|
|
|
self.ae(str(s.line(0)), 'abcde')
|
2017-01-20 11:01:05 +03:00
|
|
|
|
|
|
|
def test_oth_codes(self):
|
|
|
|
s = self.create_screen()
|
|
|
|
pb = partial(self.parse_bytes_dump, s)
|
2017-09-25 10:38:11 +03:00
|
|
|
for prefix in '\033_', '\u009f':
|
2017-01-20 11:01:05 +03:00
|
|
|
for suffix in '\u009c', '\033\\':
|
2017-09-25 10:38:11 +03:00
|
|
|
pb('a{}+\\++{}bcde'.format(prefix, suffix), ('draw', 'a'), ('Unrecognized APC code: 0x2b',), ('draw', 'bcde'))
|
|
|
|
for prefix in '\033^', '\u009e':
|
|
|
|
for suffix in '\u009c', '\033\\':
|
|
|
|
pb('a{}+\\++{}bcde'.format(prefix, suffix), ('draw', 'a'), ('Unrecognized PM code: 0x2b',), ('draw', 'bcde'))
|
2017-09-25 18:24:36 +03:00
|
|
|
|
|
|
|
def test_graphics_command(self):
|
|
|
|
from base64 import standard_b64encode
|
|
|
|
|
2017-09-26 08:45:46 +03:00
|
|
|
def enc(x):
|
2017-09-25 18:24:36 +03:00
|
|
|
return standard_b64encode(x.encode('utf-8') if isinstance(x, str) else x).decode('ascii')
|
|
|
|
|
|
|
|
def c(**k):
|
|
|
|
for p, v in tuple(k.items()):
|
2017-09-26 08:45:46 +03:00
|
|
|
if isinstance(v, str) and p != 'payload':
|
2017-09-25 18:24:36 +03:00
|
|
|
k[p] = v.encode('ascii')
|
|
|
|
for f in 'action transmission_type'.split():
|
|
|
|
k.setdefault(f, b'\0')
|
2017-09-26 08:45:46 +03:00
|
|
|
for f in 'format more id width height x_offset y_offset data_height data_width num_cells num_lines z_index'.split():
|
2017-09-25 18:24:36 +03:00
|
|
|
k.setdefault(f, 0)
|
2017-09-26 08:45:46 +03:00
|
|
|
p = k.pop('payload', '').encode('utf-8')
|
|
|
|
k['payload_sz'] = len(p)
|
2017-09-26 08:52:14 +03:00
|
|
|
return ('graphics_command', k, p)
|
2017-09-25 18:24:36 +03:00
|
|
|
|
2017-09-26 08:45:46 +03:00
|
|
|
def t(cmd, **kw):
|
|
|
|
pb('\033_G{};{}\033\\'.format(cmd, enc(kw.get('payload', ''))), c(**kw))
|
|
|
|
|
|
|
|
def e(cmd, err):
|
|
|
|
pb('\033_G{}\033\\'.format(cmd), (err,))
|
2017-09-25 19:05:41 +03:00
|
|
|
|
2017-09-25 18:24:36 +03:00
|
|
|
s = self.create_screen()
|
|
|
|
pb = partial(self.parse_bytes_dump, s)
|
2017-09-25 19:05:41 +03:00
|
|
|
pb('\033_Gi=12\033\\', c(id=12))
|
2017-09-26 08:45:46 +03:00
|
|
|
t('a=t,t=f,s=100,z=-9', payload='X', action='t', transmission_type='f', data_width=100, z_index=-9, payload_sz=1)
|
|
|
|
t('a=t,t=f,s=100,z=9', payload='payload', action='t', transmission_type='f', data_width=100, z_index=9, payload_sz=7)
|
|
|
|
t('a=t,t=f,s=100,z=9', action='t', transmission_type='f', data_width=100, z_index=9)
|
|
|
|
e(',s=1', 'Malformed graphics control block, invalid key character: 0x2c')
|
|
|
|
e('W=1', 'Malformed graphics control block, invalid key character: 0x57')
|
|
|
|
e('1=1', 'Malformed graphics control block, invalid key character: 0x31')
|
2017-09-27 06:40:56 +03:00
|
|
|
e('a=t,,w=2', 'Malformed graphics control block, invalid key character: 0x2c')
|
2017-09-26 08:45:46 +03:00
|
|
|
e('s', 'Malformed graphics control block, no = after key')
|
|
|
|
e('s=', 'Malformed graphics control block, expecting an integer value')
|
|
|
|
e('s==', 'Malformed graphics control block, expecting an integer value')
|
|
|
|
e('s=1=', 'Malformed graphics control block, expecting a comma or semi-colon after a value, found: 0x3d')
|
2017-09-27 06:40:56 +03:00
|
|
|
e('s=20000', 'Image too large')
|
|
|
|
e('v=20000', 'Image too large')
|