fix(#1): add custom color support

This commit is contained in:
Daylin Morgan 2022-09-07 00:11:26 -05:00
parent 26edcad5c5
commit 4e5c8408c5
4 changed files with 59 additions and 17 deletions

View file

@ -2,12 +2,12 @@ VERSION ?= $(shell git describe --tags --always --dirty | sed s'/dirty/dev/')
TEMPLATES := $(shell find src/ -type f)
.DEFAULT_GOAL := help
header = $(call tprint,{a.bold}==>{a.end} {a.magenta}$(1){a.end} {a.bold}<=={a.end})
msg = $(call tprint,{a.bold}==>{a.end} {a.magenta}$(1){a.end} {a.bold}<=={a.end})
## bootstrap | generate local dev environment
.PHONY: bootstrap
bootstrap:
$(call header,Bootstrap Environment)
$(call msg,Bootstrap Environment)
@mamba create -p ./env python jinja2 black -y
@mamba run -p ./env pip install yartsu
@ -35,7 +35,7 @@ list-%:
## release | release new version of task.mk
.PHONY: release
release: version-check
$(call header,Release Project)
$(call msg,Release Project)
@./generate.py $(VERSION) > task.mk
@sed -i 's/task.mk\/.*\/task.mk/task.mk\/v$(VERSION)\/task.mk/g' README.md
@git add task.mk README.md
@ -69,7 +69,7 @@ test-bash:
$(call tbash,bash_script,test bash multiline)
define msg
define mlmsg
{a.b_yellow}
It can even be multiline!{a.end}
and styles can be defined{a.end}
@ -80,9 +80,10 @@ endef
## info | demonstrate usage of tprint
.PHONY: task
info:
$(call header, Info Message)
$(call msg, Info Message)
$(call tprint,{a.black_on_cyan}This is task-print output:{a.end})
$(call tprint,$(msg))
$(call tprint,$(mlmsg))
$(call tprint,{a.custom(fg=(148, 255, 15),bg=(103, 2, 15))}Custom Colors TOO!{a.end})
task.mk:
./generate.py $(shell git describe --tags) > task.mk

View file

@ -59,7 +59,7 @@ build:
...
```
Now when you invoke `task.mk` it will parse these and generate your help output.
Now when you invoke `make help` it will parse these and generate your help output.
In addition to a generic help output you can expose some configuration settings with `make vars`.
To do so define the variables you'd like to print with `PRINT_VARS := VAR1 VAR2 VAR3`.
@ -88,6 +88,11 @@ To see the available colors and formatting(bold,italic,etc.) use the hidden reci
**Note**: Any help commands starting with an underscore will be ignored.
To view hidden `tasks` (or recipes in GNU Make land) you can use `make _help`.
In addition, you can use custom colors using the builtin `ansi.custom` or (`a.custom`) method.
It has two optional arguments `fg` and `bg`. Which can be used to specify either an 8-bit color from the [256 colors](https://en.wikipedia.org/wiki/8-bit_color).
Or a tuple/list to define an RBG 24-bit color, for instance `a.custom(fg=(5,10,255))`.
See this project's `make info` for an example.
## Configuration
You can quickly customize some of the default behavior of `task.mk` by overriding the below variables prior to the `-include .task.mk`.
@ -111,6 +116,12 @@ define USAGE ?=
endef
```
To use a custom color for one of the predefined configuration variables specify only the custom method.
```make
HEADER_COLOR = custom(fg=171,bg=227)
```
**NOTE**: `HELP_SEP` does not change the argument definitions syntax only the format of `make help`.
## Advanced Usage: Embedded Python Scripts
@ -162,7 +173,7 @@ zstyle ':completion::complete:make:*:targets' call-command true
## Why Make?
There are lot of `GNU Make` alternatives but none have near the same level of ubiquity.
This project attaches to `make` some of native features of [`just`](https://github.com/casey/just), a command runner.
This project attaches to `make` some of the native features of [`just`](https://github.com/casey/just), a command runner.
Just is a great task runner, but it suffers two problems, users probably don't have it installed already, and there is no way to define file specific recipes.
Most of my `Makefile`'s are comprised primarily of handy `.PHONY` recipes, but I always end up with a few file specific recipes.
@ -170,13 +181,13 @@ Most of my `Makefile`'s are comprised primarily of handy `.PHONY` recipes, but I
Another interesting project I've evaluated for these purposes is [`go-task/task`](https://github.com/go-task/task).
`Task` has many of the features of `GNU Make` and some novel features of it's own.
But like `just` it's a tool people don't usually already have and it's configured using a `yaml` file.
`Yaml` files can be finicky to work with and and it uses a golang based shell runtime not your native shell, which might lead to unexpected behavior.
`Yaml` files can be finicky to work with and and it uses a golang based shell runtime, not your native shell, which might lead to unexpected behavior.
## Simpler Alternative
But I just want a basic help output, surely I don't need python for this... you would be right.
`Task.mk` replaces my old `make help` recipe boilerplate which may better serve you.
`Task.mk` replaces my old `make help` recipe boilerplate which may better serve you (so long as you have `sed`/`awk`).
```make

View file

@ -31,12 +31,6 @@ def bg(byte):
class Ansi:
"""ANSI color codes"""
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.setcode("end", "\033[0m")
for name, byte in color2byte.items():
@ -48,6 +42,42 @@ class Ansi:
for name, byte in state2byte.items():
self.setcode(name, f"\033[{byte}m")
def setcode(self, name, escape_code):
"""create attr for style and escape code"""
if not sys.stdout.isatty() or os.getenv("NO_COLOR", False):
setattr(self, name, "")
else:
setattr(self, name, escape_code)
def custom(self, fg=None, bg=None):
"""use custom color"""
code, end = "\033[", "m"
if fg:
if isinstance(fg, int):
code += f"38;5;{fg}"
elif (isinstance(fg, list) or isinstance(fg, tuple)) and len(fg) == 1:
code += f"38;5;{fg[0]}"
elif (isinstance(fg, list) or isinstance(fg, tuple)) and len(fg) == 3:
code += f"38;2;{';'.join((str(i) for i in fg))}"
else:
print("Expected one or three values for fg as a list")
sys.exit(1)
if bg:
if isinstance(bg, int):
code += f"{';' if fg else ''}48;5;{bg}"
elif (isinstance(bg, list) or isinstance(bg, tuple)) and len(bg) == 1:
code += f"{';' if fg else ''}48;5;{bg[0]}"
elif (isinstance(bg, list) or isinstance(bg, tuple)) and len(bg) == 3:
code += f"{';' if fg else ''}48;2;{';'.join((str(i) for i in bg))}"
else:
print("Expected one or three values for bg as a list")
sys.exit(1)
return code + end
a = ansi = Ansi()
#% endblock %#

View file

@ -27,7 +27,7 @@ def get_help(file):
print(f"""$(USAGE)""")
goals = list(get_help(makefile))
if os.getenv("SORT_HELP",False):
if os.getenv("SORT_HELP", False):
goals.sort(key=lambda i: i[0])
goal_len = max(len(goal[0]) for goal in goals)