mirror of
https://github.com/daylinmorgan/task.mk.git
synced 2024-12-22 01:50:44 -06:00
feat: add ansi.style method
This commit is contained in:
parent
1350de0047
commit
60333cf92d
4 changed files with 46 additions and 30 deletions
7
Makefile
7
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 <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.
|
||||
See the example output below.
|
||||
|
||||
|
|
|
@ -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 %#
|
||||
|
|
25
src/help.py
25
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)""")
|
||||
|
||||
|
|
37
task.mk
37
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()
|
||||
|
||||
|
|
Loading…
Reference in a new issue