From 1734129dea39e7292910553598fa5f751a819085 Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Thu, 29 Jun 2017 10:54:42 +1000 Subject: [PATCH 1/8] colors: Add support for importing colors in a json format. --- pywal/__main__.py | 3 ++- pywal/set_colors.py | 23 +++++++++++++++-------- pywal/util.py | 8 ++++++++ 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/pywal/__main__.py b/pywal/__main__.py index f9341a5..33a8ee7 100755 --- a/pywal/__main__.py +++ b/pywal/__main__.py @@ -101,8 +101,9 @@ def process_args(args): # -f elif args.f: - colors_plain = util.read_file(args.f) + colors_plain = util.read_file_json(args.f) set_colors.send_sequences(colors_plain, args.t) + quit() export_colors.export_colors(colors_plain) # -o diff --git a/pywal/set_colors.py b/pywal/set_colors.py index 05b217c..b5da855 100755 --- a/pywal/set_colors.py +++ b/pywal/set_colors.py @@ -37,19 +37,26 @@ def set_grey(colors): def send_sequences(colors, vte): """Send colors to all open terminals.""" - sequences = [set_color(num, color) for num, color in enumerate(colors)] - sequences.append(set_special(10, colors[15])) - sequences.append(set_special(11, colors[0])) - sequences.append(set_special(12, colors[15])) - sequences.append(set_special(13, colors[15])) - sequences.append(set_special(14, colors[0])) + + # Colors 0-15. + sequences = [set_color(num, color) + for num, color in enumerate(colors["colors"].values())] + + # Special colors. + sequences.append(set_special(10, colors["special"]["foreground"])) + sequences.append(set_special(11, colors["special"]["background"])) + sequences.append(set_special(12, colors["special"]["cursor"])) + + # TODO: Figure out what these change. + # sequences.append(set_special(13, colors["foreground"])) + # sequences.append(set_special(14, colors["background"])) # Set a blank color that isn"t affected by bold highlighting. - sequences.append(set_color(66, colors[0])) + sequences.append(set_color(66, colors["special"]["background"])) # This escape sequence doesn"t work in VTE terminals. if not vte: - sequences.append(set_special(708, colors[0])) + sequences.append(set_special(708, colors["special"]["background"])) # Get a list of terminals. terminals = [f"/dev/pts/{term}" for term in os.listdir("/dev/pts/") diff --git a/pywal/util.py b/pywal/util.py index 67baa6a..415b612 100755 --- a/pywal/util.py +++ b/pywal/util.py @@ -1,6 +1,7 @@ """ Misc helper functions. """ +import json import os import pathlib import subprocess @@ -13,6 +14,13 @@ def read_file(input_file): return colors +def read_file_json(input_file): + """Read colors from a json file.""" + with open(input_file) as json_file: + colors = json.load(json_file) + return colors + + def save_file(colors, export_file): """Write the colors to the file.""" with open(export_file, "w") as file: From 950400ef7de1b39c2ff1bcecabc0645b993f0b88 Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Thu, 29 Jun 2017 11:16:15 +1000 Subject: [PATCH 2/8] colors: Add json support to exported files. --- pywal/__main__.py | 1 - pywal/format_colors.py | 68 +++++++++++++++++++++++++----------------- 2 files changed, 41 insertions(+), 28 deletions(-) diff --git a/pywal/__main__.py b/pywal/__main__.py index 33a8ee7..8f68387 100755 --- a/pywal/__main__.py +++ b/pywal/__main__.py @@ -103,7 +103,6 @@ def process_args(args): elif args.f: colors_plain = util.read_file_json(args.f) set_colors.send_sequences(colors_plain, args.t) - quit() export_colors.export_colors(colors_plain) # -o diff --git a/pywal/format_colors.py b/pywal/format_colors.py index 5d70ba0..1d0439b 100755 --- a/pywal/format_colors.py +++ b/pywal/format_colors.py @@ -6,20 +6,21 @@ from pywal import util def plain(colors): """Convert colors to plain hex.""" - return [f"{color}\n" for color in colors] + return [f"{color}\n" for color in colors["colors"].values()] def shell(colors): """Convert colors to shell variables.""" return [f"color{index}='{color}'\n" - for index, color in enumerate(colors)] + for index, color in enumerate(colors["colors"].values())] def css(colors): """Convert colors to css variables.""" css_colors = [":root {\n"] css_colors.extend([f"\t--color{index}: {color};\n" - for index, color in enumerate(colors)]) + for index, color in + enumerate(colors["colors"].values())]) css_colors.append("}\n") return css_colors @@ -27,7 +28,7 @@ def css(colors): def scss(colors): """Convert colors to scss variables.""" return [f"$color{index}: {color};\n" - for index, color in enumerate(colors)] + for index, color in enumerate(colors["colors"].values())] def putty(colors): @@ -38,7 +39,8 @@ def putty(colors): "[HKEY_CURRENT_USER\\Software\\SimonTatham\\PuTTY\\Sessions\\Wal]\n", ] putty_colors.extend([f"\"colour{index}\"=\"{rgb(color)}\"\n" - for index, color in enumerate(colors)]) + for index, color in + enumerate(colors["colors"].values())]) return putty_colors @@ -46,33 +48,45 @@ def putty(colors): def xrdb(colors): """Convert colors to xrdb format.""" x_colors = [] - x_colors.append(f"URxvt*foreground: {colors[15]}\n") - x_colors.append(f"XTerm*foreground: {colors[15]}\n") - x_colors.append(f"URxvt*background: {colors[0]}\n") - x_colors.append(f"XTerm*background: {colors[0]}\n") - x_colors.append(f"URxvt*cursorColor: {colors[15]}\n") - x_colors.append(f"XTerm*cursorColor: {colors[15]}\n") + x_colors.append(f"URxvt*foreground: {colors['special']['foreground']}\n") + x_colors.append(f"XTerm*foreground: {colors['special']['foreground']}\n") + x_colors.append(f"URxvt*background: {colors['special']['background']}\n") + x_colors.append(f"XTerm*background: {colors['special']['background']}\n") + x_colors.append(f"URxvt*cursorColor: {colors['special']['cursor']}\n") + x_colors.append(f"XTerm*cursorColor: {colors['special']['cursor']}\n") # Colors 0-15. - x_colors.extend([f"*.color{index}: {color}\n*color{index}: {color}\n" - for index, color in enumerate(colors)]) + x_colors.extend([f"*.color{index}: {color}\n*color{index}: {color}\n" + for index, color in enumerate(colors["colors"].values())]) - x_colors.append(f"*.color66: {colors[0]}\n*color66: {colors[0]}\n") + x_colors.append(f"*.color66: {colors['special']['background']}\n" + f"*color66: {colors['special']['background']}\n") # Rofi colors. - x_colors.append(f"rofi.color-window: {colors[0]}, " - f"{colors[0]}, {colors[10]}\n") - x_colors.append(f"rofi.color-normal: {colors[0]}, " - f"{colors[15]}, {colors[0]}, " - f"{colors[10]}, {colors[0]}\n") - x_colors.append(f"rofi.color-active: {colors[0]}, " - f"{colors[15]}, {colors[0]}, " - f"{colors[10]}, {colors[0]}\n") - x_colors.append(f"rofi.color-urgent: {colors[0]}, " - f"{colors[9]}, {colors[0]}, " - f"{colors[9]}, {colors[15]}\n") + x_colors.append(f"rofi.color-window: " + f"{colors['special']['background']}, " + f"{colors['special']['background']}, " + f"{colors['colors']['color10']}\n") + x_colors.append(f"rofi.color-normal: " + f"{colors['special']['background']}, " + f"{colors['special']['foreground']}, " + f"{colors['special']['background']}, " + f"{colors['colors']['color10']}, " + f"{colors['special']['background']}\n") + x_colors.append(f"rofi.color-active: " + f"{colors['special']['background']}, " + f"{colors['special']['foreground']}, " + f"{colors['special']['background']}, " + f"{colors['colors']['color10']}, " + f"{colors['special']['background']}\n") + x_colors.append(f"rofi.color-urgent: " + f"{colors['special']['background']}, " + f"{colors['colors']['color9']}, " + f"{colors['special']['background']}, " + f"{colors['colors']['color9']}, " + f"{colors['special']['foreground']}\n") # Emacs colors. - x_colors.append(f"emacs*background: {colors[0]}\n") - x_colors.append(f"emacs*foreground: {colors[15]}\n") + x_colors.append(f"emacs*background: {colors['special']['background']}\n") + x_colors.append(f"emacs*foreground: {colors['special']['foreground']}\n") return x_colors From 8a908de48c5175e5eb41fe7d14a54adb50501405 Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Thu, 29 Jun 2017 11:37:27 +1000 Subject: [PATCH 3/8] colors: Store generated colors in a dict to later export to json. --- pywal/__main__.py | 2 +- pywal/gen_colors.py | 20 +++++++++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/pywal/__main__.py b/pywal/__main__.py index 8f68387..2d22f3f 100755 --- a/pywal/__main__.py +++ b/pywal/__main__.py @@ -90,7 +90,7 @@ def process_args(args): # Create a list of hex colors. colors_plain = gen_colors.get_colors(image, args.q) - colors_plain[8] = set_colors.set_grey(colors_plain) + # colors_plain[8] = set_colors.set_grey(colors_plain) if not args.n: wallpaper.set_wallpaper(image) diff --git a/pywal/gen_colors.py b/pywal/gen_colors.py index 3f5e16e..3814819 100755 --- a/pywal/gen_colors.py +++ b/pywal/gen_colors.py @@ -122,4 +122,22 @@ def get_colors(img, quiet): def sort_colors(colors): """Sort the generated colors.""" - return colors[:1] + colors[9:] + colors[8:] + raw_colors = colors[:1] + colors[9:] + colors[8:] + + # 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_hex = {} + [colors_hex.update({f"color{index}": color}) # pylint: disable=W0106 + for index, color in enumerate(raw_colors)] + + # Add the colors to a dict. + colors = {} + colors["special"] = colors_special + colors["colors"] = colors_hex + + return colors From 3b6475be1aad86ac57445c53fca3814ea1d390ce Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Thu, 29 Jun 2017 11:54:04 +1000 Subject: [PATCH 4/8] colors: Use json for all wal generated colors. --- pywal/__main__.py | 1 - pywal/gen_colors.py | 9 +++++++-- pywal/util.py | 6 ++++++ 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/pywal/__main__.py b/pywal/__main__.py index 2d22f3f..c5a4bb8 100755 --- a/pywal/__main__.py +++ b/pywal/__main__.py @@ -90,7 +90,6 @@ def process_args(args): # Create a list of hex colors. colors_plain = gen_colors.get_colors(image, args.q) - # colors_plain[8] = set_colors.set_grey(colors_plain) if not args.n: wallpaper.set_wallpaper(image) diff --git a/pywal/gen_colors.py b/pywal/gen_colors.py index 3814819..593875f 100755 --- a/pywal/gen_colors.py +++ b/pywal/gen_colors.py @@ -9,6 +9,7 @@ import shutil import subprocess from pywal.settings import CACHE_DIR, COLOR_COUNT +from pywal import set_colors from pywal import util @@ -96,9 +97,10 @@ def get_colors(img, quiet): # Cache the sequences file. cache_file = pathlib.Path(CACHE_DIR / "schemes" / img.replace("/", "_")) + cache_file = pathlib.Path(cache_file.with_suffix(".json")) if cache_file.is_file(): - colors = util.read_file(cache_file) + colors = util.read_file_json(cache_file) print("colors: Found cached colorscheme.") else: @@ -111,7 +113,7 @@ def get_colors(img, quiet): colors = sort_colors(colors) # Cache the colorscheme. - util.save_file("\n".join(colors), cache_file) + util.save_file_json(colors, cache_file) print("colors: Generated colorscheme") if not quiet: @@ -135,6 +137,9 @@ def sort_colors(colors): [colors_hex.update({f"color{index}": color}) # pylint: disable=W0106 for index, color in enumerate(raw_colors)] + # Color 8 + colors_hex["color8"] = set_colors.set_grey(raw_colors) + # Add the colors to a dict. colors = {} colors["special"] = colors_special diff --git a/pywal/util.py b/pywal/util.py index 415b612..69a9d9d 100755 --- a/pywal/util.py +++ b/pywal/util.py @@ -27,6 +27,12 @@ def save_file(colors, export_file): file.write(colors) +def save_file_json(colors, export_file): + """Write the colors to a json file.""" + with open(export_file, "w") as file: + json.dump(colors, file, indent=4) + + def create_dir(directory): """Alias to create the cache dir.""" pathlib.Path(directory).mkdir(parents=True, exist_ok=True) From dc8b30218018d190bdee3678b14e721148d691ba Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Thu, 29 Jun 2017 11:56:36 +1000 Subject: [PATCH 5/8] General: comment. --- pywal/gen_colors.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pywal/gen_colors.py b/pywal/gen_colors.py index 593875f..bd044c2 100755 --- a/pywal/gen_colors.py +++ b/pywal/gen_colors.py @@ -123,7 +123,8 @@ def get_colors(img, quiet): def sort_colors(colors): - """Sort the generated 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:] # Special colors. From ede63852249617b71a37887de9969a5022170e5e Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Thu, 29 Jun 2017 12:15:41 +1000 Subject: [PATCH 6/8] tests: Update tests to support the new json config files. --- tests/test_export_colors.py | 4 ++-- tests/test_files/test_file | 17 +---------------- tests/test_files/test_file.json | 26 ++++++++++++++++++++++++++ tests/test_format_colors.py | 14 +++++++------- tests/test_set_colors.py | 13 +++++++------ tests/test_util.py | 24 ++++++++++++++++++++---- 6 files changed, 63 insertions(+), 35 deletions(-) create mode 100644 tests/test_files/test_file.json diff --git a/tests/test_export_colors.py b/tests/test_export_colors.py index 3e87f95..7cee594 100755 --- a/tests/test_export_colors.py +++ b/tests/test_export_colors.py @@ -7,7 +7,7 @@ from pywal import util # Import colors. -COLORS = util.read_file("tests/test_files/test_file") +COLORS = util.read_file_json("tests/test_files/test_file.json") class TestExportColors(unittest.TestCase): @@ -15,7 +15,7 @@ class TestExportColors(unittest.TestCase): def test_save_colors(self): """> Export colors to a file.""" - tmp_file = pathlib.Path("/tmp/test_file") + tmp_file = pathlib.Path("/tmp/test_file.json") export_colors.save_colors(COLORS, tmp_file, "plain colors") result = tmp_file.is_file() self.assertTrue(result) diff --git a/tests/test_files/test_file b/tests/test_files/test_file index 30eeeaa..21106fe 100644 --- a/tests/test_files/test_file +++ b/tests/test_files/test_file @@ -1,16 +1 @@ -#363442 -#99A3B1 -#C5BDB6 -#98AEC2 -#A8B9C6 -#96C4CF -#B7C5CC -#C9CFD0 -#999999 -#99A3B1 -#C5BDB6 -#98AEC2 -#A8B9C6 -#96C4CF -#B7C5CC -#C9CFD0 +/home/dylan/Pictures/Wallpapers/1.jpg diff --git a/tests/test_files/test_file.json b/tests/test_files/test_file.json new file mode 100644 index 0000000..b92c84e --- /dev/null +++ b/tests/test_files/test_file.json @@ -0,0 +1,26 @@ +{ + "special": { + "background":"#3A5130", + "foreground":"#FAF9F5", + "cursor":"#FAF9F5" + }, + + "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" + } +} diff --git a/tests/test_format_colors.py b/tests/test_format_colors.py index ab5946a..732f67b 100755 --- a/tests/test_format_colors.py +++ b/tests/test_format_colors.py @@ -6,7 +6,7 @@ from pywal import util # Import colors. -COLORS = util.read_file("tests/test_files/test_file") +COLORS = util.read_file_json("tests/test_files/test_file.json") class TestFormatColors(unittest.TestCase): @@ -15,32 +15,32 @@ class TestFormatColors(unittest.TestCase): def test_plain(self): """> Convert colors to plain.""" result = format_colors.plain(COLORS) - self.assertEqual(result[0], "#363442\n") + self.assertEqual(result[0], "#3A5130\n") def test_shell(self): """> Convert colors to shell variables.""" result = format_colors.shell(COLORS) - self.assertEqual(result[0], "color0='#363442'\n") + self.assertEqual(result[0], "color0='#3A5130'\n") def test_css(self): """> Convert colors to css variables.""" result = format_colors.css(COLORS) - self.assertEqual(result[1], "\t--color0: #363442;\n") + self.assertEqual(result[1], "\t--color0: #3A5130;\n") def test_scss(self): """> Convert colors to scss variables.""" result = format_colors.scss(COLORS) - self.assertEqual(result[0], "$color0: #363442;\n") + self.assertEqual(result[0], "$color0: #3A5130;\n") def test_putty(self): """> Convert colors to putty theme.""" result = format_colors.putty(COLORS) - self.assertEqual(result[2], "\"colour0\"=\"54,52,66\"\n") + self.assertEqual(result[2], "\"colour0\"=\"58,81,48\"\n") def test_xrdb(self): """> Convert colors to putty theme.""" result = format_colors.xrdb(COLORS) - self.assertEqual(result[6], "*.color0: #363442\n*color0: #363442\n") + self.assertEqual(result[6], "*.color0: #3A5130\n*color0: #3A5130\n") if __name__ == "__main__": diff --git a/tests/test_set_colors.py b/tests/test_set_colors.py index 43c6aed..983e7e6 100755 --- a/tests/test_set_colors.py +++ b/tests/test_set_colors.py @@ -6,7 +6,7 @@ from pywal import util # Import colors. -COLORS = util.read_file("tests/test_files/test_file") +COLORS = util.read_file_json("tests/test_files/test_file.json") class TestSetColors(unittest.TestCase): @@ -14,17 +14,18 @@ class TestSetColors(unittest.TestCase): def test_set_special(self): """> Create special escape sequence.""" - result = set_colors.set_special(11, COLORS[0]) - self.assertEqual(result, "\x1b]11;#363442\x07") + result = set_colors.set_special(11, COLORS["special"]["background"]) + self.assertEqual(result, "\x1b]11;#3A5130\x07") def test_set_color(self): """> Create color escape sequence.""" - result = set_colors.set_color(11, COLORS[0]) - self.assertEqual(result, "\033]4;11;#363442\007") + result = set_colors.set_color(11, COLORS["colors"]["color0"]) + self.assertEqual(result, "\033]4;11;#3A5130\007") def test_set_grey(self): """> Create special escape sequence.""" - result = set_colors.set_grey(COLORS) + colors = [list(COLORS["colors"].values())] + result = set_colors.set_grey(colors[0]) self.assertEqual(result, "#999999") diff --git a/tests/test_util.py b/tests/test_util.py index b1a396b..cf2b716 100755 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -5,18 +5,27 @@ import pathlib from pywal import util +# Import colors. +COLORS = util.read_file_json("tests/test_files/test_file.json") + + class TestUtil(unittest.TestCase): """Test the util functions.""" - def test_read_file_start(self): + def test_read_file(self): """> Read colors from a file.""" result = util.read_file("tests/test_files/test_file") - self.assertEqual(result[0], "#363442") + self.assertEqual(result[0], "/home/dylan/Pictures/Wallpapers/1.jpg") + + 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") def test_read_file_end(self): """> Read colors from a file.""" - result = util.read_file("tests/test_files/test_file") - self.assertEqual(result[15], "#C9CFD0") + result = util.read_file_json("tests/test_files/test_file.json") + self.assertEqual(result["colors"]["color15"], "#FAF9F5") def test_save_file(self): """> Save colors to a file.""" @@ -25,6 +34,13 @@ class TestUtil(unittest.TestCase): result = tmp_file.is_file() self.assertTrue(result) + def test_save_file_json(self): + """> Save colors to a file.""" + tmp_file = pathlib.Path("/tmp/test_file.json") + util.save_file_json(COLORS, tmp_file) + result = tmp_file.is_file() + self.assertTrue(result) + def test_create_dir(self): """> Create a directoru.""" tmp_dir = pathlib.Path("/tmp/test_dir") From a56d0facff42c3fadae879d423530c83c1c1c929 Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Thu, 29 Jun 2017 12:25:59 +1000 Subject: [PATCH 7/8] tests: Update tests to support the new json config files. --- pywal/set_colors.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pywal/set_colors.py b/pywal/set_colors.py index b5da855..da88e45 100755 --- a/pywal/set_colors.py +++ b/pywal/set_colors.py @@ -47,7 +47,7 @@ def send_sequences(colors, vte): sequences.append(set_special(11, colors["special"]["background"])) sequences.append(set_special(12, colors["special"]["cursor"])) - # TODO: Figure out what these change. + # Figure out what these change. # sequences.append(set_special(13, colors["foreground"])) # sequences.append(set_special(14, colors["background"])) From 3283d38e07d4b20ad3a0646d28284fe205fcdc57 Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Thu, 29 Jun 2017 12:33:01 +1000 Subject: [PATCH 8/8] colors: Figured out what the escape sequences do. --- pywal/set_colors.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pywal/set_colors.py b/pywal/set_colors.py index da88e45..6ac53e7 100755 --- a/pywal/set_colors.py +++ b/pywal/set_colors.py @@ -43,13 +43,13 @@ def send_sequences(colors, vte): for num, color in enumerate(colors["colors"].values())] # Special colors. + # http://pod.tst.eu/http://cvs.schmorp.de/rxvt-unicode/doc/rxvt.7.pod#XTerm_Operating_System_Commands + # 10 = foreground, 11 = background, 12 = cursor foregound + # 13 = mouse foreground sequences.append(set_special(10, colors["special"]["foreground"])) sequences.append(set_special(11, colors["special"]["background"])) sequences.append(set_special(12, colors["special"]["cursor"])) - - # Figure out what these change. - # sequences.append(set_special(13, colors["foreground"])) - # sequences.append(set_special(14, colors["background"])) + sequences.append(set_special(13, colors["special"]["cursor"])) # Set a blank color that isn"t affected by bold highlighting. sequences.append(set_color(66, colors["special"]["background"]))