feat: add ansi.style method

This commit is contained in:
Daylin Morgan 2022-09-13 18:51:01 -05:00
parent 1350de0047
commit 60333cf92d
4 changed files with 46 additions and 30 deletions

View file

@ -2,7 +2,8 @@ VERSION ?= $(shell git describe --tags --always --dirty | sed s'/dirty/dev/')
TEMPLATES := $(shell find src/ -type f) TEMPLATES := $(shell find src/ -type f)
.DEFAULT_GOAL := help .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 ### task.mk development | args: --divider --align center
@ -79,7 +80,7 @@ test-bash:
define mlmsg define mlmsg
{a.b_yellow} {a.b_yellow}
It can even be multiline!{a.end} 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 as python {a.bold}f-string{a.end} literals
{a.end} {a.end}
endef endef
@ -101,7 +102,7 @@ define USAGE
{a.$(HEADER_COLOR)}usage:{a.end} {a.$(HEADER_COLOR)}usage:{a.end}
make <recipe> make <recipe>
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. the {a.italic}{a.underline}task runner{a.end} you always needed.
See the example output below. See the example output below.

View file

@ -80,6 +80,13 @@ class Ansi:
return code + end 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() a = ansi = Ansi()
#% endblock %# #% endblock %#

View file

@ -5,9 +5,12 @@ import argparse
from collections import namedtuple from collections import namedtuple
import os import os
import re import re
from typing import Any
##- '$(ansi_py)' -## ##- '$(ansi_py)' -##
ansi: Any
MaxLens = namedtuple("MaxLens", "goal msg") MaxLens = namedtuple("MaxLens", "goal msg")
# double dollar signs to prevent make escaping them # double dollar signs to prevent make escaping them
@ -45,9 +48,9 @@ def parse_make(file):
def print_goal(goal, msg, max_goal_len): def print_goal(goal, msg, max_goal_len):
print( print(
f" {ansi.$(GOAL_COLOR)}{goal:>{max_goal_len}}{ansi.end}" ansi.style(f" {goal:>{max_goal_len}}", "$(GOAL_COLOR)")
" $(HELP_SEP) " + " $(HELP_SEP) "
f"{ansi.$(MSG_COLOR)}{msg}{ansi.end}" + ansi.style(msg, "$(MSG_COLOR)")
) )
@ -56,15 +59,17 @@ def print_rawmsg(msg, argstr, maxlens):
if msg: if msg:
if args.align == "sep": if args.align == "sep":
print( 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": 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: else:
print(f" {ansi.$(MSG_COLOR)}{msg}{ansi.end}") print(f" {ansi.style(msg,'$(MSG_COLOR)')}")
if args.divider: if args.divider:
print( 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) print_goal(item["goal"], item["msg"], maxlens.goal)
if "rawmsg" in item: if "rawmsg" in item:
print_rawmsg(item["rawmsg"], item.get("args", ""), maxlens) 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)""") print(f"""$(EPILOG)""")

37
task.mk
View file

@ -1,7 +1,7 @@
# }> [github.com/daylinmorgan/task.mk] <{ # # }> [github.com/daylinmorgan/task.mk] <{ #
# Copyright (c) 2022 Daylin Morgan # Copyright (c) 2022 Daylin Morgan
# MIT License # 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. # 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. # 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 from collections import namedtuple
import os import os
import re import re
from typing import Any
$(ansi_py) $(ansi_py)
ansi: Any
MaxLens = namedtuple("MaxLens", "goal msg") MaxLens = namedtuple("MaxLens", "goal msg")
# double dollar signs to prevent make escaping them # double dollar signs to prevent make escaping them
@ -114,6 +117,7 @@ def rawargs(argstring):
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument("-a", "--align") parser.add_argument("-a", "--align")
parser.add_argument("-d", "--divider", action="store_true") parser.add_argument("-d", "--divider", action="store_true")
parser.add_argument("-ws","--whitespace",action="store_true")
return parser.parse_known_args(argstring.split()) return parser.parse_known_args(argstring.split())
@ -139,9 +143,9 @@ def parse_make(file):
def print_goal(goal, msg, max_goal_len): def print_goal(goal, msg, max_goal_len):
print( print(
f" {ansi.$(GOAL_COLOR)}{goal:>{max_goal_len}}{ansi.end}" ansi.style(f" {goal:>{max_goal_len}}", "$(GOAL_COLOR)")
" $(HELP_SEP) " + " $(HELP_SEP) "
f"{ansi.$(MSG_COLOR)}{msg}{ansi.end}" + ansi.style(msg, "$(MSG_COLOR)")
) )
@ -150,16 +154,20 @@ def print_rawmsg(msg, argstr, maxlens):
if msg: if msg:
if args.align == "sep": if args.align == "sep":
print( 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": 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: else:
print(f" {ansi.$(MSG_COLOR)}{msg}{ansi.end}") print(f" {ansi.style(msg,'$(MSG_COLOR)')}")
if args.divider: if args.divider:
print( 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(): def print_help():
@ -175,12 +183,6 @@ def print_help():
print_goal(item["goal"], item["msg"], maxlens.goal) print_goal(item["goal"], item["msg"], maxlens.goal)
if "rawmsg" in item: if "rawmsg" in item:
print_rawmsg(item["rawmsg"], item.get("args", ""), maxlens) 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)""") print(f"""$(EPILOG)""")
@ -270,6 +272,13 @@ class Ansi:
return code + end 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() a = ansi = Ansi()