From 1036115f2d497a111d737509d490163e1d49a4a8 Mon Sep 17 00:00:00 2001 From: Juan Carrano Date: Fri, 30 Aug 2019 15:02:16 +0200 Subject: [PATCH] makefiles/utils/ansi: Refactor ansi codes into their own file. The escape codes and special chars now live in their own module. The color module is only concerned with detecting whether to use colors or not. Additional variables are defined with hard a coded ESC char, a tab and a newline. This removes the need for echo or printf. --- Makefile.include | 1 + makefiles/color.inc.mk | 11 +++++------ makefiles/utils/ansi.mk | 11 +++++++++++ makefiles/utils/ansi_special.mk | 16 ++++++++++++++++ 4 files changed, 33 insertions(+), 6 deletions(-) create mode 100644 makefiles/utils/ansi.mk create mode 100644 makefiles/utils/ansi_special.mk diff --git a/Makefile.include b/Makefile.include index 692fe86a53..5ddce7f8dd 100644 --- a/Makefile.include +++ b/Makefile.include @@ -171,6 +171,7 @@ include $(RIOTMAKE)/utils/checks.mk include $(RIOTMAKE)/docker.inc.mk # include color echo macros +include $(RIOTMAKE)/utils/ansi.mk include $(RIOTMAKE)/color.inc.mk # include concurrency helpers diff --git a/makefiles/color.inc.mk b/makefiles/color.inc.mk index d302e5832d..ea494442db 100644 --- a/makefiles/color.inc.mk +++ b/makefiles/color.inc.mk @@ -14,12 +14,11 @@ ifneq ($(CC_NOCOLOR),1) IS_TERMINAL = $(if $(MAKE_TERMOUT),$(MAKE_TERMERR),) # Check if terminal support colored output ifneq ($(IS_TERMINAL),) - _ANSI_ESC := $(shell printf "\033") - COLOR_GREEN := $(_ANSI_ESC)[1;32m - COLOR_RED := $(_ANSI_ESC)[1;31m - COLOR_YELLOW := $(_ANSI_ESC)[1;33m - COLOR_PURPLE := $(_ANSI_ESC)[1;35m - COLOR_RESET := $(_ANSI_ESC)[0m + COLOR_GREEN := $(ANSI_GREEN) + COLOR_RED := $(ANSI_RED) + COLOR_YELLOW := $(ANSI_YELLOW) + COLOR_PURPLE := $(ANSI_PURPLE) + COLOR_RESET := $(ANSI_RESET) ifeq ($(OS),Darwin) COLOR_ECHO := echo -e SHELL=bash diff --git a/makefiles/utils/ansi.mk b/makefiles/utils/ansi.mk new file mode 100644 index 0000000000..53fabdb054 --- /dev/null +++ b/makefiles/utils/ansi.mk @@ -0,0 +1,11 @@ +# ANSI Terminal codes and other escape sequences. +# The objective of this definitions is to be able to write special characters +# without resorting to shell commands like echo. + +include $(RIOTMAKE)/utils/ansi_special.mk + +ANSI_GREEN := $(ANSI_ESC)[1;32m +ANSI_RED := $(ANSI_ESC)[1;31m +ANSI_YELLOW := $(ANSI_ESC)[1;33m +ANSI_PURPLE := $(ANSI_ESC)[1;35m +ANSI_RESET := $(ANSI_ESC)[0m diff --git a/makefiles/utils/ansi_special.mk b/makefiles/utils/ansi_special.mk new file mode 100644 index 0000000000..a67d5367bb --- /dev/null +++ b/makefiles/utils/ansi_special.mk @@ -0,0 +1,16 @@ +# Make does not recognize escaped characters (e.g. \t, \033, etc). +# The following variables contain the characters themselves. +# This file is inherently fragile (i.e. your editor could destroy the tab +# without you realizing, to be careful when editing this. + +# To generate this: $ printf "ANSI_ESC := \033# comment" +ANSI_ESC := # you may or may not be able to see that character in your editor + +# The third parameter to the subst is a tab - be careful not to replace it with +# spaces! +TAB := $(subst ,, ) + +define NEWLINE + + +endef