config.py 2.3 KB

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