summaryrefslogtreecommitdiff
path: root/pywal/export.py
diff options
context:
space:
mode:
authorDylan Araps <dylan.araps@gmail.com>2020-01-24 08:26:06 +0200
committerDylan Araps <dylan.araps@gmail.com>2020-01-24 08:26:06 +0200
commit42ad8f014dfe11defe094a3ce33b60f7ec27b83b (patch)
treedcfb8a63a7e23a8bcf6bbdbc4c18bed563459148 /pywal/export.py
parent11758b4548c3b696769f307d132348eaf17d5efe (diff)
parentdb48957bde740e68dc64ceafef4e62ddc7018c6a (diff)
Merge branch 'master' of github.com:dylanaraps/pywal
Diffstat (limited to 'pywal/export.py')
-rw-r--r--pywal/export.py45
1 files changed, 42 insertions, 3 deletions
diff --git a/pywal/export.py b/pywal/export.py
index 88c98a6..0c71a76 100644
--- a/pywal/export.py
+++ b/pywal/export.py
@@ -3,22 +3,60 @@ Export colors in various formats.
"""
import logging
import os
+import re
-from .settings import CACHE_DIR, MODULE_DIR, CONF_DIR
from . import util
+from .settings import CACHE_DIR, CONF_DIR, MODULE_DIR
def template(colors, input_file, output_file=None):
"""Read template file, substitute markers and
save the file elsewhere."""
template_data = util.read_file_raw(input_file)
-
+ for i, l in enumerate(template_data):
+ for match in re.finditer(r"(?<=(?<!\{))(\{([^{}]+)\})(?=(?!\}))", l):
+ # Get the color, and the functions associated with it
+ cname, _, funcs = match.group(2).partition(".")
+ # Check that functions are needed for this color
+ if len(funcs) == 0:
+ continue
+ # Build up a string which will be replaced with the new color
+ replace_str = cname
+ # Color to be modified copied into new one
+ new_color = util.Color(colors[cname].hex_color)
+ # Execute each function to be done
+ for func in filter(None, funcs.split(")")):
+ # Get function name and arguments
+ func = func.split("(")
+ fname = func[0]
+ if fname[0] == '.':
+ fname = fname[1:]
+ if not hasattr(new_color, fname):
+ logging.error(
+ "Syntax error in template file '%s' on line '%s'",
+ input_file, i)
+ function = getattr(new_color, fname)
+
+ # If the function is callable, call it
+ if callable(function):
+ if len(func) > 1:
+ new_color = function(*func[1].split(","))
+ else:
+ new_color = function()
+ # string to replace generated colors
+ if func[0] != '.':
+ replace_str += "."
+ replace_str += "(".join(func) + ")"
+ # If the color was changed, replace with a unique identifier.
+ if new_color is not colors[cname]:
+ template_data[i] = l.replace(
+ replace_str, "color" + new_color.strip)
+ colors["color" + new_color.strip] = new_color
try:
template_data = "".join(template_data).format(**colors)
except ValueError:
logging.error("Syntax error in template file '%s'.", input_file)
return
-
util.save_file(template_data, output_file)
@@ -52,6 +90,7 @@ def get_export_type(export_type):
"speedcrunch": "colors-speedcrunch.json",
"sway": "colors-sway",
"tty": "colors-tty.sh",
+ "vscode": "colors-vscode.json",
"waybar": "colors-waybar.css",
"xresources": "colors.Xresources",
"xmonad": "colors.hs",