colors: Use templates.

This commit is contained in:
Dylan Araps 2017-06-29 17:25:16 +10:00
parent 3283d38e07
commit c0ee7985f3
5 changed files with 50 additions and 142 deletions

View File

@ -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:

View File

@ -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()
# Merge both dicts.
colors["colors"].update(colors["special"])
# Format the markers.
template_data = "".join(template_data).format(**colors["colors"])
# Export the template.
with open(export_file, "w") as file:
file.write(template_data)
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])
def reload_i3():
"""Reload i3 colors."""
if shutil.which("i3-msg"):
util.disown("i3-msg", "reload")
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)]

View File

@ -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

View File

@ -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")

View File

@ -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};
}}