Skip to content

Commit

Permalink
makefiles/utils/ansi: Refactor ansi codes into their own file.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jcarrano authored and fjmolinas committed May 4, 2021
1 parent 3ed9631 commit 1036115
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 6 deletions.
1 change: 1 addition & 0 deletions Makefile.include
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 5 additions & 6 deletions makefiles/color.inc.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions makefiles/utils/ansi.mk
Original file line number Diff line number Diff line change
@@ -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
16 changes: 16 additions & 0 deletions makefiles/utils/ansi_special.mk
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 1036115

Please sign in to comment.