From c2b5bbb4b016b923b8c1cc05359f3fbb4135dfcf Mon Sep 17 00:00:00 2001 From: 7brokenmirrors <7brokenmirrors@mailbox.org> Date: Sun, 30 Aug 2020 22:42:33 +0200 Subject: [PATCH] Add --current argument. Prints current / last used theme name. squashed: flake8 / pylint Fix line length in pywal/export.py --- pywal/__main__.py | 10 ++++++++++ pywal/export.py | 7 +++++-- pywal/theme.py | 17 ++++++++++++----- 3 files changed, 27 insertions(+), 7 deletions(-) diff --git a/pywal/__main__.py b/pywal/__main__.py index 91ead11..b4cbea0 100644 --- a/pywal/__main__.py +++ b/pywal/__main__.py @@ -67,6 +67,9 @@ def get_args(): arg.add_argument("--vte", action="store_true", help="Fix text-artifacts printed in VTE terminals.") + arg.add_argument("--current", action="store_true", + help="Print the current / last used theme name.") + arg.add_argument("-c", action="store_true", help="Delete all cached colorschemes.") @@ -143,6 +146,13 @@ def parse_args_exit(parser): shutil.rmtree(scheme_dir, ignore_errors=True) sys.exit(0) + if args.current: + theme_name = theme.get_last_used_theme() + if theme_name: + print(theme_name) + sys.exit(0) + sys.exit(1) + if not args.i and \ not args.theme and \ not args.R and \ diff --git a/pywal/export.py b/pywal/export.py index 1250528..5269db1 100644 --- a/pywal/export.py +++ b/pywal/export.py @@ -58,14 +58,17 @@ def template(colors, input_file, output_file=None): # If the color was changed, replace with a unique identifier. if new_color is not colors[cname]: new_color = str(new_color) - new_color_clean = new_color.replace('[', '_').replace(']', '_').replace('.', '_') + new_color_clean = new_color.replace('[', '_') \ + .replace(']', '_') \ + .replace('.', '_') template_data[i] = l.replace(replace_str, "color" + new_color_clean) colors["color" + new_color_clean] = new_color try: template_data = "".join(template_data).format(**colors) except (ValueError, KeyError, AttributeError) as exc: - logging.error("Syntax error in template file '%s': %r.", input_file, exc) + logging.error("Syntax error in template file '%s': %r.", + input_file, exc) return util.save_file(template_data, output_file) diff --git a/pywal/theme.py b/pywal/theme.py index 9dc4f13..82d19ca 100644 --- a/pywal/theme.py +++ b/pywal/theme.py @@ -19,11 +19,7 @@ def list_out(): user_themes = [theme.name.replace(".json", "") for theme in list_themes_user()] - try: - last_used_theme = util.read_file(os.path.join( - CACHE_DIR, "last_used_theme"))[0].replace(".json", "") - except FileNotFoundError: - last_used_theme = "" + last_used_theme = get_last_used_theme() if user_themes: print("\033[1;32mUser Themes\033[0m:") @@ -152,3 +148,14 @@ def save(colors, theme_name, light=False): theme_path = os.path.join(CONF_DIR, "colorschemes", "light" if light else "dark", theme_file) util.save_file_json(colors, theme_path) + + +def get_last_used_theme(): + """Get the name of the current / last used theme.""" + try: + last_used_theme = util.read_file(os.path.join( + CACHE_DIR, "last_used_theme"))[0].replace(".json", "") + except FileNotFoundError: + last_used_theme = "" + + return last_used_theme