options.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. #!/usr/bin/env python
  2. # License: GPL v3 Copyright: 2018, Kovid Goyal <kovid at kovidgoyal.net>
  3. import os
  4. import shutil
  5. import tempfile
  6. from kitty.fast_data_types import Color, test_cursor_blink_easing_function
  7. from kitty.options.utils import DELETE_ENV_VAR, EasingFunction, to_color
  8. from kitty.utils import log_error
  9. from . import BaseTest
  10. class TestConfParsing(BaseTest):
  11. def setUp(self):
  12. super().setUp()
  13. self.error_messages = []
  14. log_error.redirect = self.error_messages.append
  15. self.tdir = tempfile.mkdtemp()
  16. def tearDown(self):
  17. del log_error.redirect
  18. shutil.rmtree(self.tdir)
  19. super().tearDown()
  20. def test_conf_parsing(self):
  21. conf_parsing(self)
  22. def conf_parsing(self):
  23. from kitty.config import defaults, load_config
  24. from kitty.constants import is_macos
  25. from kitty.fonts import FontModification, ModificationType, ModificationUnit, ModificationValue
  26. from kitty.options.utils import to_modifiers
  27. bad_lines = []
  28. def p(*lines, bad_line_num=0, num_err=None):
  29. del bad_lines[:]
  30. del self.error_messages[:]
  31. ans = load_config(overrides=lines, accumulate_bad_lines=bad_lines)
  32. if bad_line_num:
  33. self.ae(len(bad_lines), bad_line_num)
  34. else:
  35. self.assertFalse(bad_lines)
  36. if num_err is not None:
  37. self.ae(len(self.error_messages), num_err, '\n'.join(self.error_messages))
  38. return ans
  39. def keys_for_func(opts, name):
  40. for key, defns in opts.keyboard_modes[''].keymap.items():
  41. for action in opts.alias_map.resolve_aliases(defns[0].definition):
  42. if action.func == name:
  43. yield key
  44. opts = p('font_size 11.37', 'clear_all_shortcuts y', 'color23 red')
  45. self.ae(opts.font_size, 11.37)
  46. self.ae(opts.mouse_hide_wait, 0 if is_macos else 3)
  47. self.ae(opts.color23, Color(255, 0, 0))
  48. self.assertFalse(opts.keyboard_modes[''].keymap)
  49. opts = p('clear_all_shortcuts y', 'map f1 next_window')
  50. self.ae(len(opts.keyboard_modes[''].keymap), 1)
  51. opts = p('clear_all_mouse_actions y', 'mouse_map left click ungrabbed mouse_click_url_or_select')
  52. self.ae(len(opts.mousemap), 1)
  53. opts = p('strip_trailing_spaces always')
  54. self.ae(opts.strip_trailing_spaces, 'always')
  55. self.assertFalse(bad_lines)
  56. opts = p('pointer_shape_when_grabbed XXX', bad_line_num=1)
  57. self.ae(opts.pointer_shape_when_grabbed, defaults.pointer_shape_when_grabbed)
  58. opts = p('modify_font underline_position -2', 'modify_font underline_thickness 150%', 'modify_font size Test -1px')
  59. self.ae(opts.modify_font, {
  60. 'underline_position': FontModification(ModificationType.underline_position, ModificationValue(-2., ModificationUnit.pt)),
  61. 'underline_thickness': FontModification(ModificationType.underline_thickness, ModificationValue(150, ModificationUnit.percent)),
  62. 'size:Test': FontModification(ModificationType.size, ModificationValue(-1., ModificationUnit.pixel), 'Test'),
  63. })
  64. # test the aliasing options
  65. opts = p('env A=1', 'env B=x$A', 'env C=', 'env D', 'clear_all_shortcuts y', 'kitten_alias a b --moo', 'map f1 kitten a arg')
  66. self.ae(opts.env, {'A': '1', 'B': 'x1', 'C': '', 'D': DELETE_ENV_VAR})
  67. def ac(which=0):
  68. ka = tuple(opts.keyboard_modes[''].keymap.values())[0][0]
  69. acs = opts.alias_map.resolve_aliases(ka.definition)
  70. return acs[which]
  71. ka = ac()
  72. self.ae(ka.func, 'kitten')
  73. self.ae(ka.args, ('b', '--moo', 'arg'))
  74. opts = p('clear_all_shortcuts y', 'kitten_alias hints hints --hi', 'map f1 kitten hints XXX')
  75. ka = ac()
  76. self.ae(ka.func, 'kitten')
  77. self.ae(ka.args, ('hints', '--hi', 'XXX'))
  78. opts = p('clear_all_shortcuts y', 'action_alias la launch --moo', 'map f1 la XXX')
  79. ka = ac()
  80. self.ae(ka.func, 'launch')
  81. self.ae(ka.args, ('--moo', 'XXX'))
  82. opts = p('clear_all_shortcuts y', 'action_alias one launch --moo', 'action_alias two one recursive', 'map f1 two XXX')
  83. ka = ac()
  84. self.ae(ka.func, 'launch')
  85. self.ae(ka.args, ('--moo', 'recursive', 'XXX'))
  86. opts = p('clear_all_shortcuts y', 'action_alias launch two 1', 'action_alias two launch 2', 'map f1 launch 3')
  87. ka = ac()
  88. self.ae(ka.func, 'launch')
  89. self.ae(ka.args, ('2', '1', '3'))
  90. opts = p('clear_all_shortcuts y', 'action_alias launch launch --moo', 'map f1 launch XXX')
  91. ka = ac()
  92. self.ae(ka.func, 'launch')
  93. self.ae(ka.args, ('--moo', 'XXX'))
  94. opts = p('clear_all_shortcuts y', 'action_alias cfs change_font_size current', 'map f1 cfs +2')
  95. ka = ac()
  96. self.ae(ka.func, 'change_font_size')
  97. self.ae(ka.args, (False, '+', 2.0))
  98. opts = p('clear_all_shortcuts y', 'action_alias la launch --moo', 'map f1 combine : new_window : la ')
  99. self.ae((ac().func, ac(1).func), ('new_window', 'launch'))
  100. opts = p('clear_all_shortcuts y', 'action_alias cc combine : new_window : launch --moo', 'map f1 cc XXX')
  101. self.ae((ac().func, ac(1).func), ('new_window', 'launch'))
  102. self.ae(ac(1).args, ('--moo', 'XXX'))
  103. opts = p('clear_all_shortcuts y', 'action_alias ss kitten "space 1"', 'map f1 ss "space 2"')
  104. self.ae(ac().args, ('space 1', 'space 2'))
  105. opts = p('kitty_mod alt')
  106. self.ae(opts.kitty_mod, to_modifiers('alt'))
  107. self.ae(next(keys_for_func(opts, 'next_layout')).mods, opts.kitty_mod)
  108. # deprecation handling
  109. opts = p('clear_all_shortcuts y', 'send_text all f1 hello')
  110. self.ae(len(opts.keyboard_modes[''].keymap), 1)
  111. opts = p('macos_hide_titlebar y' if is_macos else 'x11_hide_window_decorations y')
  112. self.assertTrue(opts.hide_window_decorations)
  113. self.ae(len(self.error_messages), 1)
  114. # line breaks
  115. opts = p(" font",
  116. " \t \t \\_size",
  117. " \\ 12",
  118. "\\.35",
  119. "col",
  120. "\\o",
  121. "\t \t\\r",
  122. "\\25",
  123. " \\ blue")
  124. self.ae(opts.font_size, 12.35)
  125. self.ae(opts.color25, Color(0, 0, 255))
  126. # cursor_blink_interval
  127. def cb(src, interval=-1, first=EasingFunction(), second=EasingFunction()):
  128. opts = p('cursor_blink_interval ' + src)
  129. self.ae((float(interval), first, second), (float(opts.cursor_blink_interval[0]), opts.cursor_blink_interval[1], opts.cursor_blink_interval[2]))
  130. cb('3', 3)
  131. cb('-2.3', -2.3)
  132. cb('linear', first=EasingFunction('cubic-bezier', cubic_bezier_points=(0, 0.0, 1.0, 1.0)))
  133. cb('linear 19', 19, EasingFunction('cubic-bezier', cubic_bezier_points=(0, 0.0, 1.0, 1.0)))
  134. cb('ease-in-out cubic-bezier(0.1, 0.2, 0.3, 0.4) 11', 11,
  135. EasingFunction('cubic-bezier', cubic_bezier_points=(0.42, 0, 0.58, 1)),
  136. EasingFunction('cubic-bezier', cubic_bezier_points=(0.1, 0.2, 0.3, 0.4))
  137. )
  138. cb('step-start', first=EasingFunction('steps', num_steps=1, jump_type='start'))
  139. cb('steps(7, jump-none)', first=EasingFunction('steps', num_steps=7, jump_type='none'))
  140. cb('linear(0, 0.25, 1)', first=EasingFunction('linear', linear_x=(0.0, 0.5, 1.0), linear_y=(0, 0.25, 1.0)))
  141. cb('linear(0, 0.25 75%, 1)', first=EasingFunction('linear', linear_x=(0.0, 0.75, 1.0), linear_y=(0, 0.25, 1.0)))
  142. cb('linear(0, 0.25 25% 75%, 1)', first=EasingFunction('linear', linear_x=(0.0, 0.25, 0.75, 1.0), linear_y=(0, 0.25, 0.25, 1.0)))
  143. # test that easing functions give expected values
  144. def ef(spec, tests, only_single=True, duration=0.5, accuracy=0):
  145. cfv = p('cursor_blink_interval ' + spec).cursor_blink_interval
  146. self.set_options({'cursor_blink_interval': cfv})
  147. for t, expected in tests.items():
  148. actual = test_cursor_blink_easing_function(t, only_single, duration)
  149. if abs(actual - expected) > accuracy:
  150. self.ae(expected, actual, f'Failed for {spec=} with {t=}: {expected} != {actual}')
  151. ef('linear', {0:1, 0.25: 0.5, 0.5: 0, 0.75: 0.5, 1: 1}, only_single=False)
  152. ef('linear(0, 0.25 25% 75%, 1)', {0: 0, 0.25: 0.25, 0.3: 0.25, 0.75: 0.25, 1:1})
  153. linear_vals = {0: 0, 1: 1, 0.1234: 0.1234, 0.6453: 0.6453}
  154. for spec in ('linear', 'linear(0, 1)', 'cubic-bezier(0, 0, 1, 1)', 'cubic-bezier(0.2, 0.2, 0.7, 0.7)'):
  155. ef(spec, linear_vals)
  156. # test an almost linear function to test cubic bezier implementation
  157. ef('cubic-bezier(0.2, 0.2, 0.7, 0.71)', linear_vals, accuracy=0.01)
  158. ef('cubic-bezier(0.23, 0.2, 0.7, 0.71)', linear_vals, accuracy=0.01)
  159. ef('steps(5)', {0: 0, 0.1: 0, 0.3: 0.2, 0.9:0.8})
  160. ef('steps(5, start)', {0: 0.2, 0.1: 0.2, 0.3: 0.4, 0.9:1})
  161. ef('steps(4, jump-both)', {0: 0.2, 0.1: 0.2, 0.3: 0.4, 0.9:1})
  162. ef('steps(6, jump-none)', {0: 0, 0.1: 0.0, 0.3: 0.2, 0.9:1})
  163. # test various include modes
  164. base = os.path.join(self.tdir, 'glob')
  165. os.mkdir(base)
  166. with open(os.path.join(base, 'fg'), 'w') as f:
  167. print('foreground red', file=f)
  168. opts = p(f'include {f.name}', num_err=0)
  169. self.ae(opts.foreground, to_color('red'))
  170. with open(os.path.join(self.tdir, 'bg'), 'w') as f:
  171. print('background white', file=f)
  172. print('globinclude glob/*', file=f)
  173. print('envinclude ENVINCLUDE', file=f)
  174. print('geninclude g.py', file=f)
  175. print('geninclude g', file=f)
  176. with open(os.path.join(self.tdir, 'g.py'), 'w') as g:
  177. print('print("background_opacity .77")', file=g)
  178. print('print("background_blur 77")', file=g)
  179. with open(os.path.join(self.tdir, 'g'), 'w') as g:
  180. print('#!/bin/sh', file=g)
  181. print('echo background_image_linear y', file=g)
  182. print('echo background_image_layout clamped', file=g)
  183. os.chmod(g.fileno(), 0o700)
  184. os.environ['ENVINCLUDE'] = 'cursor yellow'
  185. opts = p(f'include {f.name}', num_err=0)
  186. os.environ.pop('ENVINCLUDE')
  187. self.ae(opts.foreground, to_color('red'))
  188. self.ae(opts.background, to_color('white'))
  189. self.ae(opts.cursor, to_color('yellow'))
  190. self.ae(opts.background_opacity, .77)
  191. self.ae(opts.background_blur, 77)
  192. self.ae(opts.background_image_linear, True)
  193. self.ae(opts.background_image_layout, 'clamped')
  194. with open(os.path.join(self.tdir, 'a'), 'w') as a:
  195. print('background red', file=a)
  196. print('include b', file=a)
  197. with open(os.path.join(self.tdir, 'b'), 'w') as a:
  198. print('foreground red', file=a)
  199. print('include a', file=a)
  200. opts = p(f'include {a.name}', num_err=0, bad_line_num=1)
  201. self.ae(opts.foreground, to_color('red'))
  202. self.ae(opts.background, to_color('red'))