9 from .settings import CONF_DIR, MODULE_DIR
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()]
23 print("\033[1;32mUser Themes\033[0m:")
24 print(" -", "\n - ".join(sorted(user_themes)))
26 print("\033[1;32mDark Themes\033[0m:")
27 print(" -", "\n - ".join(sorted(dark_themes)))
29 print("\033[1;32mLight Themes\033[0m:")
30 print(" -", "\n - ".join(sorted(ligh_themes)))
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)")
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)]
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)]
52 def terminal_sexy_to_wal(data):
53 """Convert terminal.sexy json schema to wal."""
56 "foreground": data["foreground"],
57 "background": data["background"],
58 "cursor": data["color"][9]
61 for i, color in enumerate(data["color"]):
62 data["colors"]["color%s" % i] = color
67 def parse(theme_file):
68 """Parse the theme file."""
69 data = util.read_file_json(theme_file)
71 if "wallpaper" not in data:
72 data["wallpaper"] = "None"
74 if "alpha" not in data:
75 data["alpha"] = util.Color.alpha_num
77 # Terminal.sexy format.
79 data = terminal_sexy_to_wal(data)
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)
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/"))
96 theme_name = ".".join((input_file, "json"))
97 bri = "light" if light else "dark"
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)
102 # Find the theme file.
103 if input_file in ("random", "random_dark"):
104 theme_file = get_random_theme()
106 elif input_file == "random_light":
107 theme_file = get_random_theme(light)
109 elif os.path.isfile(user_theme_file):
110 theme_file = user_theme_file
112 elif os.path.isfile(input_file):
113 theme_file = input_file
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)
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.")