From c0ee7985f3c9ad5414f9eac56143c3dedcdc32cd Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Thu, 29 Jun 2017 17:25:16 +1000 Subject: colors: Use templates. --- pywal/__main__.py | 4 +- pywal/export_colors.py | 66 +++++++++++---------------------- pywal/format_colors.py | 92 ---------------------------------------------- pywal/settings.py | 2 + pywal/templates/colors.css | 20 ++++++++++ 5 files changed, 46 insertions(+), 138 deletions(-) delete mode 100755 pywal/format_colors.py create mode 100644 pywal/templates/colors.css diff --git a/pywal/__main__.py b/pywal/__main__.py index c5a4bb8..7b516aa 100755 --- a/pywal/__main__.py +++ b/pywal/__main__.py @@ -96,13 +96,13 @@ def process_args(args): # Set the colors. set_colors.send_sequences(colors_plain, args.t) - export_colors.export_colors(colors_plain) + export_colors.export_all_templates(colors_plain) # -f elif args.f: colors_plain = util.read_file_json(args.f) set_colors.send_sequences(colors_plain, args.t) - export_colors.export_colors(colors_plain) + export_colors.export_all_templates(colors_plain) # -o if args.o: diff --git a/pywal/export_colors.py b/pywal/export_colors.py index 3a89d22..6d51ca6 100755 --- a/pywal/export_colors.py +++ b/pywal/export_colors.py @@ -1,56 +1,34 @@ """ Export colors in various formats. """ -import shutil -import subprocess +import os +import pathlib -from pywal.settings import CACHE_DIR -from pywal import util -from pywal import format_colors +from pywal.settings import CACHE_DIR, TEMPLATE_DIR -def save_colors(colors, export_file, message): - """Export colors to var format.""" - colors = "".join(colors) - util.save_file(colors, CACHE_DIR / export_file) - print(f"export: exported {message}.") +def template(colors, input_file): + """Read template file, substitute markers and + save the file elsewhere.""" + template_file = pathlib.Path(TEMPLATE_DIR).joinpath(input_file) + export_file = pathlib.Path(CACHE_DIR).joinpath(input_file) + # Import the template. + with open(template_file) as file: + template_data = file.readlines() -def reload_xrdb(export_file): - """Merge the colors into the X db so new terminals use them.""" - if shutil.which("xrdb"): - subprocess.call(["xrdb", "-merge", CACHE_DIR / export_file]) + # Merge both dicts. + colors["colors"].update(colors["special"]) + # Format the markers. + template_data = "".join(template_data).format(**colors["colors"]) -def reload_i3(): - """Reload i3 colors.""" - if shutil.which("i3-msg"): - util.disown("i3-msg", "reload") + # Export the template. + with open(export_file, "w") as file: + file.write(template_data) -def export_colors(colors): - """Export colors in various formats.""" - plain_colors = format_colors.plain(colors) - save_colors(plain_colors, "colors", "plain hex colors") - - # Shell based colors. - shell_colors = format_colors.shell(colors) - save_colors(shell_colors, "colors.sh", "shell variables") - - # Web based colors. - css_colors = format_colors.css(colors) - save_colors(css_colors, "colors.css", "css variables") - scss_colors = format_colors.scss(colors) - save_colors(scss_colors, "colors.scss", "scss variables") - - # Text editor based colors. - putty_colors = format_colors.putty(colors) - save_colors(putty_colors, "colors-putty.reg", "putty theme") - - # X based colors. - xrdb_colors = format_colors.xrdb(colors) - save_colors(xrdb_colors, "xcolors", "xrdb colors") - - # i3 colors. - reload_xrdb("xcolors") - reload_i3() +def export_all_templates(colors): + """Export all template files.""" + # pylint: disable=W0106 + [template(colors, file.name) for file in os.scandir(TEMPLATE_DIR)] diff --git a/pywal/format_colors.py b/pywal/format_colors.py deleted file mode 100755 index 1d0439b..0000000 --- a/pywal/format_colors.py +++ /dev/null @@ -1,92 +0,0 @@ -""" -Convert colors to various formats. -""" -from pywal import util - - -def plain(colors): - """Convert colors to plain hex.""" - 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["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["colors"].values())]) - css_colors.append("}\n") - return css_colors - - -def scss(colors): - """Convert colors to scss variables.""" - return [f"$color{index}: {color};\n" - for index, color in enumerate(colors["colors"].values())] - - -def putty(colors): - """Convert colors to putty theme.""" - rgb = util.hex_to_rgb - putty_colors = [ - "Windows Registry Editor Version 5.00\n\n", - "[HKEY_CURRENT_USER\\Software\\SimonTatham\\PuTTY\\Sessions\\Wal]\n", - ] - putty_colors.extend([f"\"colour{index}\"=\"{rgb(color)}\"\n" - for index, color in - enumerate(colors["colors"].values())]) - - return putty_colors - - -def xrdb(colors): - """Convert colors to xrdb format.""" - x_colors = [] - 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["colors"].values())]) - - x_colors.append(f"*.color66: {colors['special']['background']}\n" - f"*color66: {colors['special']['background']}\n") - - # Rofi colors. - 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['special']['background']}\n") - x_colors.append(f"emacs*foreground: {colors['special']['foreground']}\n") - return x_colors diff --git a/pywal/settings.py b/pywal/settings.py index d94b7eb..d920103 100755 --- a/pywal/settings.py +++ b/pywal/settings.py @@ -1,6 +1,7 @@ """ Global Constants. """ +import os import pathlib @@ -10,3 +11,4 @@ __version__ = "0.2.6" # Internal variables. COLOR_COUNT = 16 CACHE_DIR = pathlib.Path.home() / ".cache/wal/" +TEMPLATE_DIR = os.path.join(os.path.dirname(__file__), "templates") diff --git a/pywal/templates/colors.css b/pywal/templates/colors.css new file mode 100644 index 0000000..439bbad --- /dev/null +++ b/pywal/templates/colors.css @@ -0,0 +1,20 @@ +/* CSS variables + Generated by 'wal' */ +:root {{ + --color0: {color0}; + --color1: {color1}; + --color2: {color2}; + --color3: {color3}; + --color4: {color4}; + --color5: {color5}; + --color6: {color6}; + --color7: {color7}; + --color8: {color8}; + --color9: {color9}; + --color10: {color10}; + --color11: {color11}; + --color12: {color12}; + --color13: {color13}; + --color14: {color14}; + --color15: {color15}; +}} -- cgit v1.2.3 From 6d560f68d3a6693500d94cc64693b40da6574db2 Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Thu, 29 Jun 2017 17:33:49 +1000 Subject: templates: Added templates for scss and sh. --- pywal/templates/colors.scss | 18 ++++++++++++++++++ pywal/templates/colors.sh | 18 ++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 pywal/templates/colors.scss create mode 100644 pywal/templates/colors.sh diff --git a/pywal/templates/colors.scss b/pywal/templates/colors.scss new file mode 100644 index 0000000..c2dc797 --- /dev/null +++ b/pywal/templates/colors.scss @@ -0,0 +1,18 @@ +// SCSS Variables +// Generated by 'wal' +$color0: {color0}; +$color1: {color1}; +$color2: {color2}; +$color3: {color3}; +$color4: {color4}; +$color5: {color5}; +$color6: {color6}; +$color7: {color7}; +$color8: {color8}; +$color9: {color9}; +$color10: {color10}; +$color11: {color11}; +$color12: {color12}; +$color13: {color13}; +$color14: {color14}; +$color15: {color15}; diff --git a/pywal/templates/colors.sh b/pywal/templates/colors.sh new file mode 100644 index 0000000..1362da2 --- /dev/null +++ b/pywal/templates/colors.sh @@ -0,0 +1,18 @@ +# Shell variables +# Generated by 'wal' +color0='{color0}' +color1='{color1}' +color2='{color2}' +color3='{color3}' +color4='{color4}' +color5='{color5}' +color6='{color6}' +color7='{color7}' +color8='{color8}' +color9='{color9}' +color10='{color10}' +color11='{color11}' +color12='{color12}' +color13='{color13}' +color14='{color14}' +color15='{color15}' -- cgit v1.2.3 From 72b0a22303d2f890a9153ea65f5a3c53c757e610 Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Thu, 29 Jun 2017 17:42:59 +1000 Subject: templates: Added template for x colors. --- pywal/templates/colors.Xresources | 53 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 pywal/templates/colors.Xresources diff --git a/pywal/templates/colors.Xresources b/pywal/templates/colors.Xresources new file mode 100644 index 0000000..f213d57 --- /dev/null +++ b/pywal/templates/colors.Xresources @@ -0,0 +1,53 @@ +URxvt*foreground: {foreground} +XTerm*foreground: {foreground} +URxvt*background: {background} +XTerm*background: {background} +URxvt*cursorColor: {cursor} +XTerm*cursorColor: {cursor} + +*.color0: {color0} +*color0: {color0} +*.color1: {color1} +*color1: {color1} +*.color2: {color2} +*color2: {color2} +*.color3: {color3} +*color3: {color3} +*.color4: {color4} +*color4: {color4} +*.color5: {color5} +*color5: {color5} +*.color6: {color6} +*color6: {color6} +*.color7: {color7} +*color7: {color7} +*.color8: {color8} +*color8: {color8} +*.color9: {color9} +*color9: {color9} +*.color10: {color10} +*color10: {color10} +*.color11: {color11} +*color11: {color11} +*.color12: {color12} +*color12: {color12} +*.color13: {color13} +*color13: {color13} +*.color14: {color14} +*color14: {color14} +*.color15: {color15} +*color15: {color15} + +! Black color that will not be affected by bold highlighting. +*.color66: {color0} +*color66: {color0} + +! Rofi colors. +rofi.color-window: {background}, {background}, {color10} +rofi.color-normal: {background}, {foreground}, {background}, {color10}, {background} +rofi.color-active: {background}, {foreground}, {background}, {color10}, {background} +rofi.color-urgent: {background}, {color9}, {background}, {color9}, {foreground} + +! Emacs colors. +emacs*background: {background} +emacs*foreground: {foreground} -- cgit v1.2.3 From 35920c71640e30dd818db971890381f35705ef75 Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Thu, 29 Jun 2017 17:44:05 +1000 Subject: templates: Added template for x colors. --- pywal/templates/colors.Xresources | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pywal/templates/colors.Xresources b/pywal/templates/colors.Xresources index f213d57..baeddf0 100644 --- a/pywal/templates/colors.Xresources +++ b/pywal/templates/colors.Xresources @@ -1,3 +1,5 @@ +! X colors +! Generated by 'wal' URxvt*foreground: {foreground} XTerm*foreground: {foreground} URxvt*background: {background} -- cgit v1.2.3 From e92bce4fe932ea9e13abb6c01a9685bb3bada73b Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Thu, 29 Jun 2017 18:12:41 +1000 Subject: template: Add initial putty template. --- pywal/export_colors.py | 12 +++++++----- pywal/templates/colors-putty.reg | 19 +++++++++++++++++++ 2 files changed, 26 insertions(+), 5 deletions(-) create mode 100644 pywal/templates/colors-putty.reg diff --git a/pywal/export_colors.py b/pywal/export_colors.py index 6d51ca6..9a3d958 100755 --- a/pywal/export_colors.py +++ b/pywal/export_colors.py @@ -5,6 +5,7 @@ import os import pathlib from pywal.settings import CACHE_DIR, TEMPLATE_DIR +from pywal import util def template(colors, input_file): @@ -17,11 +18,8 @@ def template(colors, input_file): with open(template_file) as file: template_data = file.readlines() - # Merge both dicts. - colors["colors"].update(colors["special"]) - # Format the markers. - template_data = "".join(template_data).format(**colors["colors"]) + template_data = "".join(template_data).format(**colors) # Export the template. with open(export_file, "w") as file: @@ -30,5 +28,9 @@ def template(colors, input_file): def export_all_templates(colors): """Export all template files.""" + # Merge both dicts. + colors["colors"].update(colors["special"]) + # pylint: disable=W0106 - [template(colors, file.name) for file in os.scandir(TEMPLATE_DIR)] + [template(colors["colors"], file.name) + for file in os.scandir(TEMPLATE_DIR)] diff --git a/pywal/templates/colors-putty.reg b/pywal/templates/colors-putty.reg new file mode 100644 index 0000000..e8a5b6c --- /dev/null +++ b/pywal/templates/colors-putty.reg @@ -0,0 +1,19 @@ +Windows Registry Editor Version 5.00 + +[HKEY_CURRENT_USER\Software\SimonTatham\PuTTY\Sessions\Default%20Settings]] +"colour0"="{color0}" +"colour1"="{color1}" +"colour2"="{color2}" +"colour3"="{color3}" +"colour4"="{color4}" +"colour5"="{color5}" +"colour6"="{color6}" +"colour7"="{color7}" +"colour8"="{color8}" +"colour9"="{color9}" +"colour10"="{color10}" +"colour11"="{color11}" +"colour12"="{color12}" +"colour13"="{color13}" +"colour14"="{color14}" +"colour15"="{color15}" -- cgit v1.2.3 From 1541af9052d9c12fb3d23832838fce69fcc02761 Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Thu, 29 Jun 2017 18:52:44 +1000 Subject: colors: Convert colors to rgb for putty. --- pywal/export_colors.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/pywal/export_colors.py b/pywal/export_colors.py index 9a3d958..90b3b8f 100755 --- a/pywal/export_colors.py +++ b/pywal/export_colors.py @@ -28,9 +28,21 @@ def template(colors, input_file): def export_all_templates(colors): """Export all template files.""" + # Exclude these templates from the loop. + # The excluded templates need color + # conversion or other intervention. + exclude = ["colors-putty.reg"] + # Merge both dicts. colors["colors"].update(colors["special"]) + # Convert colors to other format. + colors_rgb = {k: util.hex_to_rgb(v) for k, v in colors["colors"].items()} + # pylint: disable=W0106 [template(colors["colors"], file.name) - for file in os.scandir(TEMPLATE_DIR)] + for file in os.scandir(TEMPLATE_DIR) + if file not in exclude] + + # Call 'putty' manually since it needs RGB colors. + template(colors_rgb, "colors-putty.reg") -- cgit v1.2.3 From ad9f62e9b844e4828240861cd59b8f9288118ca7 Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Thu, 29 Jun 2017 18:55:41 +1000 Subject: general: Added back printing of export. --- pywal/export_colors.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pywal/export_colors.py b/pywal/export_colors.py index 90b3b8f..eb865fa 100755 --- a/pywal/export_colors.py +++ b/pywal/export_colors.py @@ -25,6 +25,8 @@ def template(colors, input_file): with open(export_file, "w") as file: file.write(template_data) + print(f"export: Exported {input_file}.") + def export_all_templates(colors): """Export all template files.""" -- cgit v1.2.3 From 4ec8c16d0afce5ae9245eec25ca234ba11841df1 Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Thu, 29 Jun 2017 18:56:56 +1000 Subject: general: comments. --- pywal/export_colors.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pywal/export_colors.py b/pywal/export_colors.py index eb865fa..4282124 100755 --- a/pywal/export_colors.py +++ b/pywal/export_colors.py @@ -35,7 +35,8 @@ def export_all_templates(colors): # conversion or other intervention. exclude = ["colors-putty.reg"] - # Merge both dicts. + # Merge both dicts so we can access their + # values simpler. colors["colors"].update(colors["special"]) # Convert colors to other format. @@ -46,5 +47,6 @@ def export_all_templates(colors): for file in os.scandir(TEMPLATE_DIR) if file not in exclude] - # Call 'putty' manually since it needs RGB colors. + # Call 'putty' manually since it needs RGB + # colors. template(colors_rgb, "colors-putty.reg") -- cgit v1.2.3 From 5e5908a01360ba36bd1c7ab3223dfef3dfedd02a Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Thu, 29 Jun 2017 19:52:43 +1000 Subject: export: Make function more general for the test. --- pywal/export_colors.py | 21 ++++++++++--------- pywal/templates/colors | 16 ++++++++++++++ tests/test_export_colors.py | 17 ++++++++++----- tests/test_files/test_template | 2 ++ tests/test_format_colors.py | 47 ------------------------------------------ 5 files changed, 41 insertions(+), 62 deletions(-) create mode 100644 pywal/templates/colors create mode 100644 tests/test_files/test_template delete mode 100755 tests/test_format_colors.py diff --git a/pywal/export_colors.py b/pywal/export_colors.py index 4282124..b3c4025 100755 --- a/pywal/export_colors.py +++ b/pywal/export_colors.py @@ -8,24 +8,24 @@ from pywal.settings import CACHE_DIR, TEMPLATE_DIR from pywal import util -def template(colors, input_file): +def template(colors, input_file, output_dir): """Read template file, substitute markers and save the file elsewhere.""" - template_file = pathlib.Path(TEMPLATE_DIR).joinpath(input_file) - export_file = pathlib.Path(CACHE_DIR).joinpath(input_file) + # Get the template name. + template_file = os.path.basename(input_file) # Import the template. - with open(template_file) as file: + with open(input_file) as file: template_data = file.readlines() # Format the markers. template_data = "".join(template_data).format(**colors) # Export the template. - with open(export_file, "w") as file: - file.write(template_data) + output_file = output_dir / template_file + util.save_file(template_data, output_file) - print(f"export: Exported {input_file}.") + print(f"export: Exported {template_file}.") def export_all_templates(colors): @@ -43,10 +43,11 @@ def export_all_templates(colors): colors_rgb = {k: util.hex_to_rgb(v) for k, v in colors["colors"].items()} # pylint: disable=W0106 - [template(colors["colors"], file.name) + [template(colors["colors"], file.path, CACHE_DIR) for file in os.scandir(TEMPLATE_DIR) - if file not in exclude] + if file.name not in exclude] # Call 'putty' manually since it needs RGB # colors. - template(colors_rgb, "colors-putty.reg") + putty_file = TEMPLATE_DIR / pathlib.Path("colors-putty.reg") + template(colors_rgb, putty_file, CACHE_DIR) diff --git a/pywal/templates/colors b/pywal/templates/colors new file mode 100644 index 0000000..228f4c8 --- /dev/null +++ b/pywal/templates/colors @@ -0,0 +1,16 @@ +{color0} +{color1} +{color2} +{color3} +{color4} +{color5} +{color6} +{color7} +{color8} +{color9} +{color10} +{color11} +{color12} +{color13} +{color14} +{color15} diff --git a/tests/test_export_colors.py b/tests/test_export_colors.py index 7cee594..90af273 100755 --- a/tests/test_export_colors.py +++ b/tests/test_export_colors.py @@ -13,11 +13,18 @@ COLORS = util.read_file_json("tests/test_files/test_file.json") class TestExportColors(unittest.TestCase): """Test the export_colors functions.""" - def test_save_colors(self): - """> Export colors to a file.""" - tmp_file = pathlib.Path("/tmp/test_file.json") - export_colors.save_colors(COLORS, tmp_file, "plain colors") - result = tmp_file.is_file() + def test_template(self): + """> Test substitutions in template file.""" + # Merge both dicts so we can access their + # values simpler. + COLORS["colors"].update(COLORS["special"]) + + # Dirs to use. + tmp_dir = pathlib.Path("/tmp") + test_template = pathlib.Path("tests/test_files/test_template") + export_colors.template(COLORS["colors"], test_template, tmp_dir) + + result = pathlib.Path("/tmp/test_template").is_file() self.assertTrue(result) diff --git a/tests/test_files/test_template b/tests/test_files/test_template new file mode 100644 index 0000000..167e17b --- /dev/null +++ b/tests/test_files/test_template @@ -0,0 +1,2 @@ +test {color0} +test {background} diff --git a/tests/test_format_colors.py b/tests/test_format_colors.py deleted file mode 100755 index 732f67b..0000000 --- a/tests/test_format_colors.py +++ /dev/null @@ -1,47 +0,0 @@ -"""Test format functions.""" -import unittest - -from pywal import format_colors -from pywal import util - - -# Import colors. -COLORS = util.read_file_json("tests/test_files/test_file.json") - - -class TestFormatColors(unittest.TestCase): - """Test the format_colors functions.""" - - def test_plain(self): - """> Convert colors to plain.""" - result = format_colors.plain(COLORS) - 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='#3A5130'\n") - - def test_css(self): - """> Convert colors to css variables.""" - result = format_colors.css(COLORS) - 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: #3A5130;\n") - - def test_putty(self): - """> Convert colors to putty theme.""" - result = format_colors.putty(COLORS) - 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: #3A5130\n*color0: #3A5130\n") - - -if __name__ == "__main__": - unittest.main() -- cgit v1.2.3 From d9a22726cbb260cae7e7fdaf1fe4b8d69caab80f Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Thu, 29 Jun 2017 19:56:09 +1000 Subject: template: Don't make dir a constant. --- pywal/export_colors.py | 11 +++++++---- pywal/settings.py | 2 -- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/pywal/export_colors.py b/pywal/export_colors.py index b3c4025..7af9741 100755 --- a/pywal/export_colors.py +++ b/pywal/export_colors.py @@ -4,13 +4,13 @@ Export colors in various formats. import os import pathlib -from pywal.settings import CACHE_DIR, TEMPLATE_DIR +from pywal.settings import CACHE_DIR from pywal import util def template(colors, input_file, output_dir): """Read template file, substitute markers and - save the file elsewhere.""" + save the file elsewhere.""" # Get the template name. template_file = os.path.basename(input_file) @@ -30,6 +30,9 @@ def template(colors, input_file, output_dir): def export_all_templates(colors): """Export all template files.""" + # Add the template dir to module path. + template_dir = os.path.join(os.path.dirname(__file__), "templates") + # Exclude these templates from the loop. # The excluded templates need color # conversion or other intervention. @@ -44,10 +47,10 @@ def export_all_templates(colors): # pylint: disable=W0106 [template(colors["colors"], file.path, CACHE_DIR) - for file in os.scandir(TEMPLATE_DIR) + for file in os.scandir(template_dir) if file.name not in exclude] # Call 'putty' manually since it needs RGB # colors. - putty_file = TEMPLATE_DIR / pathlib.Path("colors-putty.reg") + putty_file = template_dir / pathlib.Path("colors-putty.reg") template(colors_rgb, putty_file, CACHE_DIR) diff --git a/pywal/settings.py b/pywal/settings.py index d920103..d94b7eb 100755 --- a/pywal/settings.py +++ b/pywal/settings.py @@ -1,7 +1,6 @@ """ Global Constants. """ -import os import pathlib @@ -11,4 +10,3 @@ __version__ = "0.2.6" # Internal variables. COLOR_COUNT = 16 CACHE_DIR = pathlib.Path.home() / ".cache/wal/" -TEMPLATE_DIR = os.path.join(os.path.dirname(__file__), "templates") -- cgit v1.2.3 From 0834cd024ba0988c9241ee8b64c38c2e0c6337cb Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Thu, 29 Jun 2017 20:05:11 +1000 Subject: general: Remove duplicate code. --- pywal/__main__.py | 9 +++++---- pywal/reload.py | 27 +++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 4 deletions(-) create mode 100644 pywal/reload.py diff --git a/pywal/__main__.py b/pywal/__main__.py index 7b516aa..4236267 100755 --- a/pywal/__main__.py +++ b/pywal/__main__.py @@ -11,6 +11,7 @@ from pywal.settings import CACHE_DIR, __version__ from pywal import export_colors from pywal import gen_colors from pywal import set_colors +from pywal import reload from pywal import wallpaper from pywal import util @@ -94,15 +95,15 @@ def process_args(args): if not args.n: wallpaper.set_wallpaper(image) - # Set the colors. - set_colors.send_sequences(colors_plain, args.t) - export_colors.export_all_templates(colors_plain) - # -f elif args.f: colors_plain = util.read_file_json(args.f) + + # -i or -f + if args.i or args.f: set_colors.send_sequences(colors_plain, args.t) export_colors.export_all_templates(colors_plain) + reload.reload_env() # -o if args.o: diff --git a/pywal/reload.py b/pywal/reload.py new file mode 100644 index 0000000..0c5c612 --- /dev/null +++ b/pywal/reload.py @@ -0,0 +1,27 @@ +""" +Reload programs. +""" +import shutil +import subprocess + +from pywal.settings import CACHE_DIR +from pywal import util + + +def reload_i3(): + """Reload i3 colors.""" + if shutil.which("i3-msg"): + util.disown("i3-msg", "reload") + + +def reload_xrdb(): + """Merge the colors into the X db so new terminals use them.""" + if shutil.which("xrdb"): + subprocess.call(["xrdb", "-merge", CACHE_DIR / "colors.Xresources"]) + + +def reload_env(): + """Reload environment programs.""" + reload_i3() + reload_xrdb() + print("reload: Reloaded environment.") -- cgit v1.2.3 From 08f396c653e6b3fb738f39d13a9ca5eea0d61f28 Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Thu, 29 Jun 2017 22:35:47 +1000 Subject: templates: Add background, foreground and cursor. --- pywal/templates/colors | 16 ---------------- pywal/templates/colors.css | 3 +++ pywal/templates/colors.json | 25 +++++++++++++++++++++++++ pywal/templates/colors.scss | 3 +++ pywal/templates/colors.sh | 3 +++ 5 files changed, 34 insertions(+), 16 deletions(-) delete mode 100644 pywal/templates/colors create mode 100644 pywal/templates/colors.json diff --git a/pywal/templates/colors b/pywal/templates/colors deleted file mode 100644 index 228f4c8..0000000 --- a/pywal/templates/colors +++ /dev/null @@ -1,16 +0,0 @@ -{color0} -{color1} -{color2} -{color3} -{color4} -{color5} -{color6} -{color7} -{color8} -{color9} -{color10} -{color11} -{color12} -{color13} -{color14} -{color15} diff --git a/pywal/templates/colors.css b/pywal/templates/colors.css index 439bbad..f55238b 100644 --- a/pywal/templates/colors.css +++ b/pywal/templates/colors.css @@ -1,6 +1,9 @@ /* CSS variables Generated by 'wal' */ :root {{ + --background: {background}; + --foreground: {foreground}; + --cursor: {cursor}; --color0: {color0}; --color1: {color1}; --color2: {color2}; diff --git a/pywal/templates/colors.json b/pywal/templates/colors.json new file mode 100644 index 0000000..fdd9828 --- /dev/null +++ b/pywal/templates/colors.json @@ -0,0 +1,25 @@ +{{ + "special": {{ + "background": "{background}", + "foreground": "{foreground}", + "cursor": "{cursor}" + }}, + "colors": {{ + "color0": "{color0}", + "color1": "{color1}", + "color2": "{color2}", + "color3": "{color3}", + "color4": "{color4}", + "color5": "{color5}", + "color6": "{color6}", + "color7": "{color7}", + "color8": "{color8}", + "color9": "{color9}", + "color10": "{color10}", + "color11": "{color11}", + "color12": "{color12}", + "color13": "{color13}", + "color14": "{color14}", + "color15": "{color15}" + }} +}} diff --git a/pywal/templates/colors.scss b/pywal/templates/colors.scss index c2dc797..1a2e183 100644 --- a/pywal/templates/colors.scss +++ b/pywal/templates/colors.scss @@ -1,5 +1,8 @@ // SCSS Variables // Generated by 'wal' +$background: {background}; +$foreground: {foreground}; +$cursor: {cursor}; $color0: {color0}; $color1: {color1}; $color2: {color2}; diff --git a/pywal/templates/colors.sh b/pywal/templates/colors.sh index 1362da2..607d19e 100644 --- a/pywal/templates/colors.sh +++ b/pywal/templates/colors.sh @@ -1,5 +1,8 @@ # Shell variables # Generated by 'wal' +background='{background}' +foreground='{foreground}' +cursor='{cursor}' color0='{color0}' color1='{color1}' color2='{color2}' -- cgit v1.2.3