preview.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import sys
  2. import os
  3. import sys
  4. theme_keys = [
  5. "cursor", "foreground", "background", "background_opacity", "dynamic_background_opacity", "dim_opacity",
  6. "selection_foreground", "selection_background", "color0", "color8", "color1", "color9", "color2", "color10",
  7. "color3", "color11", "color4", "color12", "color5", "color13", "color6", "color14", "color7", "color15"
  8. ]
  9. def is_valid(line):
  10. """
  11. Returns true if a line inside a configuration file is a valid theme configuration pair: is not a comment, is not
  12. empty and the key is correct.
  13. :param line: a line inside the configuration file
  14. :type line: str
  15. :return: true if is valid, false otherwise
  16. :rtype: bool
  17. """
  18. return (not line.lstrip().startswith("#") # is not a comment
  19. and len(line.strip()) != 0 # is not empty
  20. and line.split(maxsplit=1)[0] in theme_keys) # key is a valid one
  21. def extract_configuration_pair(line):
  22. """
  23. Extract a configuration pair by splitting on spaces and taking the first couple of values.
  24. :param line: a line inside the configuration file
  25. :type line: str
  26. :return: a key-value pair
  27. :rtype: bool
  28. """
  29. split = line.split(maxsplit=2)
  30. return split[0], split[1]
  31. def read_configuration(filename):
  32. """
  33. Read a kitty configuration file and extract only theme related keys and values.
  34. :param filename: path to the configuration file
  35. :type filename: str
  36. :return: a map with theme related configuration values
  37. :rtype: dict[str, str]
  38. """
  39. with open(filename, "r") as fp:
  40. lines = fp.readlines()
  41. theme_config = dict([extract_configuration_pair(line) for line in lines if is_valid(line)])
  42. return theme_config
  43. def fg(color, text):
  44. rgb = tuple(int(color[i + 1:i + 3], 16) for i in (0, 2, 4))
  45. return ('\x1b[38;2;%s;%s;%sm' % rgb + text + '\x1b[0m')
  46. def bg(color, text):
  47. rgb = tuple(int(color[i + 1:i + 3], 16) for i in (0, 2, 4))
  48. return ('\x1b[48;2;%s;%s;%sm' % rgb + text + '\x1b[0m')
  49. def print_preview(filename, configuration):
  50. cursor = configuration["cursor"]
  51. background = configuration["background"]
  52. foreground = configuration["foreground"]
  53. theme = os.path.basename(filename)
  54. size = len(theme) + (2 + 2 + 16 + 2 + 16 + 1 + 2)
  55. print(bg(background, " " * size))
  56. print(bg(background, " "), end="")
  57. print(bg(background, fg(foreground, theme)), end="")
  58. print(bg(background, " "), end="")
  59. c='a'
  60. for i in range(0, 16):
  61. color = configuration["color%d" % i]
  62. print(bg(background, fg(color, c)), end="")
  63. c = chr(ord(c) + 1)
  64. print(bg(background, " "), end="")
  65. selection_background = configuration["selection_background"]
  66. selection_foreground = configuration["selection_foreground"]
  67. c='A'
  68. for i in range(0, 16):
  69. print(bg(selection_background, fg(selection_foreground, c)), end="")
  70. c = chr(ord(c) + 1)
  71. print(bg(cursor, " "), end="")
  72. print(bg(background, " "))
  73. print(bg(background, " " * size))
  74. print(bg(background, " "), end="")
  75. print(bg(configuration["color0"], " "), end="")
  76. print(bg(configuration["color1"], " "), end="")
  77. print(bg(configuration["color2"], " "), end="")
  78. print(bg(configuration["color3"], " "), end="")
  79. print(bg(configuration["color4"], " "), end="")
  80. print(bg(configuration["color5"], " "), end="")
  81. print(bg(configuration["color6"], " "), end="")
  82. print(bg(configuration["color7"], " "), end="")
  83. print(bg(background, " "), end="")
  84. print(bg(configuration["color8"], " "), end="")
  85. print(bg(configuration["color9"], " "), end="")
  86. print(bg(configuration["color10"], " "), end="")
  87. print(bg(configuration["color11"], " "), end="")
  88. print(bg(configuration["color12"], " "), end="")
  89. print(bg(configuration["color13"], " "), end="")
  90. print(bg(configuration["color14"], " "), end="")
  91. print(bg(configuration["color15"], " "), end="")
  92. print(bg(background, " " * (size - 16 - 4)), end="")
  93. print()
  94. print(bg(background, " " * size))
  95. print()
  96. def main(directory):
  97. for filename in os.listdir(directory):
  98. try:
  99. path = os.path.join(directory, filename)
  100. configuration = read_configuration(path)
  101. print_preview(path, configuration)
  102. except Exception as e:
  103. print(e, file=sys.stderr)
  104. print("Error while processing %s" % filename, file=sys.stderr)
  105. if __name__ == "__main__":
  106. main(sys.argv[1])