Skip to content

Instantly share code, notes, and snippets.

@rsperl
Last active April 17, 2024 23:02
Show Gist options
  • Save rsperl/d2dfe88a520968fbc1f49db0a29345b9 to your computer and use it in GitHub Desktop.
Save rsperl/d2dfe88a520968fbc1f49db0a29345b9 to your computer and use it in GitHub Desktop.
self-documenting makefile with colors
SHELL=/bin/bash
# to see all colors, run
# bash -c 'for c in {0..255}; do tput setaf $c; tput setaf $c | cat -v; echo =$c; done'
# the first 15 entries are the 8-bit colors
# define standard colors
ifneq (,$(findstring xterm,${TERM}))
BLACK := $(shell tput -Txterm setaf 0)
RED := $(shell tput -Txterm setaf 1)
GREEN := $(shell tput -Txterm setaf 2)
YELLOW := $(shell tput -Txterm setaf 3)
LIGHTPURPLE := $(shell tput -Txterm setaf 4)
PURPLE := $(shell tput -Txterm setaf 5)
BLUE := $(shell tput -Txterm setaf 6)
WHITE := $(shell tput -Txterm setaf 7)
RESET := $(shell tput -Txterm sgr0)
else
BLACK := ""
RED := ""
GREEN := ""
YELLOW := ""
LIGHTPURPLE := ""
PURPLE := ""
BLUE := ""
WHITE := ""
RESET := ""
endif
# set target color
TARGET_COLOR := $(BLUE)
POUND = \#
.PHONY: no_targets__ info help build deploy doc
no_targets__:
.DEFAULT_GOAL := help
colors: ## show all the colors
@echo "${BLACK}BLACK${RESET}"
@echo "${RED}RED${RESET}"
@echo "${GREEN}GREEN${RESET}"
@echo "${YELLOW}YELLOW${RESET}"
@echo "${LIGHTPURPLE}LIGHTPURPLE${RESET}"
@echo "${PURPLE}PURPLE${RESET}"
@echo "${BLUE}BLUE${RESET}"
@echo "${WHITE}WHITE${RESET}"
job1: ## help for job 1
@echo "job 1 started"
@$(MAKE) job2
@echo "job 1 finished"
job2: ## help for job 2
@echo "job 2"
job%: ## help for job with wildcard
@echo "job $@"
help:
@echo ""
@echo " ${BLACK}:: ${RED}Self-documenting Makefile${RESET} ${BLACK}::${RESET}"
@echo ""
@echo "Document targets by adding '$(POUND)$(POUND) comment' after the target"
@echo ""
@echo "Example:"
@echo " | job1: $(POUND)$(POUND) help for job 1"
@echo " | @echo \"run stuff for target1\""
@echo ""
@echo "${BLACK}-----------------------------------------------------------------${RESET}"
@grep -E '^[a-zA-Z_0-9%-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "${TARGET_COLOR}%-30s${RESET} %s\n", $$1, $$2}'
# vim:noexpandtab:ts=8:sw=8:ai
@ujlbu4
Copy link

ujlbu4 commented Sep 2, 2021

Normally -Ttype is not necessary because the default is taken from the $TERM (, so looks like we can improve it like this:

ifneq ($(TERM),)
    GREEN        := $(shell tput setaf 2)
    RESET        := $(shell tput sgr0)
else
    GREEN        := ""
    RESET        := ""
endif

^ if TERM is defined let tput decide which type to use, otherwise keep it empty

@4F2E4A2E
Copy link

O macOS, I had to set RESET to RESET := $(shell printf "\033[0;10m") to escape (B char being printed.

See: https://unix.stackexchange.com/questions/470499/what-is-the-ansi-escape-equivalent-of-tput-sgr0

@kerns
Copy link

kerns commented Aug 31, 2022

Very cool. Is there an easy way to reuse this setup in the head of bash scripts? Would be nice if scripts could share the same colors used in a Makefile.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment