config.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #!./kitty/launcher/kitty +launch
  2. # License: GPLv3 Copyright: 2021, Kovid Goyal <kovid at kovidgoyal.net>
  3. import os
  4. import re
  5. import subprocess
  6. import sys
  7. from kitty.conf.generate import write_output
  8. if __name__ == '__main__' and not __package__:
  9. import __main__
  10. __main__.__package__ = 'gen'
  11. sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  12. def patch_color_list(path: str, colors: list[str], name: str, spc: str = ' ') -> None:
  13. with open(path, 'r+') as f:
  14. raw = f.read()
  15. colors = sorted(colors)
  16. if path.endswith('.go'):
  17. spc = '\t'
  18. nraw = re.sub(
  19. fr'(// {name}_COLORS_START).+?(\s+// {name}_COLORS_END)',
  20. r'\1' + f'\n{spc}' + f'\n{spc}'.join(map(lambda x: f'"{x}":true,', colors)) + r'\2',
  21. raw, flags=re.DOTALL | re.MULTILINE)
  22. else:
  23. nraw = re.sub(
  24. fr'(# {name}_COLORS_START).+?(\s+# {name}_COLORS_END)',
  25. r'\1' + f'\n{spc}' + f'\n{spc}'.join(map(lambda x: f'{x!r},', colors)) + r'\2',
  26. raw, flags=re.DOTALL | re.MULTILINE)
  27. if nraw != raw:
  28. f.seek(0)
  29. f.truncate()
  30. f.write(nraw)
  31. f.flush()
  32. if path.endswith('.go'):
  33. subprocess.check_call(['gofmt', '-w', path])
  34. def main(args: list[str]=sys.argv) -> None:
  35. from kitty.options.definition import definition
  36. nullable_colors = []
  37. all_colors = []
  38. for opt in definition.iter_all_options():
  39. if callable(opt.parser_func):
  40. if opt.parser_func.__name__ in ('to_color_or_none', 'cursor_text_color'):
  41. nullable_colors.append(opt.name)
  42. all_colors.append(opt.name)
  43. elif opt.parser_func.__name__ in ('to_color', 'titlebar_color', 'macos_titlebar_color'):
  44. all_colors.append(opt.name)
  45. patch_color_list('tools/cmd/at/set_colors.go', nullable_colors, 'NULLABLE')
  46. patch_color_list('tools/themes/collection.go', all_colors, 'ALL')
  47. nc = '\n '.join(f'{x!r}' for x in nullable_colors)
  48. write_output('kitty', definition, f'\nnullable_colors = frozenset({{\n {nc}\n}})')
  49. if __name__ == '__main__':
  50. import runpy
  51. m = runpy.run_path(os.path.dirname(os.path.abspath(__file__)))
  52. m['main']([sys.executable, 'config'])