efficient vim config
[dotfiles/.git] / .local / lib / python3.9 / site-packages / pywal / theme.py
1 """
2 Theme file handling.
3 """
4 import logging
5 import os
6 import random
7 import sys
8
9 from .settings import CONF_DIR, MODULE_DIR
10 from . import util
11
12
13 def list_out():
14     """List all themes in a pretty format."""
15     dark_themes = [theme.name.replace(".json", "")
16                    for theme in list_themes()]
17     ligh_themes = [theme.name.replace(".json", "")
18                    for theme in list_themes(dark=False)]
19     user_themes = [theme.name.replace(".json", "")
20                    for theme in list_themes_user()]
21
22     if user_themes:
23         print("\033[1;32mUser Themes\033[0m:")
24         print(" -", "\n - ".join(sorted(user_themes)))
25
26     print("\033[1;32mDark Themes\033[0m:")
27     print(" -", "\n - ".join(sorted(dark_themes)))
28
29     print("\033[1;32mLight Themes\033[0m:")
30     print(" -", "\n - ".join(sorted(ligh_themes)))
31
32     print("\033[1;32mExtra\033[0m:")
33     print(" - random (select a random dark theme)")
34     print(" - random_dark (select a random dark theme)")
35     print(" - random_light (select a random light theme)")
36
37
38 def list_themes(dark=True):
39     """List all installed theme files."""
40     dark = "dark" if dark else "light"
41     themes = os.scandir(os.path.join(MODULE_DIR, "colorschemes", dark))
42     return [t for t in themes if os.path.isfile(t.path)]
43
44
45 def list_themes_user():
46     """List user theme files."""
47     themes = [*os.scandir(os.path.join(CONF_DIR, "colorschemes/dark/")),
48               *os.scandir(os.path.join(CONF_DIR, "colorschemes/light/"))]
49     return [t for t in themes if os.path.isfile(t.path)]
50
51
52 def terminal_sexy_to_wal(data):
53     """Convert terminal.sexy json schema to wal."""
54     data["colors"] = {}
55     data["special"] = {
56         "foreground": data["foreground"],
57         "background": data["background"],
58         "cursor": data["color"][9]
59     }
60
61     for i, color in enumerate(data["color"]):
62         data["colors"]["color%s" % i] = color
63
64     return data
65
66
67 def parse(theme_file):
68     """Parse the theme file."""
69     data = util.read_file_json(theme_file)
70
71     if "wallpaper" not in data:
72         data["wallpaper"] = "None"
73
74     if "alpha" not in data:
75         data["alpha"] = util.Color.alpha_num
76
77     # Terminal.sexy format.
78     if "color" in data:
79         data = terminal_sexy_to_wal(data)
80
81     return data
82
83
84 def get_random_theme(dark=True):
85     """Get a random theme file."""
86     themes = [theme.path for theme in list_themes(dark)]
87     random.shuffle(themes)
88     return themes[0]
89
90
91 def file(input_file, light=False):
92     """Import colorscheme from json file."""
93     util.create_dir(os.path.join(CONF_DIR, "colorschemes/light/"))
94     util.create_dir(os.path.join(CONF_DIR, "colorschemes/dark/"))
95
96     theme_name = ".".join((input_file, "json"))
97     bri = "light" if light else "dark"
98
99     user_theme_file = os.path.join(CONF_DIR, "colorschemes", bri, theme_name)
100     theme_file = os.path.join(MODULE_DIR, "colorschemes", bri, theme_name)
101
102     # Find the theme file.
103     if input_file in ("random", "random_dark"):
104         theme_file = get_random_theme()
105
106     elif input_file == "random_light":
107         theme_file = get_random_theme(light)
108
109     elif os.path.isfile(user_theme_file):
110         theme_file = user_theme_file
111
112     elif os.path.isfile(input_file):
113         theme_file = input_file
114
115     # Parse the theme file.
116     if os.path.isfile(theme_file):
117         logging.info("Set theme to \033[1;37m%s\033[0m.",
118                      os.path.basename(theme_file))
119         return parse(theme_file)
120
121     logging.error("No %s colorscheme file found.", bri)
122     logging.error("Try adding   '-l' to set light themes.")
123     logging.error("Try removing '-l' to set dark themes.")
124     sys.exit(1)