screen.py 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317
  1. #!/usr/bin/env python
  2. # License: GPL v3 Copyright: 2016, Kovid Goyal <kovid at kovidgoyal.net>
  3. from kitty.config import defaults
  4. from kitty.fast_data_types import DECAWM, DECCOLM, DECOM, IRM, VT_PARSER_BUFFER_SIZE, Color, ColorProfile, Cursor
  5. from kitty.marks import marker_from_function, marker_from_regex
  6. from kitty.rgb import color_names
  7. from kitty.window import pagerhist
  8. from . import BaseTest, parse_bytes
  9. class TestScreen(BaseTest):
  10. def test_draw_fast(self):
  11. s = self.create_screen()
  12. # Test in line-wrap, non-insert mode
  13. s.draw('a' * 5)
  14. self.ae(str(s.line(0)), 'a' * 5)
  15. self.ae(s.cursor.x, 5), self.ae(s.cursor.y, 0)
  16. s.draw('b' * 7)
  17. self.assertTrue(s.linebuf.is_continued(1))
  18. self.assertTrue(s.linebuf.is_continued(2))
  19. self.ae(str(s.line(0)), 'a' * 5)
  20. self.ae(str(s.line(1)), 'b' * 5)
  21. self.ae(str(s.line(2)), 'b' * 2)
  22. self.ae(s.cursor.x, 2), self.ae(s.cursor.y, 2)
  23. s.draw('c' * 15)
  24. self.ae(str(s.line(0)), 'b' * 5)
  25. self.ae(str(s.line(1)), 'bbccc')
  26. # Now test without line-wrap
  27. s.reset(), s.reset_dirty()
  28. s.reset_mode(DECAWM)
  29. s.draw('0123456789')
  30. self.ae(str(s.line(0)), '01239')
  31. self.ae(s.cursor.x, 5), self.ae(s.cursor.y, 0)
  32. s.draw('ab')
  33. self.ae(str(s.line(0)), '0123b')
  34. self.ae(s.cursor.x, 5), self.ae(s.cursor.y, 0)
  35. # Now test in insert mode
  36. s.reset(), s.reset_dirty()
  37. s.set_mode(IRM)
  38. s.draw('12345' * 5)
  39. s.cursor_back(5)
  40. self.ae(s.cursor.x, 0), self.ae(s.cursor.y, 4)
  41. s.reset_dirty()
  42. s.draw('ab')
  43. self.ae(str(s.line(4)), 'ab123')
  44. self.ae((s.cursor.x, s.cursor.y), (2, 4))
  45. def test_draw_char(self):
  46. # Test in line-wrap, non-insert mode
  47. s = self.create_screen()
  48. s.draw('ココx')
  49. self.ae(str(s.line(0)), 'ココx')
  50. self.ae(tuple(map(s.line(0).width, range(5))), (2, 0, 2, 0, 1))
  51. self.ae(s.cursor.x, 5), self.ae(s.cursor.y, 0)
  52. s.draw('ニチハ')
  53. self.ae(str(s.line(0)), 'ココx')
  54. self.ae(str(s.line(1)), 'ニチ')
  55. self.ae(str(s.line(2)), 'ハ')
  56. self.ae(s.cursor.x, 2), self.ae(s.cursor.y, 2)
  57. s.draw('Ƶ̧\u0308')
  58. self.ae(str(s.line(2)), 'ハƵ̧\u0308')
  59. self.ae(s.cursor.x, 3), self.ae(s.cursor.y, 2)
  60. s.draw('xy'), s.draw('\u0306')
  61. self.ae(str(s.line(2)), 'ハƵ̧\u0308xy\u0306')
  62. self.ae(s.cursor.x, 5), self.ae(s.cursor.y, 2)
  63. s.draw('c' * 15)
  64. self.ae(str(s.line(0)), 'ニチ')
  65. # Now test without line-wrap
  66. s.reset(), s.reset_dirty()
  67. s.reset_mode(DECAWM)
  68. s.draw('0\u030612345\u03066789\u0306')
  69. self.ae(str(s.line(0)), '0\u03061239\u0306')
  70. self.ae(s.cursor.x, 5), self.ae(s.cursor.y, 0)
  71. s.draw('ab\u0306')
  72. self.ae(str(s.line(0)), '0\u0306123b\u0306')
  73. self.ae(s.cursor.x, 5), self.ae(s.cursor.y, 0)
  74. # Now test in insert mode
  75. s.reset(), s.reset_dirty()
  76. s.set_mode(IRM)
  77. s.draw('1\u03062345' * 5)
  78. s.cursor_back(5)
  79. self.ae(s.cursor.x, 0), self.ae(s.cursor.y, 4)
  80. s.reset_dirty()
  81. s.draw('a\u0306b')
  82. self.ae(str(s.line(4)), 'a\u0306b1\u030623')
  83. self.ae((s.cursor.x, s.cursor.y), (2, 4))
  84. def test_rep(self):
  85. s = self.create_screen()
  86. s.draw('a')
  87. parse_bytes(s, b'\x1b[b')
  88. self.ae(str(s.line(0)), 'aa')
  89. parse_bytes(s, b'\x1b[3b')
  90. self.ae(str(s.line(0)), 'a'*5)
  91. s.draw(' ')
  92. parse_bytes(s, b'\x1b[3b')
  93. self.ae(str(s.line(1)), ' '*4)
  94. def test_emoji_skin_tone_modifiers(self):
  95. s = self.create_screen()
  96. q = chr(0x1f469) + chr(0x1f3fd)
  97. s.draw(q)
  98. self.ae(str(s.line(0)), q)
  99. self.ae(s.cursor.x, 2)
  100. def test_regional_indicators(self):
  101. s = self.create_screen()
  102. flag = '\U0001f1ee\U0001f1f3'
  103. s.draw(flag)
  104. self.ae(str(s.line(0)), flag)
  105. self.ae(s.cursor.x, 2)
  106. s = self.create_screen()
  107. s.draw('a'), s.draw(flag[0]), s.draw('b')
  108. self.ae(str(s.line(0)), 'a' + flag[0] + 'b')
  109. self.ae(s.cursor.x, 4)
  110. def test_zwj(self):
  111. s = self.create_screen(cols=20)
  112. q = '\U0001f468\u200d\U0001f469\u200d\U0001f467\u200d\U0001f466'
  113. s.draw(q)
  114. self.ae(q, str(s.line(0)))
  115. self.ae(s.cursor.x, 8)
  116. for x in '\u200b\u200c\u200d':
  117. s = self.create_screen()
  118. q = f'X{x}Y'
  119. s.draw(q)
  120. self.ae(q, str(s.line(0)))
  121. self.ae(s.cursor.x, 2)
  122. def test_char_manipulation(self):
  123. s = self.create_screen()
  124. def init():
  125. s.reset(), s.reset_dirty()
  126. s.draw('abcde')
  127. s.cursor.bold = True
  128. s.cursor_back(4)
  129. s.reset_dirty()
  130. self.ae(s.cursor.x, 1)
  131. init()
  132. s.insert_characters(2)
  133. self.ae(str(s.line(0)), 'a bc')
  134. self.assertTrue(s.line(0).cursor_from(1).bold)
  135. s.cursor_back(1)
  136. s.insert_characters(20)
  137. self.ae(str(s.line(0)), '')
  138. s.draw('xココ')
  139. s.cursor_back(5)
  140. s.reset_dirty()
  141. s.insert_characters(1)
  142. self.ae(str(s.line(0)), ' xコ')
  143. c = Cursor()
  144. c.italic = True
  145. s.line(0).apply_cursor(c, 0, 5)
  146. self.ae(s.line(0).width(2), 2)
  147. self.assertTrue(s.line(0).cursor_from(2).italic)
  148. self.assertFalse(s.line(0).cursor_from(2).bold)
  149. init()
  150. s.delete_characters(2)
  151. self.ae(str(s.line(0)), 'ade')
  152. self.assertTrue(s.line(0).cursor_from(4).bold)
  153. self.assertFalse(s.line(0).cursor_from(2).bold)
  154. s = self.create_screen()
  155. s.set_margins(1, 2)
  156. s.cursor.y = 3
  157. s.draw('abcde')
  158. s.cursor.x = 0
  159. s.delete_characters(2)
  160. self.ae('cde', str(s.line(s.cursor.y)))
  161. init()
  162. s.erase_characters(2)
  163. self.ae(str(s.line(0)), 'a de')
  164. self.assertTrue(s.line(0).cursor_from(1).bold)
  165. self.assertFalse(s.line(0).cursor_from(4).bold)
  166. s.erase_characters(20)
  167. self.ae(str(s.line(0)), 'a')
  168. init()
  169. s.erase_in_line()
  170. self.ae(str(s.line(0)), 'a')
  171. self.assertTrue(s.line(0).cursor_from(1).bold)
  172. self.assertFalse(s.line(0).cursor_from(0).bold)
  173. init()
  174. s.erase_in_line(1)
  175. self.ae(str(s.line(0)), ' cde')
  176. init()
  177. s.erase_in_line(2)
  178. self.ae(str(s.line(0)), '')
  179. init()
  180. s.erase_in_line(2, True)
  181. self.ae((False, False, False, False, False), tuple(map(lambda i: s.line(0).cursor_from(i).bold, range(5))))
  182. def test_erase_in_screen(self):
  183. s = self.create_screen()
  184. def init():
  185. s.reset()
  186. s.draw('12345' * 5)
  187. s.reset_dirty()
  188. s.cursor.x, s.cursor.y = 2, 1
  189. s.cursor.bold = True
  190. self.ae(continuations(s), (True, True, True, True, False))
  191. def all_lines(s):
  192. return tuple(str(s.line(i)) for i in range(s.lines))
  193. def continuations(s):
  194. return tuple(s.line(i).last_char_has_wrapped_flag() for i in range(s.lines))
  195. init()
  196. s.erase_in_display(0)
  197. self.ae(all_lines(s), ('12345', '12', '', '', ''))
  198. self.ae(continuations(s), (True, False, False, False, False))
  199. init()
  200. s.erase_in_display(1)
  201. self.ae(all_lines(s), ('', ' 45', '12345', '12345', '12345'))
  202. self.ae(continuations(s), (False, True, True, True, False))
  203. init()
  204. s.erase_in_display(2)
  205. self.ae(all_lines(s), ('', '', '', '', ''))
  206. self.assertTrue(s.line(0).cursor_from(1).bold)
  207. self.ae(continuations(s), (False, False, False, False, False))
  208. init()
  209. s.erase_in_display(2, True)
  210. self.ae(all_lines(s), ('', '', '', '', ''))
  211. self.ae(continuations(s), (False, False, False, False, False))
  212. self.assertFalse(s.line(0).cursor_from(1).bold)
  213. def test_cursor_movement(self):
  214. s = self.create_screen()
  215. s.draw('12345' * 5)
  216. s.reset_dirty()
  217. s.cursor_up(2)
  218. self.ae((s.cursor.x, s.cursor.y), (4, 2))
  219. s.cursor_up1()
  220. self.ae((s.cursor.x, s.cursor.y), (0, 1))
  221. s.cursor_forward(3)
  222. self.ae((s.cursor.x, s.cursor.y), (3, 1))
  223. s.cursor_back()
  224. self.ae((s.cursor.x, s.cursor.y), (2, 1))
  225. s.cursor_down()
  226. self.ae((s.cursor.x, s.cursor.y), (2, 2))
  227. s.cursor_down1(5)
  228. self.ae((s.cursor.x, s.cursor.y), (0, 4))
  229. s = self.create_screen()
  230. s.draw('12345' * 5)
  231. s.index()
  232. self.ae(str(s.line(4)), '')
  233. for i in range(4):
  234. self.ae(str(s.line(i)), '12345')
  235. s.draw('12345' * 5)
  236. s.cursor_up(5)
  237. s.reverse_index()
  238. self.ae(str(s.line(0)), '')
  239. for i in range(1, 5):
  240. self.ae(str(s.line(i)), '12345')
  241. def test_backspace_wide_characters(self):
  242. s = self.create_screen()
  243. s.draw('⛅')
  244. self.ae(s.cursor.x, 2)
  245. s.backspace()
  246. s.draw(' ')
  247. s.backspace()
  248. self.ae(s.cursor.x, 1)
  249. def test_resize(self):
  250. # test that trailing blank line is preserved on resize
  251. s = self.create_screen(cols=5, lines=5, scrollback=15)
  252. for i in range(3):
  253. s.draw(f'oo{i}'), s.index(), s.carriage_return()
  254. s.draw('$ pp'), s.index(), s.carriage_return()
  255. s.resize(s.lines, 2)
  256. self.assertFalse(str(s.line(s.cursor.y)))
  257. self.assertFalse(s.cursor.x)
  258. # test that only happens when last line is not continued
  259. s = self.create_screen(cols=5, lines=5, scrollback=15)
  260. for i in range(3):
  261. s.draw(f'oo{i}'), s.index(), s.carriage_return()
  262. s.draw('p' * (s.columns + 2)), s.carriage_return()
  263. s.resize(s.lines, 2)
  264. self.assertTrue(str(s.line(s.cursor.y)))
  265. s = self.create_screen(scrollback=6)
  266. s.draw(''.join([str(i) * s.columns for i in range(s.lines)]))
  267. s.resize(3, 10)
  268. self.ae(str(s.line(0)), '0'*5 + '1'*5)
  269. self.ae(str(s.line(1)), '2'*5 + '3'*5)
  270. self.ae(str(s.line(2)), '4'*5)
  271. s.resize(5, 1)
  272. self.ae(str(s.line(0)), '4')
  273. self.ae(str(s.historybuf), '3\n3\n3\n3\n3\n2')
  274. s = self.create_screen(scrollback=20)
  275. s.draw(''.join(str(i) * s.columns for i in range(s.lines*2)))
  276. self.ae(str(s.linebuf), '55555\n66666\n77777\n88888\n99999')
  277. s.resize(5, 2)
  278. self.ae(str(s.linebuf), '88\n88\n99\n99\n9')
  279. s = self.create_screen()
  280. s.draw('a' * s.columns)
  281. s.linefeed(), s.carriage_return()
  282. s.draw('bb')
  283. s.resize(s.lines, s.columns - 2)
  284. self.ae(str(s.linebuf), 'aaa\naa\nbb\n\n')
  285. s.cursor.y = s.cursor.x = 0
  286. s.draw('x' * len(str(s.line(0))))
  287. s.linefeed(), s.carriage_return()
  288. s.draw('x' * len(str(s.line(1))))
  289. s.resize(s.lines, s.columns + 4)
  290. self.ae(str(s.linebuf), 'xxx\nxx\nbb\n\n')
  291. s = self.create_screen()
  292. c = s.callbacks
  293. parse_bytes(s, b'\x1b[?2048$p') # ]
  294. self.ae(c.wtcbuf, b'\x1b[?2048;2$y') # ]
  295. c.clear()
  296. parse_bytes(s, b'\x1b[?2048h\x1b[?2048$p') # ]]
  297. self.ae(c.wtcbuf, b'\x1b[?2048;1$y') # ]
  298. self.ae(c.num_of_resize_events, 1)
  299. parse_bytes(s, b'\x1b[?2048h') # ]
  300. self.ae(c.num_of_resize_events, 2)
  301. def test_cursor_after_resize(self):
  302. def draw(text, end_line=True):
  303. s.draw(text)
  304. if end_line:
  305. s.linefeed(), s.carriage_return()
  306. s = self.create_screen()
  307. draw('123'), draw('123')
  308. y_before = s.cursor.y
  309. s.resize(s.lines, s.columns-1)
  310. self.ae(y_before, s.cursor.y)
  311. s = self.create_screen(cols=5, lines=8)
  312. draw('one')
  313. draw('two three four five |||', end_line=False)
  314. s.resize(s.lines + 2, s.columns + 2)
  315. y = s.cursor.y
  316. self.assertIn('|', str(s.line(y)))
  317. s = self.create_screen()
  318. draw('a')
  319. x_before = s.cursor.x
  320. s.resize(s.lines - 1, s.columns)
  321. self.ae(x_before, s.cursor.x)
  322. s = self.create_screen()
  323. s.draw('abc')
  324. b = s.cursor.x
  325. s.resize(7, s.columns)
  326. self.assertEqual(s.cursor.x, b)
  327. s.cursor.x = 0
  328. s.resize(5, s.columns)
  329. self.assertEqual(s.cursor.x, 0)
  330. def test_scrollback_fill_after_resize(self):
  331. def prepare_screen(content=()):
  332. ans = self.create_screen(options={'scrollback_fill_enlarged_window': True})
  333. for line in content:
  334. ans.draw(line)
  335. ans.linefeed()
  336. ans.carriage_return()
  337. return ans
  338. def assert_lines(*lines):
  339. return self.ae(lines, tuple(str(s.line(i)) for i in range(s.lines)))
  340. # test the reverse scroll function
  341. s = prepare_screen(map(str, range(6)))
  342. assert_lines('2', '3', '4', '5', '')
  343. s.reverse_scroll(2, True)
  344. assert_lines('0', '1', '2', '3', '4')
  345. # Height increased, width unchanged → pull down lines to fill new space at the top
  346. s = prepare_screen(map(str, range(6)))
  347. assert_lines('2', '3', '4', '5', '')
  348. dist_from_bottom = s.lines - s.cursor.y
  349. s.resize(7, s.columns)
  350. assert_lines('0', '1', '2', '3', '4', '5', '')
  351. self.ae(dist_from_bottom, s.lines - s.cursor.y)
  352. # Height increased, width increased → rewrap, pull down
  353. s = prepare_screen(['0', '1', '2', '3' * 15])
  354. assert_lines('2', '33333', '33333', '33333', '')
  355. s.resize(7, 12)
  356. assert_lines('0', '1', '2', '333333333333', '333', '', '')
  357. # Height increased, width decreased → rewrap, pull down if possible
  358. s = prepare_screen(['0', '1', '2', '3' * 5])
  359. assert_lines('0', '1', '2', '33333', '')
  360. s.resize(6, 4)
  361. assert_lines('0', '1', '2', '3333', '3', '')
  362. # Height unchanged, width increased → rewrap, pull down if possible
  363. s = prepare_screen(['0', '1', '2', '3' * 15])
  364. assert_lines('2', '33333', '33333', '33333', '')
  365. s.resize(s.lines, 12)
  366. assert_lines('1', '2', '333333333333', '333', '')
  367. # Height decreased, width increased → rewrap, pull down if possible
  368. s = prepare_screen(['0', '1', '2', '3' * 15])
  369. assert_lines('2', '33333', '33333', '33333', '')
  370. s.resize(4, 12)
  371. assert_lines('2', '333333333333', '333', '')
  372. # Height increased with large continued text
  373. s = self.create_screen(options={'scrollback_fill_enlarged_window': True})
  374. s.draw(('x' * (s.columns * s.lines * 2)) + 'abcde')
  375. s.carriage_return(), s.linefeed()
  376. s.draw('>')
  377. assert_lines('xxxxx', 'xxxxx', 'xxxxx', 'abcde', '>')
  378. s.resize(s.lines + 2, s.columns)
  379. assert_lines('xxxxx', 'xxxxx', 'xxxxx', 'xxxxx', 'xxxxx', 'abcde', '>')
  380. def test_tab_stops(self):
  381. # Taken from vttest/main.c
  382. s = self.create_screen(cols=80, lines=2)
  383. s.cursor_position(1, 1)
  384. s.clear_tab_stop(3)
  385. for col in range(1, s.columns - 1, 3):
  386. s.cursor_forward(3)
  387. s.set_tab_stop()
  388. s.cursor_position(1, 4)
  389. for col in range(4, s.columns - 1, 6):
  390. s.clear_tab_stop(0)
  391. s.cursor_forward(6)
  392. s.cursor_position(1, 7)
  393. s.clear_tab_stop(2)
  394. s.cursor_position(1, 1)
  395. for col in range(1, s.columns - 1, 6):
  396. s.tab()
  397. s.draw('*')
  398. s.cursor_position(2, 2)
  399. self.ae(str(s.line(0)), '\t*'*13)
  400. s = self.create_screen(cols=4, lines=2)
  401. s.draw('aaaX\tbbbb')
  402. self.ae(str(s.line(0)) + str(s.line(1)), 'aaaXbbbb')
  403. def test_margins(self):
  404. # Taken from vttest/main.c
  405. s = self.create_screen(cols=80, lines=24)
  406. def nl():
  407. s.carriage_return(), s.linefeed()
  408. for deccolm in (False, True):
  409. if deccolm:
  410. s.resize(24, 132)
  411. s.set_mode(DECCOLM)
  412. else:
  413. s.reset_mode(DECCOLM)
  414. region = s.lines - 6
  415. s.set_margins(3, region + 3)
  416. s.set_mode(DECOM)
  417. for i in range(26):
  418. ch = chr(ord('A') + i)
  419. which = i % 4
  420. if which == 0:
  421. s.cursor_position(region + 1, 1), s.draw(ch)
  422. s.cursor_position(region + 1, s.columns), s.draw(ch.lower())
  423. nl()
  424. elif which == 1:
  425. # Simple wrapping
  426. s.cursor_position(region, s.columns), s.draw(chr(ord('A') + i - 1).lower() + ch)
  427. # Backspace at right margin
  428. s.cursor_position(region + 1, s.columns), s.draw(ch), s.backspace(), s.draw(ch.lower())
  429. nl()
  430. elif which == 2:
  431. # Tab to right margin
  432. s.cursor_position(region + 1, s.columns), s.draw(ch), s.backspace(), s.backspace(), s.tab(), s.tab(), s.draw(ch.lower())
  433. s.cursor_position(region + 1, 2), s.backspace(), s.draw(ch), nl()
  434. else:
  435. s.cursor_position(region + 1, 1), nl()
  436. s.cursor_position(region, 1), s.draw(ch)
  437. s.cursor_position(region, s.columns), s.draw(ch.lower())
  438. for ln in range(2, region + 2):
  439. c = chr(ord('I') + ln - 2)
  440. before = '\t' if ln % 4 == 0 else ' '
  441. self.ae(c + ' ' * (s.columns - 3) + before + c.lower(), str(s.line(ln)))
  442. s.reset_mode(DECOM)
  443. # Test that moving cursor outside the margins works as expected
  444. s = self.create_screen(10, 10)
  445. s.set_margins(4, 6)
  446. s.cursor_position(0, 0)
  447. self.ae(s.cursor.y, 0)
  448. nl()
  449. self.ae(s.cursor.y, 1)
  450. s.cursor.y = s.lines - 1
  451. self.ae(s.cursor.y, 9)
  452. s.reverse_index()
  453. self.ae(s.cursor.y, 8)
  454. def test_sgr(self):
  455. s = self.create_screen()
  456. s.select_graphic_rendition(0, 1, 37, 42)
  457. s.draw('a')
  458. c = s.line(0).cursor_from(0)
  459. self.assertTrue(c.bold)
  460. self.ae(c.bg, (2 << 8) | 1)
  461. s.cursor_position(2, 1)
  462. s.select_graphic_rendition(0, 35)
  463. s.draw('b')
  464. c = s.line(1).cursor_from(0)
  465. self.ae(c.fg, (5 << 8) | 1)
  466. self.ae(c.bg, 0)
  467. s.cursor_position(2, 2)
  468. s.select_graphic_rendition(38, 2, 99, 1, 2, 3)
  469. s.draw('c')
  470. c = s.line(1).cursor_from(1)
  471. self.ae(c.fg, (1 << 24) | (2 << 16) | (3 << 8) | 2)
  472. def test_cursor_hidden(self):
  473. s = self.create_screen()
  474. s.toggle_alt_screen()
  475. s.cursor_visible = False
  476. s.toggle_alt_screen()
  477. self.assertFalse(s.cursor_visible)
  478. def test_dirty_lines(self):
  479. s = self.create_screen()
  480. self.assertFalse(s.linebuf.dirty_lines())
  481. s.draw('a' * (s.columns * 2))
  482. self.ae(s.linebuf.dirty_lines(), [0, 1])
  483. self.assertFalse(s.historybuf.dirty_lines())
  484. while not s.historybuf.count:
  485. s.draw('a' * (s.columns * 2))
  486. self.ae(s.historybuf.dirty_lines(), list(range(s.historybuf.count)))
  487. self.ae(s.linebuf.dirty_lines(), list(range(s.lines)))
  488. s.cursor.x, s.cursor.y = 0, 1
  489. s.insert_lines(2)
  490. self.ae(s.linebuf.dirty_lines(), [0, 3, 4])
  491. s.draw('a' * (s.columns * s.lines))
  492. self.ae(s.linebuf.dirty_lines(), list(range(s.lines)))
  493. s.cursor.x, s.cursor.y = 0, 1
  494. s.delete_lines(2)
  495. self.ae(s.linebuf.dirty_lines(), [0, 1, 2])
  496. s = self.create_screen()
  497. self.assertFalse(s.linebuf.dirty_lines())
  498. s.erase_in_line(0, False)
  499. self.ae(s.linebuf.dirty_lines(), [0])
  500. s.index(), s.index()
  501. s.erase_in_display(0, False)
  502. self.ae(s.linebuf.dirty_lines(), [0, 2, 3, 4])
  503. s = self.create_screen()
  504. self.assertFalse(s.linebuf.dirty_lines())
  505. s.insert_characters(2)
  506. self.ae(s.linebuf.dirty_lines(), [0])
  507. s.cursor.y = 1
  508. s.delete_characters(2)
  509. self.ae(s.linebuf.dirty_lines(), [0, 1])
  510. s.cursor.y = 2
  511. s.erase_characters(2)
  512. self.ae(s.linebuf.dirty_lines(), [0, 1, 2])
  513. def test_selection_as_text(self):
  514. s = self.create_screen()
  515. for i in range(2 * s.lines):
  516. if i != 0:
  517. s.carriage_return(), s.linefeed()
  518. s.draw(str(i) * s.columns)
  519. s.start_selection(0, 0)
  520. s.update_selection(4, 4)
  521. def ts(*args):
  522. return ''.join(s.text_for_selection(*args))
  523. expected = ''.join(('55555', '\n66666', '\n77777', '\n88888', '\n99999'))
  524. self.ae(ts(), expected)
  525. s.scroll(2, True)
  526. self.ae(ts(), expected)
  527. s.reset()
  528. s.draw('ab cd')
  529. s.start_selection(0, 0)
  530. s.update_selection(1, 3)
  531. self.ae(ts(), ''.join(('ab ', 'cd')))
  532. self.ae(ts(False, True), ''.join(('ab', 'cd')))
  533. s.reset()
  534. s.draw('ab cd')
  535. s.start_selection(0, 0)
  536. s.update_selection(3, 4)
  537. self.ae(s.text_for_selection(), ('ab ', ' ', 'cd'))
  538. self.ae(s.text_for_selection(False, True), ('ab', '\n', 'cd'))
  539. s.reset()
  540. s.draw('a')
  541. s.select_graphic_rendition(32)
  542. s.draw('b')
  543. s.select_graphic_rendition(39)
  544. s.draw('c xy')
  545. s.start_selection(0, 0)
  546. s.update_selection(1, 3)
  547. self.ae(s.text_for_selection(), ('abc ', 'xy'))
  548. self.ae(s.text_for_selection(True), ('a\x1b[32mb\x1b[39mc ', 'xy', '\x1b[m'))
  549. self.ae(s.text_for_selection(True, True), ('a\x1b[32mb\x1b[39mc', 'xy', '\x1b[m'))
  550. def test_soft_hyphen(self):
  551. s = self.create_screen()
  552. s.draw('a\u00adb')
  553. self.ae(s.cursor.x, 2)
  554. s.start_selection(0, 0)
  555. s.update_selection(2, 0)
  556. self.ae(s.text_for_selection(), ('a\u00adb',))
  557. def test_variation_selectors(self):
  558. s = self.create_screen()
  559. s.draw('\U0001f610')
  560. self.ae(s.cursor.x, 2)
  561. s.carriage_return(), s.linefeed()
  562. s.draw('\U0001f610\ufe0e')
  563. self.ae(s.cursor.x, 1)
  564. s.carriage_return(), s.linefeed()
  565. s.draw('\u25b6')
  566. self.ae(s.cursor.x, 1)
  567. s.carriage_return(), s.linefeed()
  568. s.draw('\u25b6\ufe0f')
  569. self.ae(s.cursor.x, 2)
  570. def test_writing_with_cursor_on_trailer_of_wide_character(self):
  571. s = self.create_screen()
  572. def r(x, pos, expected):
  573. s.reset()
  574. s.draw('😸')
  575. self.ae(s.cursor.x, 2)
  576. s.cursor.x = 1
  577. s.draw(x)
  578. self.ae(s.cursor.x, pos)
  579. self.ae(str(s.line(0)), expected)
  580. r('a', 2, ' a')
  581. r('😸', 3, ' 😸')
  582. r('\u0304', 1, '😸\u0304')
  583. r('\r', 0, '😸')
  584. def test_serialize(self):
  585. from kitty.window import as_text
  586. s = self.create_screen()
  587. parse_bytes(s, b'\x1b[1;91m')
  588. s.draw('X')
  589. parse_bytes(s, b'\x1b[0m\x1b[2m')
  590. s.draw('Y')
  591. self.ae(as_text(s, True), '\x1b[m\x1b[22;1;91mX\x1b[22;2;39mY\n\n\n\n')
  592. s.reset()
  593. s.draw('ab' * s.columns)
  594. s.carriage_return(), s.linefeed()
  595. s.draw('c')
  596. self.ae(as_text(s), 'ababababab\nc\n\n')
  597. self.ae(as_text(s, True), '\x1b[mababa\x1b[mbabab\n\x1b[mc\n\n')
  598. s = self.create_screen(cols=2, lines=2, scrollback=2)
  599. for i in range(1, 7):
  600. s.select_graphic_rendition(30 + i)
  601. s.draw(f'{i}' * s.columns)
  602. self.ae(as_text(s, True, True), '\x1b[m\x1b[31m11\x1b[m\x1b[32m22\x1b[m\x1b[33m33\x1b[m\x1b[34m44\x1b[m\x1b[m\x1b[35m55\x1b[m\x1b[36m66')
  603. def set_link(url=None, id=None):
  604. parse_bytes(s, '\x1b]8;id={};{}\x1b\\'.format(id or '', url or '').encode('utf-8'))
  605. s = self.create_screen()
  606. s.draw('a')
  607. set_link('moo', 'foo')
  608. s.draw('bcdef')
  609. self.ae(as_text(s, True), '\x1b[ma\x1b]8;id=foo;moo\x1b\\bcde\x1b[mf\n\n\n\x1b]8;;\x1b\\')
  610. set_link()
  611. s.draw('gh')
  612. self.ae(as_text(s, True), '\x1b[ma\x1b]8;id=foo;moo\x1b\\bcde\x1b[mf\x1b]8;;\x1b\\gh\n\n\n')
  613. s = self.create_screen()
  614. s.draw('a')
  615. set_link('moo')
  616. s.draw('bcdef')
  617. self.ae(as_text(s, True), '\x1b[ma\x1b]8;;moo\x1b\\bcde\x1b[mf\n\n\n\x1b]8;;\x1b\\')
  618. def test_wrapping_serialization(self):
  619. from kitty.window import as_text
  620. s = self.create_screen(cols=2, lines=2, scrollback=2, options={'scrollback_pager_history_size': 128})
  621. s.draw('ū̀abbccddeefū̀')
  622. self.ae(as_text(s, add_history=True), 'ū̀abbccddeefū̀')
  623. self.assertNotIn('\n', as_text(s, add_history=True, as_ansi=True))
  624. s = self.create_screen(cols=2, lines=2, scrollback=2, options={'scrollback_pager_history_size': 128})
  625. s.draw('1'), s.carriage_return(), s.linefeed()
  626. s.draw('2'), s.carriage_return(), s.linefeed()
  627. s.draw('3'), s.carriage_return(), s.linefeed()
  628. s.draw('4'), s.carriage_return(), s.linefeed()
  629. s.draw('5'), s.carriage_return(), s.linefeed()
  630. s.draw('6'), s.carriage_return(), s.linefeed()
  631. s.draw('7')
  632. self.ae(as_text(s, add_history=True), '1\n2\n3\n4\n5\n6\n7')
  633. s = self.create_screen(cols=2, lines=2, scrollback=2, options={'scrollback_pager_history_size': 128})
  634. s.draw('aabb')
  635. s.cursor.y = 0
  636. s.carriage_return(), s.linefeed()
  637. self.ae(as_text(s, add_history=True), 'aabb')
  638. s = self.create_screen(cols=2, lines=2, scrollback=2, options={'scrollback_pager_history_size': 128})
  639. s.draw('a'), s.carriage_return(), s.linefeed()
  640. s.cursor.y = 0
  641. s.draw('aabb')
  642. self.ae(as_text(s), 'aabb')
  643. s = self.create_screen(cols=2, lines=2, scrollback=2, options={'scrollback_pager_history_size': 128})
  644. s.draw('a😀')
  645. self.ae(as_text(s), 'a😀')
  646. def test_pagerhist(self):
  647. hsz = 8
  648. s = self.create_screen(cols=2, lines=2, scrollback=2, options={'scrollback_pager_history_size': hsz})
  649. def contents():
  650. return s.historybuf.pagerhist_as_text()
  651. def line(i):
  652. q.append('\x1b[m' + f'{i}' * s.columns + '\r')
  653. def w(x):
  654. s.historybuf.pagerhist_write(x)
  655. def test():
  656. expected = ''.join(q)
  657. maxlen = hsz
  658. extra = len(expected) - maxlen
  659. if extra > 0:
  660. expected = expected[extra:]
  661. got = contents()
  662. self.ae(got, expected)
  663. q = []
  664. for i in range(4):
  665. s.draw(f'{i}' * s.columns)
  666. self.ae(contents(), '')
  667. s.draw('4' * s.columns), line(0), test()
  668. s.draw('5' * s.columns), line(1), test()
  669. s.draw('6' * s.columns), line(2), test()
  670. s.draw('7' * s.columns), line(3), test()
  671. s.draw('8' * s.columns), line(4), test()
  672. s.draw('9' * s.columns), line(5), test()
  673. s = self.create_screen(options={'scrollback_pager_history_size': 2048})
  674. text = '\x1b[msoft\r\x1b[mbreak\nnext😼cat'
  675. w(text)
  676. self.ae(contents(), text)
  677. s.historybuf.pagerhist_rewrap(2)
  678. self.ae(contents(), '\x1b[mso\rft\x1b[m\rbr\rea\rk\nne\rxt\r😼\rca\rt')
  679. s = self.create_screen(options={'scrollback_pager_history_size': 8})
  680. w('😼')
  681. self.ae(contents(), '😼')
  682. w('abcd')
  683. self.ae(contents(), '😼abcd')
  684. w('e')
  685. self.ae(contents(), 'abcde')
  686. def test_user_marking(self):
  687. def cells(*a, y=0, mark=3):
  688. return [(x, y, mark) for x in a]
  689. s = self.create_screen()
  690. s.draw('abaa')
  691. s.carriage_return(), s.linefeed()
  692. s.draw('xyxyx')
  693. s.set_marker(marker_from_regex('a', 3))
  694. self.ae(s.marked_cells(), cells(0, 2, 3))
  695. s.set_marker()
  696. self.ae(s.marked_cells(), [])
  697. def mark_x(text):
  698. col = 0
  699. for i, c in enumerate(text):
  700. if c == 'x':
  701. col += 1
  702. yield i, i, col
  703. s.set_marker(marker_from_function(mark_x))
  704. self.ae(s.marked_cells(), [(0, 1, 1), (2, 1, 2), (4, 1, 3)])
  705. s = self.create_screen(lines=5, scrollback=10)
  706. for i in range(15):
  707. s.draw(str(i))
  708. if i != 14:
  709. s.carriage_return(), s.linefeed()
  710. s.set_marker(marker_from_regex(r'\d+', 3))
  711. for i in range(10):
  712. self.assertTrue(s.scroll_to_next_mark())
  713. self.ae(s.scrolled_by, i + 1)
  714. self.ae(s.scrolled_by, 10)
  715. for i in range(10):
  716. self.assertTrue(s.scroll_to_next_mark(0, False))
  717. self.ae(s.scrolled_by, 10 - i - 1)
  718. self.ae(s.scrolled_by, 0)
  719. s = self.create_screen()
  720. s.draw('🐈ab')
  721. s.set_marker(marker_from_regex('🐈', 3))
  722. self.ae(s.marked_cells(), cells(0, 1))
  723. s.set_marker(marker_from_regex('🐈a', 3))
  724. self.ae(s.marked_cells(), cells(0, 1, 2))
  725. s.set_marker(marker_from_regex('a', 3))
  726. self.ae(s.marked_cells(), cells(2))
  727. s = self.create_screen(cols=20)
  728. s.tab()
  729. s.draw('ab')
  730. s.set_marker(marker_from_regex('a', 3))
  731. self.ae(s.marked_cells(), cells(8))
  732. s.set_marker(marker_from_regex('\t', 3))
  733. self.ae(s.marked_cells(), cells(*range(8)))
  734. s = self.create_screen()
  735. s.cursor.x = 2
  736. s.draw('x')
  737. s.cursor.x += 1
  738. s.draw('x')
  739. s.set_marker(marker_from_function(mark_x))
  740. self.ae(s.marked_cells(), [(2, 0, 1), (4, 0, 2)])
  741. def test_hyperlinks(self):
  742. s = self.create_screen()
  743. self.ae(s.line(0).hyperlink_ids(), tuple(0 for x in range(s.columns)))
  744. def set_link(url=None, id=None):
  745. parse_bytes(s, '\x1b]8;id={};{}\x1b\\'.format(id or '', url or '').encode('utf-8'))
  746. set_link('url-a', 'a')
  747. self.ae(s.line(0).hyperlink_ids(), tuple(0 for x in range(s.columns)))
  748. s.draw('a')
  749. self.ae(s.line(0).hyperlink_ids(), (1,) + tuple(0 for x in range(s.columns - 1)))
  750. s.draw('bc')
  751. self.ae(s.line(0).hyperlink_ids(), (1, 1, 1, 0, 0))
  752. set_link()
  753. s.draw('d')
  754. self.ae(s.line(0).hyperlink_ids(), (1, 1, 1, 0, 0))
  755. set_link('url-a', 'a')
  756. s.draw('efg')
  757. self.ae(s.line(0).hyperlink_ids(), (1, 1, 1, 0, 1))
  758. self.ae(s.line(1).hyperlink_ids(), (1, 1, 0, 0, 0))
  759. set_link('url-b')
  760. s.draw('hij')
  761. self.ae(s.line(1).hyperlink_ids(), (1, 1, 2, 2, 2))
  762. set_link()
  763. self.ae({('a:url-a', 1), (':url-b', 2)}, s.hyperlinks_as_set())
  764. s.garbage_collect_hyperlink_pool()
  765. self.ae({('a:url-a', 1), (':url-b', 2)}, s.hyperlinks_as_set())
  766. for i in range(s.lines + 2):
  767. s.linefeed()
  768. s.garbage_collect_hyperlink_pool()
  769. self.ae({('a:url-a', 1), (':url-b', 2)}, s.hyperlinks_as_set())
  770. for i in range(s.lines * 2):
  771. s.linefeed()
  772. s.garbage_collect_hyperlink_pool()
  773. self.assertFalse(s.hyperlinks_as_set())
  774. set_link('url-a', 'x')
  775. s.draw('a')
  776. set_link('url-a', 'y')
  777. s.draw('a')
  778. set_link()
  779. self.ae({('x:url-a', 1), ('y:url-a', 2)}, s.hyperlinks_as_set())
  780. s = self.create_screen()
  781. set_link('u' * 2048)
  782. s.draw('a')
  783. self.ae({(':' + 'u' * 2045, 1)}, s.hyperlinks_as_set())
  784. s = self.create_screen()
  785. set_link('u' * 2048, 'i' * 300)
  786. s.draw('a')
  787. self.ae({('i'*256 + ':' + 'u' * (2045 - 256), 1)}, s.hyperlinks_as_set())
  788. s = self.create_screen()
  789. set_link('1'), s.draw('1')
  790. set_link('2'), s.draw('2')
  791. set_link('3'), s.draw('3')
  792. s.cursor.x = 1
  793. set_link(), s.draw('X')
  794. self.ae(s.line(0).hyperlink_ids(), (1, 0, 3, 0, 0))
  795. self.ae({(':1', 1), (':2', 2), (':3', 3)}, s.hyperlinks_as_set())
  796. s.garbage_collect_hyperlink_pool()
  797. self.ae({(':1', 1), (':3', 2)}, s.hyperlinks_as_set())
  798. set_link('3'), s.draw('3')
  799. self.ae({(':1', 1), (':3', 2)}, s.hyperlinks_as_set())
  800. set_link('4'), s.draw('4')
  801. self.ae({(':1', 1), (':3', 2), (':4', 3)}, s.hyperlinks_as_set())
  802. s = self.create_screen()
  803. set_link('1'), s.draw('1')
  804. set_link('2'), s.draw('2')
  805. set_link('1'), s.draw('1')
  806. self.ae({(':2', 2), (':1', 1)}, s.hyperlinks_as_set())
  807. s = self.create_screen()
  808. set_link('1'), s.draw('12'), set_link(), s.draw('X'), set_link('1'), s.draw('3')
  809. s.linefeed(), s.carriage_return()
  810. s.draw('abc')
  811. s.linefeed(), s.carriage_return()
  812. set_link(), s.draw('Z ')
  813. set_link('1'), s.draw('xyz')
  814. s.linefeed(), s.carriage_return()
  815. set_link('2'), s.draw('Z Z')
  816. self.assertIsNone(s.current_url_text())
  817. self.assertIsNone(s.hyperlink_at(0, 4))
  818. self.assertIsNone(s.current_url_text())
  819. self.ae(s.hyperlink_at(0, 0), '1')
  820. self.ae(s.current_url_text(), '123abcxyz')
  821. self.ae('1', s.hyperlink_at(3, 2))
  822. self.ae(s.current_url_text(), '123abcxyz')
  823. self.ae('2', s.hyperlink_at(1, 3))
  824. self.ae(s.current_url_text(), 'Z Z')
  825. def test_bottom_margin(self):
  826. s = self.create_screen(cols=80, lines=6, scrollback=4)
  827. s.set_margins(0, 5)
  828. for i in range(8):
  829. s.draw(str(i))
  830. s.linefeed()
  831. s.carriage_return()
  832. self.ae(str(s.linebuf), '4\n5\n6\n7\n\n')
  833. self.ae(str(s.historybuf), '3\n2\n1\n0')
  834. def test_top_margin(self):
  835. s = self.create_screen(cols=80, lines=6, scrollback=4)
  836. s.set_margins(2, 6)
  837. for i in range(8):
  838. s.draw(str(i))
  839. s.linefeed()
  840. s.carriage_return()
  841. self.ae(str(s.linebuf), '0\n4\n5\n6\n7\n')
  842. self.ae(str(s.historybuf), '')
  843. def test_top_and_bottom_margin(self):
  844. s = self.create_screen(cols=80, lines=6, scrollback=4)
  845. s.set_margins(2, 5)
  846. for i in range(8):
  847. s.draw(str(i))
  848. s.linefeed()
  849. s.carriage_return()
  850. self.ae(str(s.linebuf), '0\n5\n6\n7\n\n')
  851. self.ae(str(s.historybuf), '')
  852. def test_osc_52(self):
  853. s = self.create_screen()
  854. c = s.callbacks
  855. def send(what: str):
  856. return parse_bytes(s, f'\033]52;p;{what}\a'.encode('ascii'))
  857. def t(q, *expected):
  858. c.clear()
  859. send(q)
  860. del q
  861. t.ex = list(expected)
  862. del expected
  863. try:
  864. self.ae(tuple(map(len, c.cc_buf)), tuple(map(len, t.ex)))
  865. self.ae(c.cc_buf, t.ex)
  866. finally:
  867. del t.ex
  868. t('XYZ', ('p;XYZ', False))
  869. t('a' * VT_PARSER_BUFFER_SIZE, ('p;' + 'a' * (VT_PARSER_BUFFER_SIZE - 8), True), (';' + 'a' * 8, False))
  870. t('', ('p;', False))
  871. t('!', ('p;!', False))
  872. def test_key_encoding_flags_stack(self):
  873. s = self.create_screen()
  874. c = s.callbacks
  875. def w(code, p1='', p2=''):
  876. p = f'{p1}'
  877. if p2:
  878. p += f';{p2}'
  879. return parse_bytes(s, f'\033[{code}{p}u'.encode('ascii'))
  880. def ac(flags):
  881. parse_bytes(s, b'\033[?u')
  882. self.ae(c.wtcbuf, f'\033[?{flags}u'.encode('ascii'))
  883. c.clear()
  884. ac(0)
  885. w('=', 0b1001)
  886. ac(0b1001)
  887. w('=', 0b0011, 2)
  888. ac(0b1011)
  889. w('=', 0b0110, 3)
  890. ac(0b1001)
  891. s.reset()
  892. ac(0)
  893. w('>', 0b0011)
  894. ac(0b0011)
  895. w('=', 0b1111)
  896. ac(0b1111)
  897. w('>', 0b10)
  898. ac(0b10)
  899. w('<')
  900. ac(0b1111)
  901. for i in range(10):
  902. w('<')
  903. ac(0)
  904. s.reset()
  905. for i in range(1, 16):
  906. w('>', i)
  907. ac(15)
  908. w('<'), ac(14), w('<'), ac(13)
  909. def test_color_stack(self):
  910. s = self.create_screen()
  911. c = s.callbacks
  912. def w(code):
  913. return parse_bytes(s, ('\033[' + code).encode('ascii'))
  914. def ac(idx, count):
  915. self.ae(c.wtcbuf, f'\033[{idx};{count}#Q'.encode('ascii'))
  916. c.clear()
  917. # ]]]]]]]]]]]]]]]]}}}}}}}}}}}}}}}}))))))))))))))))))))))
  918. w('#R')
  919. ac(0, 0)
  920. w('#P')
  921. w('#R')
  922. ac(0, 1)
  923. w('10#P')
  924. w('#R')
  925. ac(0, 1)
  926. w('#Q')
  927. w('#R')
  928. ac(0, 0)
  929. for i in range(20):
  930. w('#P')
  931. w('#R')
  932. ac(9, 10)
  933. def test_detect_url(self):
  934. s = self.create_screen(cols=30)
  935. def ae(expected, x=3, y=0):
  936. s.detect_url(x, y)
  937. url = ''.join(s.text_for_marked_url())
  938. self.assertEqual(expected, url)
  939. def t(url, x=0, y=0, before='', after='', expected=''):
  940. s.reset()
  941. s.cursor.x = x
  942. s.cursor.y = y
  943. s.draw(before + url + after)
  944. ae(expected or url, x=x + 1 + len(before), y=y)
  945. t('http://moo.com')
  946. t('http://moo.com/something?else=+&what-')
  947. t('http://moo.com#fragme')
  948. for (st, e) in '() {} [] <>'.split():
  949. t('http://moo.com', before=st, after=e)
  950. for trailer in ')-=':
  951. t('http://moo.com' + trailer)
  952. for trailer in '{}([<>': # )]>
  953. t('http://moo.com', after=trailer)
  954. t('http://moo.com', x=s.columns - 9)
  955. t('https://wraps-by-one-char.com', before='[', after=']')
  956. t('http://[::1]:8080')
  957. t('https://wr[aps-by-one-ch]ar.com')
  958. t('http://[::1]:8080/x', after='[') # ]
  959. t('http://[::1]:8080/x]y34', expected='http://[::1]:8080/x')
  960. t('https://wraps-by-one-char.com[]/x', after='[') # ]
  961. def test_prompt_marking(self):
  962. # ]]]]]]]]]]]]]]]]}}}}}}}}}}}}}}}}))))))))))))))))))))))
  963. def mark_prompt():
  964. parse_bytes(s, b'\033]133;A\007')
  965. def mark_output():
  966. parse_bytes(s, b'\033]133;C\007')
  967. def draw_prompt(x):
  968. mark_prompt(), s.draw(f'$ {x}'), s.carriage_return(), s.index()
  969. def draw_output(n, x='', m=True):
  970. if m:
  971. mark_output()
  972. for i in range(n):
  973. s.draw(f'{i}{x}'), s.index(), s.carriage_return()
  974. s = self.create_screen(cols=5, lines=5, scrollback=15)
  975. draw_output(3, 'oo')
  976. draw_prompt('pp')
  977. mark_output()
  978. s.toggle_alt_screen()
  979. s.resize(s.lines, 2)
  980. s.toggle_alt_screen()
  981. self.assertFalse(str(s.line(s.cursor.y)))
  982. s = self.create_screen()
  983. for i in range(4):
  984. mark_prompt()
  985. s.draw(f'$ {i}')
  986. s.carriage_return()
  987. s.index(), s.index()
  988. self.ae(s.scrolled_by, 0)
  989. self.assertTrue(s.scroll_to_prompt())
  990. self.ae(str(s.visual_line(0)), '$ 1')
  991. self.assertTrue(s.scroll_to_prompt())
  992. self.ae(str(s.visual_line(0)), '$ 0')
  993. self.assertFalse(s.scroll_to_prompt())
  994. self.assertTrue(s.scroll_to_prompt(1))
  995. self.ae(str(s.visual_line(0)), '$ 1')
  996. self.assertTrue(s.scroll_to_prompt(1))
  997. self.ae(str(s.visual_line(0)), '$ 2')
  998. self.assertFalse(s.scroll_to_prompt(1))
  999. s = self.create_screen()
  1000. mark_prompt(), s.draw('$ 0')
  1001. s.carriage_return(), s.index()
  1002. mark_prompt(), s.draw('$ 1')
  1003. for i in range(s.lines):
  1004. s.carriage_return(), s.index()
  1005. s.draw(str(i))
  1006. self.assertTrue(s.scroll_to_prompt())
  1007. self.ae(str(s.visual_line(0)), '$ 1')
  1008. def lco(as_ansi=False, which=0):
  1009. a = []
  1010. if s.cmd_output(which, a.append, as_ansi):
  1011. pht = pagerhist(s, as_ansi=as_ansi, upto_output_start=True)
  1012. if pht:
  1013. a.insert(0, pht)
  1014. return ''.join(a)
  1015. def fco():
  1016. a = []
  1017. s.cmd_output(1, a.append)
  1018. return ''.join(a)
  1019. def lvco():
  1020. a = []
  1021. s.cmd_output(2, a.append)
  1022. return ''.join(a)
  1023. s = self.create_screen()
  1024. s.draw('abcd'), s.index(), s.carriage_return()
  1025. s.draw('12'), s.index(), s.carriage_return()
  1026. self.ae(fco(), '')
  1027. self.ae(lco(), 'abcd\n12\n')
  1028. s = self.create_screen()
  1029. mark_prompt(), s.draw('$ 0')
  1030. s.carriage_return(), s.index()
  1031. mark_output()
  1032. s.draw('abcd'), s.index(), s.carriage_return()
  1033. s.draw('12'), s.index(), s.carriage_return()
  1034. mark_prompt(), s.draw('$ 1')
  1035. self.ae(fco(), 'abcd\n12')
  1036. self.ae(lco(), 'abcd\n12')
  1037. self.ae(lco(as_ansi=True), '\x1b[m\x1b]133;C\x1b\\abcd\n\x1b[m12') # ]]]
  1038. s = self.create_screen(cols=5, lines=5, scrollback=15)
  1039. draw_output(1, 'start', False)
  1040. draw_prompt('0'), draw_output(3)
  1041. draw_prompt('1')
  1042. draw_prompt('2'), draw_output(2, 'x')
  1043. # last cmd output
  1044. # test: find upwards
  1045. self.ae(s.scrolled_by, 0)
  1046. self.ae(lco(), '0x\n1x\n')
  1047. # get output after scroll up
  1048. s.scroll_to_prompt()
  1049. self.ae(s.scrolled_by, 4)
  1050. self.ae(str(s.visual_line(0)), '$ 0')
  1051. self.ae(lco(), '0x\n1x\n')
  1052. # first cmd output on screen
  1053. # test: find around
  1054. self.ae(fco(), '0\n1\n2')
  1055. s.scroll(2, False)
  1056. self.ae(s.scrolled_by, 2)
  1057. self.ae(str(s.visual_line(0)), '1')
  1058. self.ae(fco(), '0x\n1x\n')
  1059. # test: find downwards
  1060. s.scroll(2, False)
  1061. self.ae(str(s.visual_line(0)), '$ 1')
  1062. self.ae(fco(), '0x\n1x\n')
  1063. # resize
  1064. # get last cmd output with continued output mark
  1065. draw_prompt('3'), draw_output(1, 'long_line'), draw_output(2, 'l', False)
  1066. s.resize(4, 5)
  1067. s.scroll_to_prompt(-4)
  1068. self.ae(str(s.visual_line(0)), '$ 0')
  1069. self.ae(lco(), '0long_line\n0l\n1l\n')
  1070. # last visited cmd output
  1071. self.ae(lvco(), '0\n1\n2')
  1072. s.scroll_to_prompt(1)
  1073. self.ae(lvco(), '0x\n1x')
  1074. # last command output without line break
  1075. s = self.create_screen(cols=10, lines=3)
  1076. draw_prompt('p1')
  1077. mark_output(), s.draw('running')
  1078. self.ae(lco(), 'running')
  1079. s.index(), s.carriage_return()
  1080. self.ae(lco(), 'running\n')
  1081. # last command output from pager history
  1082. s = self.create_screen()
  1083. draw_prompt('p1')
  1084. draw_output(30)
  1085. self.ae(tuple(map(int, lco().split())), tuple(range(0, 30)))
  1086. # last non empty command output
  1087. s = self.create_screen()
  1088. draw_prompt('a'), draw_output(2, 'a')
  1089. draw_prompt('b'), mark_output()
  1090. self.ae(lco(), '')
  1091. self.ae(lco(which=3), '0a\n1a')
  1092. s.draw('running'), s.index(), s.carriage_return()
  1093. self.ae(lco(which=3), 'running\n')
  1094. s = self.create_screen()
  1095. draw_prompt('p1')
  1096. draw_output(30)
  1097. self.ae(tuple(map(int, lco(which=3).split())), tuple(range(0, 30)))
  1098. s = self.create_screen()
  1099. draw_prompt('p1')
  1100. draw_output(2, 'a')
  1101. draw_prompt('p1')
  1102. draw_prompt('p1')
  1103. self.ae(lco(which=3), '0a\n1a')
  1104. def test_pointer_shapes(self):
  1105. from kitty.window import set_pointer_shape
  1106. s = self.create_screen()
  1107. c = s.callbacks
  1108. response = ''
  1109. def cb(data):
  1110. nonlocal response
  1111. response = set_pointer_shape(s, str(data, 'utf-8'))
  1112. c.set_pointer_shape = cb
  1113. def send(a):
  1114. nonlocal response
  1115. response = ''
  1116. parse_bytes(s, f'\x1b]22;{a}\x1b\\'.encode())
  1117. return response
  1118. self.ae(send('?__current__'), '0')
  1119. self.ae(send('?__default__,__grabbed__,default,ne-resize,crosshair,XXX'), 'text,default,1,1,1,0')
  1120. def t(q, e=None):
  1121. self.ae(send(q), '')
  1122. self.ae(send('?__current__'), e)
  1123. t('default', 'default')
  1124. s.reset()
  1125. self.ae(send('?__current__'), '0')
  1126. t('=crosshair', 'crosshair')
  1127. t('<', '0')
  1128. t('=crosshair', 'crosshair')
  1129. t('', '0')
  1130. t('>help', 'help')
  1131. t('>wait', 'wait')
  1132. t('<', 'help')
  1133. t('<', '0')
  1134. t('default,help', 'help')
  1135. t('<', '0')
  1136. t('>default,help', 'help')
  1137. t('<', 'default')
  1138. t('<', '0')
  1139. t('=left_ptr', 'default')
  1140. t('=fleur', 'move')
  1141. def test_color_profile(self):
  1142. c = ColorProfile(defaults)
  1143. for i in range(8):
  1144. col = getattr(defaults, f'color{i}')
  1145. self.ae(c.as_color(i << 8 | 1), col)
  1146. self.ae(c.as_color(255 << 8 | 1), Color(0xee, 0xee, 0xee))
  1147. s = self.create_screen()
  1148. s.color_profile.reload_from_opts(defaults)
  1149. def q(send, expected=None):
  1150. s.callbacks.clear()
  1151. parse_bytes(s, b'\x1b]21;' + ';'.join(f'{k}={v}' for k, v in send.items()).encode() + b'\a')
  1152. self.ae(s.callbacks.color_control_responses, [expected] if expected else [])
  1153. q({k: '?' for k in 'background foreground 213 unknown'.split()}, {
  1154. 'background': defaults.background, 'foreground': defaults.foreground, '213': defaults.color213, 'unknown': '?'})
  1155. q({'background':'aquamarine'})
  1156. q({'background':'?', 'selection_background': '?'}, {'background': color_names['aquamarine'], 'selection_background': s.color_profile.highlight_bg})
  1157. q({'selection_background': ''})
  1158. self.assertIsNone(s.color_profile.highlight_bg)
  1159. q({'selection_background': '?'}, {'selection_background': ''})
  1160. s.color_profile.reload_from_opts(defaults)
  1161. q({'transparent_background_color9': '?'}, {'transparent_background_color9': '?'})
  1162. q({'transparent_background_color2': '?'}, {'transparent_background_color2': ''})
  1163. q({'transparent_background_color2': 'red@0.5'})
  1164. q({'transparent_background_color2': '?'}, {'transparent_background_color2': (Color(255, 0, 0), 126)})
  1165. q({'transparent_background_color2': '#ffffff@-1'})
  1166. q({'transparent_background_color2': '?'}, {'transparent_background_color2': (Color(255, 255, 255), 255)})