mirror of
https://github.com/daylinmorgan/task.mk.git
synced 2024-12-22 01:50:44 -06:00
refactor: color -> ansi
This commit is contained in:
parent
3a185ff62e
commit
33eca2b52d
8 changed files with 30 additions and 27 deletions
|
@ -5,7 +5,7 @@ from pathlib import Path
|
||||||
|
|
||||||
import jinja2
|
import jinja2
|
||||||
|
|
||||||
py_script_names = ["help", "color", "info", "print-colors", "vars"]
|
py_script_names = ["help", "ansi", "info", "print-ansi", "vars"]
|
||||||
|
|
||||||
|
|
||||||
def get_jinja_env():
|
def get_jinja_env():
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
#% extends "py-script.mk" %#
|
#% extends "py-script.mk" %#
|
||||||
#% block name %#color#% endblock %#
|
#% block name %#ansi#% endblock %#
|
||||||
#% block script %#
|
#% block script %#
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
@ -28,26 +28,26 @@ def bg(byte):
|
||||||
return 40 + byte
|
return 40 + byte
|
||||||
|
|
||||||
|
|
||||||
class Colors:
|
class Ansi:
|
||||||
"""ANSI color codes"""
|
"""ANSI color codes"""
|
||||||
|
|
||||||
def setcolor(self, name, escape_code):
|
def setcode(self, name, escape_code):
|
||||||
if not sys.stdout.isatty() or os.getenv("NO_COLOR", False):
|
if not sys.stdout.isatty() or os.getenv("NO_COLOR", False):
|
||||||
setattr(self, name, "")
|
setattr(self, name, "")
|
||||||
else:
|
else:
|
||||||
setattr(self, name, escape_code)
|
setattr(self, name, escape_code)
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.setcolor("end", "\033[0m")
|
self.setcode("end", "\033[0m")
|
||||||
for name, byte in color2byte.items():
|
for name, byte in color2byte.items():
|
||||||
self.setcolor(name, f"\033[{fg(byte)}m")
|
self.setcode(name, f"\033[{fg(byte)}m")
|
||||||
self.setcolor(f"b_{name}", f"\033[1;{fg(byte)}m")
|
self.setcode(f"b_{name}", f"\033[1;{fg(byte)}m")
|
||||||
self.setcolor(f"d_{name}", f"\033[2;{fg(byte)}m")
|
self.setcode(f"d_{name}", f"\033[2;{fg(byte)}m")
|
||||||
for bgname, bgbyte in color2byte.items():
|
for bgname, bgbyte in color2byte.items():
|
||||||
self.setcolor(f"{name}_on_{bgname}", f"\033[{bg(bgbyte)};{fg(byte)}m")
|
self.setcode(f"{name}_on_{bgname}", f"\033[{bg(bgbyte)};{fg(byte)}m")
|
||||||
for name, byte in state2byte.items():
|
for name, byte in state2byte.items():
|
||||||
self.setcolor(name, f"\033[{byte}m")
|
self.setcode(name, f"\033[{byte}m")
|
||||||
|
|
||||||
|
|
||||||
c = color = Colors()
|
a = ansi = Ansi()
|
||||||
#% endblock %#
|
#% endblock %#
|
|
@ -20,10 +20,10 @@ vars v:
|
||||||
|
|
||||||
endif
|
endif
|
||||||
|
|
||||||
## _print-colors | show all possible ansi color code combinations
|
## _print-ansi | show all possible ansi color code combinations
|
||||||
.PHONY:
|
.PHONY:
|
||||||
_print-colors:
|
_print-ansi:
|
||||||
$(call py,print_colors_py)
|
$(call py,print_ansi_py)
|
||||||
|
|
||||||
# functions to take f-string literals and pass to python print
|
# functions to take f-string literals and pass to python print
|
||||||
tprint = $(call py,info_py,$(1))
|
tprint = $(call py,info_py,$(1))
|
||||||
|
|
|
@ -5,11 +5,12 @@ ACCENT_COLOR ?= b_yellow
|
||||||
GOAL_COLOR ?= $(ACCENT_COLOR)
|
GOAL_COLOR ?= $(ACCENT_COLOR)
|
||||||
MSG_COLOR ?= faint
|
MSG_COLOR ?= faint
|
||||||
HELP_SEP ?= |
|
HELP_SEP ?= |
|
||||||
|
HELP_SORT ?= # sort goals alphabetically
|
||||||
|
|
||||||
# python f-string literals
|
# python f-string literals
|
||||||
EPILOG ?=
|
EPILOG ?=
|
||||||
define USAGE ?=
|
define USAGE ?=
|
||||||
{color.$(HEADER_COLOR)}usage{color.end}:
|
{ansi.$(HEADER_COLOR)}usage{ansi.end}:
|
||||||
make <recipe>
|
make <recipe>
|
||||||
|
|
||||||
endef
|
endef
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
|
|
||||||
##- '$(color_py)' -##
|
##- '$(ansi_py)' -##
|
||||||
|
|
||||||
pattern = re.compile(r"^## (.*) \| (.*)")
|
pattern = re.compile(r"^## (.*) \| (.*)")
|
||||||
|
|
||||||
|
@ -27,11 +27,13 @@ def get_help(file):
|
||||||
print(f"""$(USAGE)""")
|
print(f"""$(USAGE)""")
|
||||||
|
|
||||||
goals = list(get_help(makefile))
|
goals = list(get_help(makefile))
|
||||||
|
if os.getenv("SORT_HELP",False):
|
||||||
|
goals.sort(key=lambda i: i[0])
|
||||||
goal_len = max(len(goal[0]) for goal in goals)
|
goal_len = max(len(goal[0]) for goal in goals)
|
||||||
|
|
||||||
for goal, msg in goals:
|
for goal, msg in goals:
|
||||||
print(
|
print(
|
||||||
f"{color.$(GOAL_COLOR)}{goal:>{goal_len}}{color.end} $(HELP_SEP) {color.$(MSG_COLOR)}{msg}{color.end}"
|
f"{ansi.$(GOAL_COLOR)}{goal:>{goal_len}}{ansi.end} $(HELP_SEP) {ansi.$(MSG_COLOR)}{msg}{ansi.end}"
|
||||||
)
|
)
|
||||||
|
|
||||||
print(f"""$(EPILOG)""")
|
print(f"""$(EPILOG)""")
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
#% extends "py-script.mk" %#
|
#% extends "py-script.mk" %#
|
||||||
#% block name %#info#% endblock %#
|
#% block name %#info#% endblock %#
|
||||||
#% block script %#
|
#% block script %#
|
||||||
##- '$(color_py)' -##
|
##- '$(ansi_py)' -##
|
||||||
|
|
||||||
print(f"""$(2)""")
|
print(f"""$(2)""")
|
||||||
#% endblock %#
|
#% endblock %#
|
||||||
|
|
|
@ -1,14 +1,14 @@
|
||||||
#% extends "py-script.mk" %#
|
#% extends "py-script.mk" %#
|
||||||
#% block name %#print_colors#% endblock %#
|
#% block name %#print_ansi#% endblock %#
|
||||||
#% block script %#
|
#% block script %#
|
||||||
##- '$(color_py)' -##
|
##- '$(ansi_py)' -##
|
||||||
|
|
||||||
codes_names = {
|
codes_names = {
|
||||||
getattr(colors, attr): attr
|
getattr(ansi, attr): attr
|
||||||
for attr in dir(colors)
|
for attr in dir(ansi)
|
||||||
if attr[0:1] != "_" and attr != "end" and attr != "setcolor"
|
if attr[0:1] != "_" and attr != "end" and attr != "setcode"
|
||||||
}
|
}
|
||||||
for code in sorted(codes_names.keys(), key=lambda item: (len(item), item)):
|
for code in sorted(codes_names.keys(), key=lambda item: (len(item), item)):
|
||||||
print("{:>20} {}".format(codes_names[code], code + "******" + color.end))
|
print("{:>20} {}".format(codes_names[code], code + "******" + ansi.end))
|
||||||
|
|
||||||
#% endblock %#
|
#% endblock %#
|
|
@ -4,15 +4,15 @@
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
|
||||||
##- '$(color_py)' -##
|
##- '$(ansi_py)' -##
|
||||||
|
|
||||||
vars = "$2".split()
|
vars = "$2".split()
|
||||||
length = max((len(v) for v in vars))
|
length = max((len(v) for v in vars))
|
||||||
|
|
||||||
print(f"{color.$(HEADER_COLOR)}vars:{color.end}\n")
|
print(f"{ansi.$(HEADER_COLOR)}vars:{ansi.end}\n")
|
||||||
|
|
||||||
for v in vars:
|
for v in vars:
|
||||||
print(f" {color.b_magenta}{v:<{length}}{color.end} = {os.getenv(v)}")
|
print(f" {ansi.b_magenta}{v:<{length}}{ansi.end} = {os.getenv(v)}")
|
||||||
|
|
||||||
print()
|
print()
|
||||||
#% endblock %#
|
#% endblock %#
|
||||||
|
|
Loading…
Reference in a new issue