diff --git a/preview.py b/preview.py new file mode 100644 index 0000000..d30a84e --- /dev/null +++ b/preview.py @@ -0,0 +1,99 @@ +import sys + +theme_keys = [ + "cursor", "foreground", "background", "background_opacity", "dynamic_background_opacity", "dim_opacity", + "selection_foreground", "selection_background", "color0", "color8", "color1", "color9", "color2", "color10", + "color3", "color11", "color4", "color12", "color5", "color13", "color6", "color14", "color7", "color15" +] + + +def is_valid(line): + """ + Returns true if a line inside a configuration file is a valid theme configuration pair: is not a comment, is not + empty and the key is correct. + + :param line: a line inside the configuration file + :type line: str + :return: true if is valid, false otherwise + :rtype: bool + """ + return (not line.lstrip().startswith("#") # is not a comment + and len(line.strip()) != 0 # is not empty + and line.split(maxsplit=1)[0] in theme_keys) # key is a valid one + + +def extract_configuration_pair(line): + """ + Extract a configuration pair by splitting on spaces and taking the first couple of values. + + :param line: a line inside the configuration file + :type line: str + :return: a key-value pair + :rtype: bool + """ + split = line.split(maxsplit=2) + return split[0], split[1] + + +def read_configuration(filename): + """ + Read a kitty configuration file and extract only theme related keys and values. + + :param filename: path to the configuration file + :type filename: str + :return: a map with theme related configuration values + :rtype: dict[str, str] + """ + with open(filename, "r") as fp: + lines = fp.readlines() + theme_config = dict([extract_configuration_pair(line) for line in lines if is_valid(line)]) + return theme_config + + +def fg(color, text): + rgb = tuple(int(color[i + 1:i + 3], 16) for i in (0, 2, 4)) + return ('\x1b[38;2;%s;%s;%sm' % rgb + text + '\x1b[0m') + + +def bg(color, text): + rgb = tuple(int(color[i + 1:i + 3], 16) for i in (0, 2, 4)) + return ('\x1b[48;2;%s;%s;%sm' % rgb + text + '\x1b[0m') + + +def print_preview(filename, configuration): + cursor = configuration["cursor"] + background = configuration["background"] + foreground = configuration["foreground"] + + print(bg(background, " "), end="") + print(bg(background, fg(foreground, filename)), end="") + print(bg(background, " "), end="") + + c='a' + for i in range(0, 16): + color = configuration["color%d" % i] + print(bg(background, fg(color, c)), end="") + c = chr(ord(c) + 1) + + print(bg(background, " "), end="") + + selection_background = configuration["selection_background"] + selection_foreground = configuration["selection_foreground"] + + c='A' + for i in range(0, 16): + print(bg(selection_background, fg(selection_foreground, c)), end="") + c = chr(ord(c) + 1) + + print(bg(cursor, " "), end="") + print(bg(background, " "), end="") + print() + + +def main(filename): + configuration = read_configuration(filename) + print_preview(filename, configuration) + + +if __name__ == "__main__": + main(sys.argv[1])