diff options
| author | Dylan Araps <dylanaraps@users.noreply.github.com> | 2017-06-29 23:07:13 +1000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2017-06-29 23:07:13 +1000 |
| commit | c7cd458bfd9fd5767dafb7948dd22ef6774ac9c9 (patch) | |
| tree | 0fd07c4096ee4ca495faecdb2201f76e515da787 | |
| parent | 3283d38e07d4b20ad3a0646d28284fe205fcdc57 (diff) | |
| parent | 08f396c653e6b3fb738f39d13a9ca5eea0d61f28 (diff) | |
Merge pull request #18 from dylanaraps/template
colors: Use template files and un-hard-code export formats.
| -rwxr-xr-x | pywal/__main__.py | 11 | ||||
| -rwxr-xr-x | pywal/export_colors.py | 74 | ||||
| -rwxr-xr-x | pywal/format_colors.py | 92 | ||||
| -rw-r--r-- | pywal/reload.py | 27 | ||||
| -rw-r--r-- | pywal/templates/colors-putty.reg | 19 | ||||
| -rw-r--r-- | pywal/templates/colors.Xresources | 55 | ||||
| -rw-r--r-- | pywal/templates/colors.css | 23 | ||||
| -rw-r--r-- | pywal/templates/colors.json | 25 | ||||
| -rw-r--r-- | pywal/templates/colors.scss | 21 | ||||
| -rw-r--r-- | pywal/templates/colors.sh | 21 | ||||
| -rwxr-xr-x | tests/test_export_colors.py | 17 | ||||
| -rw-r--r-- | tests/test_files/test_template | 2 | ||||
| -rwxr-xr-x | tests/test_format_colors.py | 47 |
13 files changed, 248 insertions, 186 deletions
diff --git a/pywal/__main__.py b/pywal/__main__.py index c5a4bb8..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_colors(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_colors(colors_plain) + export_colors.export_all_templates(colors_plain) + reload.reload_env() # -o if args.o: diff --git a/pywal/export_colors.py b/pywal/export_colors.py index 3a89d22..7af9741 100755 --- a/pywal/export_colors.py +++ b/pywal/export_colors.py @@ -1,56 +1,56 @@ """ 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 -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, output_dir): + """Read template file, substitute markers and + save the file elsewhere.""" + # Get the template name. + template_file = os.path.basename(input_file) + # Import the template. + with open(input_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]) + # Format the markers. + template_data = "".join(template_data).format(**colors) + # Export the template. + output_file = output_dir / template_file + util.save_file(template_data, output_file) -def reload_i3(): - """Reload i3 colors.""" - if shutil.which("i3-msg"): - util.disown("i3-msg", "reload") + print(f"export: Exported {template_file}.") -def export_colors(colors): - """Export colors in various formats.""" - plain_colors = format_colors.plain(colors) - save_colors(plain_colors, "colors", "plain hex colors") +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") - # Shell based colors. - shell_colors = format_colors.shell(colors) - save_colors(shell_colors, "colors.sh", "shell variables") + # Exclude these templates from the loop. + # The excluded templates need color + # conversion or other intervention. + exclude = ["colors-putty.reg"] - # 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") + # Merge both dicts so we can access their + # values simpler. + colors["colors"].update(colors["special"]) - # Text editor based colors. - putty_colors = format_colors.putty(colors) - save_colors(putty_colors, "colors-putty.reg", "putty theme") + # Convert colors to other format. + colors_rgb = {k: util.hex_to_rgb(v) for k, v in colors["colors"].items()} - # X based colors. - xrdb_colors = format_colors.xrdb(colors) - save_colors(xrdb_colors, "xcolors", "xrdb colors") + # pylint: disable=W0106 + [template(colors["colors"], file.path, CACHE_DIR) + for file in os.scandir(template_dir) + if file.name not in exclude] - # i3 colors. - reload_xrdb("xcolors") - reload_i3() + # Call 'putty' manually since it needs RGB + # colors. + putty_file = template_dir / pathlib.Path("colors-putty.reg") + template(colors_rgb, putty_file, CACHE_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/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.") 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}" diff --git a/pywal/templates/colors.Xresources b/pywal/templates/colors.Xresources new file mode 100644 index 0000000..baeddf0 --- /dev/null +++ b/pywal/templates/colors.Xresources @@ -0,0 +1,55 @@ +! X colors +! Generated by 'wal' +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} diff --git a/pywal/templates/colors.css b/pywal/templates/colors.css new file mode 100644 index 0000000..f55238b --- /dev/null +++ b/pywal/templates/colors.css @@ -0,0 +1,23 @@ +/* CSS variables + Generated by 'wal' */ +:root {{ + --background: {background}; + --foreground: {foreground}; + --cursor: {cursor}; + --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.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 new file mode 100644 index 0000000..1a2e183 --- /dev/null +++ b/pywal/templates/colors.scss @@ -0,0 +1,21 @@ +// SCSS Variables +// Generated by 'wal' +$background: {background}; +$foreground: {foreground}; +$cursor: {cursor}; +$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..607d19e --- /dev/null +++ b/pywal/templates/colors.sh @@ -0,0 +1,21 @@ +# Shell variables +# Generated by 'wal' +background='{background}' +foreground='{foreground}' +cursor='{cursor}' +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/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() |
