From 33eca2b52df95f2ba88eb3457906b5c3750f7c9c Mon Sep 17 00:00:00 2001 From: Daylin Morgan Date: Mon, 5 Sep 2022 17:44:39 -0500 Subject: [PATCH] refactor: color -> ansi --- generate.py | 2 +- src/{color.py => ansi.py} | 20 ++++++++++---------- src/builtins.mk | 6 +++--- src/config.mk | 3 ++- src/help.py | 6 ++++-- src/info.py | 2 +- src/{print-colors.py => print-ansi.py} | 12 ++++++------ src/vars.py | 6 +++--- 8 files changed, 30 insertions(+), 27 deletions(-) rename src/{color.py => ansi.py} (61%) rename src/{print-colors.py => print-ansi.py} (52%) diff --git a/generate.py b/generate.py index 39e6c82..59c8e0c 100755 --- a/generate.py +++ b/generate.py @@ -5,7 +5,7 @@ from pathlib import Path import jinja2 -py_script_names = ["help", "color", "info", "print-colors", "vars"] +py_script_names = ["help", "ansi", "info", "print-ansi", "vars"] def get_jinja_env(): diff --git a/src/color.py b/src/ansi.py similarity index 61% rename from src/color.py rename to src/ansi.py index d11f289..8b27e97 100644 --- a/src/color.py +++ b/src/ansi.py @@ -1,5 +1,5 @@ #% extends "py-script.mk" %# -#% block name %#color#% endblock %# +#% block name %#ansi#% endblock %# #% block script %# import os import sys @@ -28,26 +28,26 @@ def bg(byte): return 40 + byte -class Colors: +class Ansi: """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): setattr(self, name, "") else: setattr(self, name, escape_code) def __init__(self): - self.setcolor("end", "\033[0m") + self.setcode("end", "\033[0m") for name, byte in color2byte.items(): - self.setcolor(name, f"\033[{fg(byte)}m") - self.setcolor(f"b_{name}", f"\033[1;{fg(byte)}m") - self.setcolor(f"d_{name}", f"\033[2;{fg(byte)}m") + self.setcode(name, f"\033[{fg(byte)}m") + self.setcode(f"b_{name}", f"\033[1;{fg(byte)}m") + self.setcode(f"d_{name}", f"\033[2;{fg(byte)}m") 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(): - self.setcolor(name, f"\033[{byte}m") + self.setcode(name, f"\033[{byte}m") -c = color = Colors() +a = ansi = Ansi() #% endblock %# diff --git a/src/builtins.mk b/src/builtins.mk index ae6da0d..c06230f 100644 --- a/src/builtins.mk +++ b/src/builtins.mk @@ -20,10 +20,10 @@ vars v: endif -## _print-colors | show all possible ansi color code combinations +## _print-ansi | show all possible ansi color code combinations .PHONY: -_print-colors: - $(call py,print_colors_py) +_print-ansi: + $(call py,print_ansi_py) # functions to take f-string literals and pass to python print tprint = $(call py,info_py,$(1)) diff --git a/src/config.mk b/src/config.mk index fa4c2d3..824a546 100644 --- a/src/config.mk +++ b/src/config.mk @@ -5,11 +5,12 @@ ACCENT_COLOR ?= b_yellow GOAL_COLOR ?= $(ACCENT_COLOR) MSG_COLOR ?= faint HELP_SEP ?= | +HELP_SORT ?= # sort goals alphabetically # python f-string literals EPILOG ?= define USAGE ?= -{color.$(HEADER_COLOR)}usage{color.end}: +{ansi.$(HEADER_COLOR)}usage{ansi.end}: make endef diff --git a/src/help.py b/src/help.py index 4d1d91b..86204f8 100644 --- a/src/help.py +++ b/src/help.py @@ -4,7 +4,7 @@ import os import re -##- '$(color_py)' -## +##- '$(ansi_py)' -## pattern = re.compile(r"^## (.*) \| (.*)") @@ -27,11 +27,13 @@ def get_help(file): print(f"""$(USAGE)""") 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) for goal, msg in goals: 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)""") diff --git a/src/info.py b/src/info.py index 931d748..dbbf480 100644 --- a/src/info.py +++ b/src/info.py @@ -1,7 +1,7 @@ #% extends "py-script.mk" %# #% block name %#info#% endblock %# #% block script %# -##- '$(color_py)' -## +##- '$(ansi_py)' -## print(f"""$(2)""") #% endblock %# diff --git a/src/print-colors.py b/src/print-ansi.py similarity index 52% rename from src/print-colors.py rename to src/print-ansi.py index a9099ea..d6a6b62 100644 --- a/src/print-colors.py +++ b/src/print-ansi.py @@ -1,14 +1,14 @@ #% extends "py-script.mk" %# -#% block name %#print_colors#% endblock %# +#% block name %#print_ansi#% endblock %# #% block script %# -##- '$(color_py)' -## +##- '$(ansi_py)' -## codes_names = { - getattr(colors, attr): attr - for attr in dir(colors) - if attr[0:1] != "_" and attr != "end" and attr != "setcolor" + getattr(ansi, attr): attr + for attr in dir(ansi) + if attr[0:1] != "_" and attr != "end" and attr != "setcode" } 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 %# diff --git a/src/vars.py b/src/vars.py index 630a0e4..59c2ce4 100644 --- a/src/vars.py +++ b/src/vars.py @@ -4,15 +4,15 @@ import os -##- '$(color_py)' -## +##- '$(ansi_py)' -## vars = "$2".split() 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: - 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() #% endblock %#