Skip to content

Commit 788533e

Browse files
committed
added features for saving user themes and loading random themes
1 parent 5374071 commit 788533e

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

pywal/__main__.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def get_args():
4545

4646
arg.add_argument("--theme", "-f", metavar="/path/to/file or theme_name",
4747
help="Which colorscheme file to use. \
48-
Use 'wal --theme' to list builtin themes.",
48+
Use 'wal --theme' to list builtin and user themes.",
4949
const="list_themes", nargs="?")
5050

5151
arg.add_argument("--iterative", action="store_true",
@@ -77,6 +77,11 @@ def get_args():
7777
arg.add_argument("-o", metavar="\"script_name\"", action="append",
7878
help="External script to run after \"wal\".")
7979

80+
arg.add_argument("-p", metavar="\"theme_name\"",
81+
help="permanently save theme to "
82+
"$XDG_CONFIG_HOME/wal/colorschemes with "
83+
"the specified name")
84+
8085
arg.add_argument("-q", action="store_true",
8186
help="Quiet mode, don\'t print anything.")
8287

@@ -177,6 +182,9 @@ def parse_args(parser):
177182
if not args.n:
178183
wallpaper.change(colors_plain["wallpaper"])
179184

185+
if args.p:
186+
theme.save(colors_plain, args.p, args.l)
187+
180188
sequences.send(colors_plain, to_send=not args.s, vte_fix=args.vte)
181189

182190
if sys.stdout.isatty():

pywal/theme.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ def list_out():
3333
print(" - random (select a random dark theme)")
3434
print(" - random_dark (select a random dark theme)")
3535
print(" - random_light (select a random light theme)")
36+
print(" - random_user (select a random user theme)")
3637

3738

3839
def list_themes(dark=True):
@@ -88,6 +89,13 @@ def get_random_theme(dark=True):
8889
return themes[0]
8990

9091

92+
def get_random_theme_user():
93+
"""Get a random theme file from user theme directories."""
94+
themes = [theme.path for theme in list_themes_user()]
95+
random.shuffle(themes)
96+
return themes[0]
97+
98+
9199
def file(input_file, light=False):
92100
"""Import colorscheme from json file."""
93101
util.create_dir(os.path.join(CONF_DIR, "colorschemes/light/"))
@@ -106,6 +114,9 @@ def file(input_file, light=False):
106114
elif input_file == "random_light":
107115
theme_file = get_random_theme(light)
108116

117+
elif input_file == "random_user":
118+
theme_file = get_random_theme_user()
119+
109120
elif os.path.isfile(user_theme_file):
110121
theme_file = user_theme_file
111122

@@ -122,3 +133,11 @@ def file(input_file, light=False):
122133
logging.error("Try adding '-l' to set light themes.")
123134
logging.error("Try removing '-l' to set dark themes.")
124135
sys.exit(1)
136+
137+
138+
def save(colors, theme_name, light=False):
139+
"""Save colors to a theme file."""
140+
theme_file = theme_name + ".json"
141+
theme_path = os.path.join(CONF_DIR, "colorschemes",
142+
"light" if light else "dark", theme_file)
143+
util.save_file_json(colors, theme_path)

0 commit comments

Comments
 (0)