From bc4c95aab968552822f7dc0768ea79d8310ba602 Mon Sep 17 00:00:00 2001 From: Daylin Morgan Date: Sat, 24 Sep 2022 16:20:23 -0500 Subject: [PATCH] feat: add reusable config object --- generate.py | 9 ++++++++- src/config.mk | 4 ++-- src/confirm.py | 2 +- src/help.py | 32 +++++++++++++++++------------- src/info.py | 2 +- src/print-ansi.py | 2 +- src/{ansi.py => utils.py} | 41 ++++++++++++++++++++++++++++++++++++++- src/vars.py | 6 +++--- 8 files changed, 75 insertions(+), 23 deletions(-) rename src/{ansi.py => utils.py} (77%) diff --git a/generate.py b/generate.py index a7cd4e2..fa6aef4 100755 --- a/generate.py +++ b/generate.py @@ -4,7 +4,14 @@ import sys from pathlib import Path import jinja2 -py_script_names = ["help", "ansi", "info", "print-ansi", "vars", "confirm"] +py_script_names = [ + "help", + "info", + "print-ansi", + "vars", + "confirm", + "utils", +] def get_jinja_env(): diff --git a/src/config.mk b/src/config.mk index a0c1fa9..c2efd45 100644 --- a/src/config.mk +++ b/src/config.mk @@ -4,10 +4,10 @@ ACCENT_STYLE ?= b_yellow PARAMS_STYLE ?= $(ACCENT_STYLE) GOAL_STYLE ?= $(ACCENT_STYLE) MSG_STYLE ?= faint -DIVIDER_STYLE ?= default DIVIDER ?= ─ +DIVIDER_STYLE ?= default HELP_SEP ?= │ # python f-string literals EPILOG ?= -USAGE ?={ansi.$(HEADER_STYLE)}usage{ansi.end}:\n make \n +USAGE ?={ansi.header}usage{ansi.end}:\n make \n INHERIT_SHELL ?= diff --git a/src/confirm.py b/src/confirm.py index bc78e16..deac724 100644 --- a/src/confirm.py +++ b/src/confirm.py @@ -4,7 +4,7 @@ import sys -##- '$(ansi_py)' -## +##- '$(utils_py)' -## def confirm(): diff --git a/src/help.py b/src/help.py index bf5e44d..b10c6e4 100644 --- a/src/help.py +++ b/src/help.py @@ -9,7 +9,13 @@ import subprocess import sys from textwrap import wrap -##- '$(ansi_py)' -## +##- '$(utils_py)' -## +###- +# this is just to trick the LSP during development +from utils import ansi, cfg + +# -### + MaxLens = namedtuple("MaxLens", "goal msg") @@ -32,12 +38,12 @@ def parseargs(argstring): def divider(len): - return ansi.style(f" {'$(DIVIDER)'*len}", "$(DIVIDER_STYLE)") + return ansi.style(f" {cfg.div*len}", "div_style") def gen_makefile(): makefile = "" - for file in os.getenv("MAKEFILE_LIST").split(): + for file in os.getenv("MAKEFILE_LIST", "").split(): with open(file, "r") as f: makefile += f.read() + "\n\n" return makefile @@ -71,7 +77,7 @@ def recipe_help_header(goal): item[0].get("msgargs", ""), ) else: - return f" {ansi.style(goal,'$(GOAL_STYLE)')}" + return f" {ansi.style(goal,'goal')}" def get_goal_deps(goal="task.mk"): @@ -82,7 +88,7 @@ def get_goal_deps(goal="task.mk"): match = dep_pattern.search(line) if match and match.groups()[0]: return wrap( - f"{ansi.style('deps','default')}: {ansi.style(match.groups()[0].strip(),'$(MSG_STYLE)')}", + f"{ansi.style('deps','default')}: {ansi.style(match.groups()[0].strip(),'msg')}", initial_indent=" ", subsequent_indent=" ", ) @@ -116,8 +122,8 @@ def parse_goal(file, goal): def fmt_goal(goal, msg, max_goal_len, argstr): args = parseargs(argstr) - goal_style = args.goal_style.strip() if args.goal_style else "$(GOAL_STYLE)" - msg_style = args.msg_style.strip() if args.msg_style else "$(MSG_STYLE)" + goal_style = args.goal_style.strip() if args.goal_style else "goal" + msg_style = args.msg_style.strip() if args.msg_style else "msg" return ( ansi.style(f" {goal:>{max_goal_len}}", goal_style) + f" $(HELP_SEP) " @@ -128,20 +134,20 @@ def fmt_goal(goal, msg, max_goal_len, argstr): def fmt_rawmsg(msg, argstr, maxlens): args = parseargs(argstr) lines = [] - msg_style = args.msg_style.strip() if args.msg_style else "$(MSG_STYLE)" + msg_style = args.msg_style.strip() if args.msg_style else "msg" if not os.getenv("SHOW_HIDDEN") and args.hidden: return [] if msg: if args.align == "sep": lines.append( - f"{' '*(maxlens.goal+len('$(HELP_SEP)')+4)}{ansi.style(msg,msg_style)}" + f"{' '*(maxlens.goal+len(cfg.sep)+4)}{ansi.style(msg,msg_style)}" ) elif args.align == "center": lines.append(f" {ansi.style(msg.center(sum(maxlens)),msg_style)}") else: lines.append(f" {ansi.style(msg,msg_style)}") if args.divider: - lines.append(divider(len("$(HELP_SEP)") + sum(maxlens) + 2)) + lines.append(divider(len(cfg.sep) + sum(maxlens) + 2)) if args.whitespace: lines.append("\n") @@ -149,7 +155,7 @@ def fmt_rawmsg(msg, argstr, maxlens): def print_help(): - lines = [f"""$(USAGE)"""] + lines = [cfg.usage] items = list(parse_help(gen_makefile())) maxlens = MaxLens( @@ -164,12 +170,12 @@ def print_help(): ) if "rawmsg" in item: lines.extend(fmt_rawmsg(item["rawmsg"], item.get("rawargs", ""), maxlens)) - lines.append(f"""$(EPILOG)""") + lines.append(cfg.epilog) print("\n".join(lines)) def print_arg_help(help_args): - print(f"{ansi.style('task.mk recipe help','$(HEADER_STYLE)')}\n") + print(f"{ansi.style('task.mk recipe help','header')}\n") for arg in help_args.split(): print("\n".join(parse_goal(gen_makefile(), arg))) diff --git a/src/info.py b/src/info.py index dbbf480..55307a0 100644 --- a/src/info.py +++ b/src/info.py @@ -1,7 +1,7 @@ #% extends "py-script.mk" %# #% block name %#info#% endblock %# #% block script %# -##- '$(ansi_py)' -## +##- '$(utils_py)' -## print(f"""$(2)""") #% endblock %# diff --git a/src/print-ansi.py b/src/print-ansi.py index 39a9825..00019ff 100644 --- a/src/print-ansi.py +++ b/src/print-ansi.py @@ -1,7 +1,7 @@ #% extends "py-script.mk" %# #% block name %#print_ansi#% endblock %# #% block script %# -##- '$(ansi_py)' -## +##- '$(utils_py)' -## sep = f"$(HELP_SEP)" codes_names = {getattr(ansi, attr): attr for attr in ansi.__dict__} for code in sorted(codes_names.keys(), key=lambda item: (len(item), item)): diff --git a/src/ansi.py b/src/utils.py similarity index 77% rename from src/ansi.py rename to src/utils.py index a92e87a..417e279 100644 --- a/src/ansi.py +++ b/src/utils.py @@ -1,8 +1,24 @@ #% extends "py-script.mk" %# -#% block name %#ansi#% endblock %# +#% block name %#utils#% endblock %# #% block script %# import os import sys +from dataclasses import dataclass + + +@dataclass +class Config: + header: str + accent: str + params: str + goal: str + msg: str + div: str + div_style: str + sep: str + epilog: str + usage: str + color2byte = dict( black=0, @@ -77,6 +93,15 @@ class Ansi: return code + end + def add_cfg(self, cfg): + cfg_attrs = { + attr: getattr(cfg, attr) + for attr in cfg.__dict__ + if attr not in ["div", "sep", "epilog", "usage"] + } + for name, cfg_attr in cfg_attrs.items(): + self.setcode(name, getattr(ansi, cfg_attr)) + def style(self, text, style): if style not in self.__dict__: print(f"unknown style: {style}") @@ -86,4 +111,18 @@ class Ansi: a = ansi = Ansi() + +cfg = Config( + "$(HEADER_STYLE)", + "$(ACCENT_STYLE)", + "$(PARAMS_STYLE)", + "$(GOAL_STYLE)", + "$(MSG_STYLE)", + "$(DIVIDER)", + "$(DIVIDER_STYLE)", + "$(HELP_SEP)", + f"""$(EPILOG)""", + f"""$(USAGE)""", +) +ansi.add_cfg(cfg) #% endblock %# diff --git a/src/vars.py b/src/vars.py index 9585981..608c8b9 100644 --- a/src/vars.py +++ b/src/vars.py @@ -3,15 +3,15 @@ #% block script %# import os -##- '$(ansi_py)' -## +##- '$(utils_py)' -## vars = "$2".split() length = max((len(v) for v in vars)) -print(f"{ansi.$(HEADER_STYLE)}vars:{ansi.end}\n") +print(f"{ansi.header}vars{ansi.end}:\n") for v in vars: - print(f" {ansi.b_magenta}{v:<{length}}{ansi.end} = {os.getenv(v)}") + print(f" {ansi.params}{v:<{length}}{ansi.end} = {os.getenv(v)}") print() #% endblock %#