From 07fd6fa6619444c0da248cce96604de972a04720 Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Sun, 9 Jul 2017 20:55:07 +1000 Subject: [PATCH 1/6] export: Add wallpaper to export files, --- pywal/export.py | 4 +++- pywal/magic.py | 12 +++++++----- pywal/templates/colors.css | 5 +++++ pywal/templates/colors.json | 2 ++ pywal/templates/colors.scss | 5 +++++ pywal/templates/colors.sh | 5 +++++ 6 files changed, 27 insertions(+), 6 deletions(-) diff --git a/pywal/export.py b/pywal/export.py index 5e7f2e6..9eea198 100644 --- a/pywal/export.py +++ b/pywal/export.py @@ -35,7 +35,9 @@ def export_all_templates(colors, template_dir=None, output_dir=CACHE_DIR): # Merge all colors (specials and normals) into one dict so we can access # their values simpler. - all_colors = {**colors["special"], **colors["colors"]} + all_colors = {"wallpaper": colors["wallpaper"], + **colors["special"], + **colors["colors"]} # Turn all those colors into util.Color instances for accessing the # .hex and .rgb formats diff --git a/pywal/magic.py b/pywal/magic.py index 3af10ce..0cc868a 100644 --- a/pywal/magic.py +++ b/pywal/magic.py @@ -75,7 +75,7 @@ def get_colors(img, quiet): # Generate the colors. colors = gen_colors(img) - colors = sort_colors(colors) + colors = sort_colors(img, colors) # Cache the colorscheme. util.save_file_json(colors, cache_file) @@ -87,27 +87,29 @@ def get_colors(img, quiet): return colors -def sort_colors(colors): +def sort_colors(img, colors): """Sort the generated colors and store them in a dict that we will later save in json format.""" raw_colors = colors[:1] + colors[9:] + colors[8:] + # Wallpaper. + colors = {"wallpaper": img} + # Special colors. colors_special = {} colors_special.update({"background": raw_colors[0]}) colors_special.update({"foreground": raw_colors[15]}) colors_special.update({"cursor": raw_colors[15]}) - # Colors 0-15 + # Colors 0-15. colors_hex = {} [colors_hex.update({f"color{index}": color}) # pylint: disable=W0106 for index, color in enumerate(raw_colors)] - # Color 8 + # Color 8. colors_hex["color8"] = util.set_grey(raw_colors) # Add the colors to a dict. - colors = {} colors["special"] = colors_special colors["colors"] = colors_hex diff --git a/pywal/templates/colors.css b/pywal/templates/colors.css index 04c2aa9..11e00d3 100644 --- a/pywal/templates/colors.css +++ b/pywal/templates/colors.css @@ -1,9 +1,14 @@ /* CSS variables Generated by 'wal' */ :root {{ + --wallpaper: "{wallpaper}"; + + /* Special */ --background: {background}; --foreground: {foreground}; --cursor: {cursor}; + + /* Colors */ --color0: {color0}; --color1: {color1}; --color2: {color2}; diff --git a/pywal/templates/colors.json b/pywal/templates/colors.json index fdd9828..5c94bc4 100644 --- a/pywal/templates/colors.json +++ b/pywal/templates/colors.json @@ -1,4 +1,6 @@ {{ + "wallpaper": "{wallpaper}", + "special": {{ "background": "{background}", "foreground": "{foreground}", diff --git a/pywal/templates/colors.scss b/pywal/templates/colors.scss index 1a2e183..001be1f 100644 --- a/pywal/templates/colors.scss +++ b/pywal/templates/colors.scss @@ -1,8 +1,13 @@ // SCSS Variables // Generated by 'wal' +$wallpaper: "{wallpaper}"; + +// Special $background: {background}; $foreground: {foreground}; $cursor: {cursor}; + +// Colors $color0: {color0}; $color1: {color1}; $color2: {color2}; diff --git a/pywal/templates/colors.sh b/pywal/templates/colors.sh index 607d19e..19b31c7 100644 --- a/pywal/templates/colors.sh +++ b/pywal/templates/colors.sh @@ -1,8 +1,13 @@ # Shell variables # Generated by 'wal' +wallpaper='{wallpaper}' + +# Special background='{background}' foreground='{foreground}' cursor='{cursor}' + +# Colors color0='{color0}' color1='{color1}' color2='{color2}' From 91fa1e0ca5177587dfb8a8689dbec0aab7f440b8 Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Sun, 9 Jul 2017 23:27:15 +1000 Subject: [PATCH 2/6] export: Add error handling --- pywal/__main__.py | 13 ++++++++----- pywal/wallpaper.py | 3 +++ 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/pywal/__main__.py b/pywal/__main__.py index ff86b00..0be6cc8 100644 --- a/pywal/__main__.py +++ b/pywal/__main__.py @@ -89,20 +89,23 @@ def process_args(args): # -i if args.i: image_file = image.get_image(args.i) - - # Create a list of hex colors. colors_plain = magic.get_colors(image_file, args.q) - if not args.n: - wallpaper.set_wallpaper(image_file) - # -f elif args.f: colors_plain = util.read_file_json(args.f) + # If wallpaper is unset, set it to "None" + if "wallpaper" not in colors_plain: + colors_plain["wallpaper"] = "None" + # -i or -f if args.i or args.f: sequences.send_sequences(colors_plain, args.t) + + if not args.n: + wallpaper.set_wallpaper(colors_plain["wallpaper"]) + export.export_all_templates(colors_plain) reload.reload_env() diff --git a/pywal/wallpaper.py b/pywal/wallpaper.py index 59f1aeb..5e524a3 100644 --- a/pywal/wallpaper.py +++ b/pywal/wallpaper.py @@ -82,6 +82,9 @@ def set_desktop_wallpaper(desktop, img): def set_wallpaper(img): """Set the wallpaper.""" + if not os.path.isfile(img): + return + desktop = get_desktop_env() if desktop: From dbea2814836e03fac6cf5a314d375f30cb7a12e8 Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Sun, 9 Jul 2017 23:32:26 +1000 Subject: [PATCH 3/6] export: Add error handling --- pywal/__main__.py | 1 + pywal/wallpaper.py | 3 --- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/pywal/__main__.py b/pywal/__main__.py index 0be6cc8..a816360 100644 --- a/pywal/__main__.py +++ b/pywal/__main__.py @@ -98,6 +98,7 @@ def process_args(args): # If wallpaper is unset, set it to "None" if "wallpaper" not in colors_plain: colors_plain["wallpaper"] = "None" + args.n = True # -i or -f if args.i or args.f: diff --git a/pywal/wallpaper.py b/pywal/wallpaper.py index 5e524a3..59f1aeb 100644 --- a/pywal/wallpaper.py +++ b/pywal/wallpaper.py @@ -82,9 +82,6 @@ def set_desktop_wallpaper(desktop, img): def set_wallpaper(img): """Set the wallpaper.""" - if not os.path.isfile(img): - return - desktop = get_desktop_env() if desktop: From 1d63b05e408d9f63e0147cdd9b1988ef63100eb9 Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Sun, 9 Jul 2017 23:39:35 +1000 Subject: [PATCH 4/6] export: Add wallpaper to export files, --- pywal/__main__.py | 5 ----- pywal/util.py | 5 +++++ pywal/wallpaper.py | 3 +++ 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/pywal/__main__.py b/pywal/__main__.py index a816360..cdd5001 100644 --- a/pywal/__main__.py +++ b/pywal/__main__.py @@ -95,11 +95,6 @@ def process_args(args): elif args.f: colors_plain = util.read_file_json(args.f) - # If wallpaper is unset, set it to "None" - if "wallpaper" not in colors_plain: - colors_plain["wallpaper"] = "None" - args.n = True - # -i or -f if args.i or args.f: sequences.send_sequences(colors_plain, args.t) diff --git a/pywal/util.py b/pywal/util.py index 2796d8e..6c5b420 100644 --- a/pywal/util.py +++ b/pywal/util.py @@ -54,6 +54,11 @@ def read_file_json(input_file): """Read data from a json file.""" with open(input_file) as json_file: data = json.load(json_file) + + # If wallpaper is unset, set it to "None" + if "wallpaper" not in data: + data["wallpaper"] = "None" + return data diff --git a/pywal/wallpaper.py b/pywal/wallpaper.py index 59f1aeb..5e524a3 100644 --- a/pywal/wallpaper.py +++ b/pywal/wallpaper.py @@ -82,6 +82,9 @@ def set_desktop_wallpaper(desktop, img): def set_wallpaper(img): """Set the wallpaper.""" + if not os.path.isfile(img): + return + desktop = get_desktop_env() if desktop: From b5806287d303fe0ee36e5930c24f488b08c3c441 Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Sun, 9 Jul 2017 23:57:53 +1000 Subject: [PATCH 5/6] export: Add wallpaper to export files, --- tests/test_export.py | 6 ++--- tests/test_files/test_file.json | 43 +++++++++++++++++---------------- tests/test_sequences.py | 4 +-- tests/test_util.py | 11 ++++++--- 4 files changed, 35 insertions(+), 29 deletions(-) diff --git a/tests/test_export.py b/tests/test_export.py index 7204f48..167d3ed 100755 --- a/tests/test_export.py +++ b/tests/test_export.py @@ -27,9 +27,9 @@ class TestExportColors(unittest.TestCase): self.assertTrue(result) content = pathlib.Path("/tmp/test_template").read_text() - self.assertEqual(content, '\n'.join(["test1 #3A5130", - "test2 #3A5130", - "test3 58,81,48", ""])) + self.assertEqual(content, '\n'.join(["test1 #1F211E", + "test2 #1F211E", + "test3 31,33,30", ""])) if __name__ == "__main__": diff --git a/tests/test_files/test_file.json b/tests/test_files/test_file.json index b92c84e..c555eda 100644 --- a/tests/test_files/test_file.json +++ b/tests/test_files/test_file.json @@ -1,26 +1,27 @@ { - "special": { - "background":"#3A5130", - "foreground":"#FAF9F5", - "cursor":"#FAF9F5" - }, + "wallpaper": "5.png", + "special": { + "background": "#1F211E", + "foreground": "#F5F1F4", + "cursor": "#F5F1F4" + }, "colors": { - "color0":"#3A5130", - "color1":"#E3A19D", - "color2":"#E1CEAE", - "color3":"#D6DDCC", - "color4":"#F1D2CB", - "color5":"#F5E9D6", - "color6":"#F9F0E5", - "color7":"#FAF9F5", - "color8":"#999999", - "color9":"#E3A19D", - "color10":"#E1CEAE", - "color11":"#D6DDCC", - "color12":"#F1D2CB", - "color13":"#F5E9D6", - "color14":"#F9F0E5", - "color15":"#FAF9F5" + "color0": "#1F211E", + "color1": "#4B7A85", + "color2": "#CC6A93", + "color3": "#5C9894", + "color4": "#A0A89B", + "color5": "#D1B9A9", + "color6": "#E3D6D8", + "color7": "#F5F1F4", + "color8": "#666666", + "color9": "#4B7A85", + "color10": "#CC6A93", + "color11": "#5C9894", + "color12": "#A0A89B", + "color13": "#D1B9A9", + "color14": "#E3D6D8", + "color15": "#F5F1F4" } } diff --git a/tests/test_sequences.py b/tests/test_sequences.py index f4cbb36..44a98ce 100755 --- a/tests/test_sequences.py +++ b/tests/test_sequences.py @@ -15,12 +15,12 @@ class Testsequences(unittest.TestCase): def test_set_special(self): """> Create special escape sequence.""" result = sequences.set_special(11, COLORS["special"]["background"]) - self.assertEqual(result, "\x1b]11;#3A5130\x07") + self.assertEqual(result, "\x1b]11;#1F211E\x07") def test_set_color(self): """> Create color escape sequence.""" result = sequences.set_color(11, COLORS["colors"]["color0"]) - self.assertEqual(result, "\033]4;11;#3A5130\007") + self.assertEqual(result, "\033]4;11;#1F211E\007") if __name__ == "__main__": diff --git a/tests/test_util.py b/tests/test_util.py index 9cb908a..841c242 100755 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -16,7 +16,7 @@ class TestUtil(unittest.TestCase): """> Get grey color based on brightness of color0""" colors = [list(COLORS["colors"].values())] result = util.set_grey(colors[0]) - self.assertEqual(result, "#999999") + self.assertEqual(result, "#666666") def test_read_file(self): """> Read colors from a file.""" @@ -26,12 +26,17 @@ class TestUtil(unittest.TestCase): def test_read_file_start(self): """> Read colors from a file.""" result = util.read_file_json("tests/test_files/test_file.json") - self.assertEqual(result["colors"]["color0"], "#3A5130") + self.assertEqual(result["colors"]["color0"], "#1F211E") def test_read_file_end(self): """> Read colors from a file.""" result = util.read_file_json("tests/test_files/test_file.json") - self.assertEqual(result["colors"]["color15"], "#FAF9F5") + self.assertEqual(result["colors"]["color15"], "#F5F1F4") + + def test_read_wallpaper(self): + """> Read wallpaper from json file.""" + result = util.read_file_json("tests/test_files/test_file.json") + self.assertEqual(result["wallpaper"], "5.png") def test_save_file(self): """> Save colors to a file.""" From c38c89ece1d41942fa322c555993e50e5c515e41 Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Mon, 10 Jul 2017 00:00:54 +1000 Subject: [PATCH 6/6] export: Add wallpaper to export files, --- tests/test_sequences.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_sequences.py b/tests/test_sequences.py index 44a98ce..a1ea10c 100755 --- a/tests/test_sequences.py +++ b/tests/test_sequences.py @@ -15,7 +15,7 @@ class Testsequences(unittest.TestCase): def test_set_special(self): """> Create special escape sequence.""" result = sequences.set_special(11, COLORS["special"]["background"]) - self.assertEqual(result, "\x1b]11;#1F211E\x07") + self.assertEqual(result, "\033]11;#1F211E\007") def test_set_color(self): """> Create color escape sequence."""