From 60333cf92dd281d2964b2724971676847c82bae7 Mon Sep 17 00:00:00 2001 From: Daylin Morgan Date: Tue, 13 Sep 2022 18:51:01 -0500 Subject: [PATCH] feat: add ansi.style method --- Makefile | 7 ++++--- src/ansi.py | 7 +++++++ src/help.py | 25 ++++++++++++------------- task.mk | 37 +++++++++++++++++++++++-------------- 4 files changed, 46 insertions(+), 30 deletions(-) diff --git a/Makefile b/Makefile index af2d4f6..ba42ab4 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,8 @@ VERSION ?= $(shell git describe --tags --always --dirty | sed s'/dirty/dev/') TEMPLATES := $(shell find src/ -type f) .DEFAULT_GOAL := help -msg = $(call tprint,{a.bold}==>{a.end} {a.magenta}$(1){a.end} {a.bold}<=={a.end}) +msgfmt = {a.style('==>','bold')} {a.style('$(1)','b_magenta')} {a.style('<==','bold')} +msg = $(call tprint,$(call msgfmt ,$(1))) ### task.mk development | args: --divider --align center @@ -79,7 +80,7 @@ test-bash: define mlmsg {a.b_yellow} It can even be multiline!{a.end} -and styles can be defined{a.end} +{a.style('and styles can be defined','red')} as python {a.bold}f-string{a.end} literals {a.end} endef @@ -101,7 +102,7 @@ define USAGE {a.$(HEADER_COLOR)}usage:{a.end} make - Turn your {a.b_magenta}`Makefile`{a.end} into + Turn your {a.style('`Makefile`','b_magenta')} into the {a.italic}{a.underline}task runner{a.end} you always needed. See the example output below. diff --git a/src/ansi.py b/src/ansi.py index 87d959a..83bd470 100644 --- a/src/ansi.py +++ b/src/ansi.py @@ -80,6 +80,13 @@ class Ansi: return code + end + def style(self, text, style): + if style not in self.__dict__: + print(f"unknown style {style}") + sys.exit(1) + else: + return f"{self.__dict__[style]}{text}{self.__dict__['end']}" + a = ansi = Ansi() #% endblock %# diff --git a/src/help.py b/src/help.py index ccba3c2..da68ed1 100644 --- a/src/help.py +++ b/src/help.py @@ -5,9 +5,12 @@ import argparse from collections import namedtuple import os import re +from typing import Any ##- '$(ansi_py)' -## +ansi: Any + MaxLens = namedtuple("MaxLens", "goal msg") # double dollar signs to prevent make escaping them @@ -45,9 +48,9 @@ def parse_make(file): def print_goal(goal, msg, max_goal_len): print( - f" {ansi.$(GOAL_COLOR)}{goal:>{max_goal_len}}{ansi.end}" - " $(HELP_SEP) " - f"{ansi.$(MSG_COLOR)}{msg}{ansi.end}" + ansi.style(f" {goal:>{max_goal_len}}", "$(GOAL_COLOR)") + + " $(HELP_SEP) " + + ansi.style(msg, "$(MSG_COLOR)") ) @@ -56,15 +59,17 @@ def print_rawmsg(msg, argstr, maxlens): if msg: if args.align == "sep": print( - f"{' '*(maxlens.goal+len('$(HELP_SEP)')+4)}{ansi.$(MSG_COLOR)}{msg}{ansi.end}" + f"{' '*(maxlens.goal+len('$(HELP_SEP)')+4)}{ansi.style(msg,'$(MSG_COLOR)')}" ) elif args.align == "center": - print(f" {ansi.$(MSG_COLOR)}{msg.center(sum(maxlens))}{ansi.end}") + print(f" {ansi.style(msg.center(sum(maxlens)),'$(MSG_COLOR)')}") else: - print(f" {ansi.$(MSG_COLOR)}{msg}{ansi.end}") + print(f" {ansi.style(msg,'$(MSG_COLOR)')}") if args.divider: print( - f"{ansi.$(DIVIDER_COLOR)} {'─'*(len('$(HELP_SEP)')+sum(maxlens)+2)}{ansi.end}" + ansi.style( + f" {'─'*(len('$(HELP_SEP)')+sum(maxlens)+2)}", "$(DIVIDER_COLOR)" + ) ) @@ -81,12 +86,6 @@ def print_help(): print_goal(item["goal"], item["msg"], maxlens.goal) if "rawmsg" in item: print_rawmsg(item["rawmsg"], item.get("args", ""), maxlens) - if len(item) == 1 and "args" in item: - args, unknown = rawargs(item["args"]) - if args.divider: - print( - " " + "─" * (len("$(HELP_SEP)") + maxlens.goal + maxlens.msg + 2) - ) print(f"""$(EPILOG)""") diff --git a/task.mk b/task.mk index a01f9a1..7fab46d 100644 --- a/task.mk +++ b/task.mk @@ -1,7 +1,7 @@ # }> [github.com/daylinmorgan/task.mk] <{ # # Copyright (c) 2022 Daylin Morgan # MIT License -# version: v22.9.12-3-gcf1233a-dev +# version: v22.9.12-4-g037e474-dev # # task.mk should be included at the bottom of your Makefile. # See below for the standard configuration options that should be set prior to including this file. @@ -99,9 +99,12 @@ import argparse from collections import namedtuple import os import re +from typing import Any $(ansi_py) +ansi: Any + MaxLens = namedtuple("MaxLens", "goal msg") # double dollar signs to prevent make escaping them @@ -114,6 +117,7 @@ def rawargs(argstring): parser = argparse.ArgumentParser() parser.add_argument("-a", "--align") parser.add_argument("-d", "--divider", action="store_true") + parser.add_argument("-ws","--whitespace",action="store_true") return parser.parse_known_args(argstring.split()) @@ -139,9 +143,9 @@ def parse_make(file): def print_goal(goal, msg, max_goal_len): print( - f" {ansi.$(GOAL_COLOR)}{goal:>{max_goal_len}}{ansi.end}" - " $(HELP_SEP) " - f"{ansi.$(MSG_COLOR)}{msg}{ansi.end}" + ansi.style(f" {goal:>{max_goal_len}}", "$(GOAL_COLOR)") + + " $(HELP_SEP) " + + ansi.style(msg, "$(MSG_COLOR)") ) @@ -150,16 +154,20 @@ def print_rawmsg(msg, argstr, maxlens): if msg: if args.align == "sep": print( - f"{' '*(maxlens.goal+len('$(HELP_SEP)')+4)}{ansi.$(MSG_COLOR)}{msg}{ansi.end}" + f"{' '*(maxlens.goal+len('$(HELP_SEP)')+4)}{ansi.style(msg,'$(MSG_COLOR)')}" ) elif args.align == "center": - print(f" {ansi.$(MSG_COLOR)}{msg.center(sum(maxlens))}{ansi.end}") + print(f" {ansi.style(msg.center(sum(maxlens)),'$(MSG_COLOR)')}") else: - print(f" {ansi.$(MSG_COLOR)}{msg}{ansi.end}") + print(f" {ansi.style(msg,'$(MSG_COLOR)')}") if args.divider: print( - f"{ansi.$(DIVIDER_COLOR)} {'─'*(len('$(HELP_SEP)')+sum(maxlens)+2)}{ansi.end}" + ansi.style( + f" {'─'*(len('$(HELP_SEP)')+sum(maxlens)+2)}", "$(DIVIDER_COLOR)" + ) ) + if args.whitespace: + print() def print_help(): @@ -175,12 +183,6 @@ def print_help(): print_goal(item["goal"], item["msg"], maxlens.goal) if "rawmsg" in item: print_rawmsg(item["rawmsg"], item.get("args", ""), maxlens) - if len(item) == 1 and "args" in item: - args, unknown = rawargs(item["args"]) - if args.divider: - print( - " " + "─" * (len("$(HELP_SEP)") + maxlens.goal + maxlens.msg + 2) - ) print(f"""$(EPILOG)""") @@ -270,6 +272,13 @@ class Ansi: return code + end + def style(self, text, style): + if style not in self.__dict__: + print(f"unknown style {style}") + sys.exit(1) + else: + return f"{self.__dict__[style]}{text}{self.__dict__['end']}" + a = ansi = Ansi()