patterncMinor
Add color to terminal output
Viewed 0 times
outputcolorterminaladd
Problem
I have the following header included in some of my projects so that I can add a little color to the terminal output. Here is how it would be used:
fprintf(stdout, "Recognized text: %s\n", text ?: RED_TEXT("No text recognized."));color.h:/**
* @file color.h
* @brief Defines all of the ANSI terminal escape codes that modify the color of text.
*/
#ifndef COLOR_H
#define COLOR_H
#define BLACK_TEXT(x) "\033[30;1m" x "\033[0m"
#define RED_TEXT(x) "\033[31;1m" x "\033[0m"
#define GREEN_TEXT(x) "\033[32;1m" x "\033[0m"
#define YELLOW_TEXT(x) "\033[33;1m" x "\033[0m"
#define BLUE_TEXT(x) "\033[34;1m" x "\033[0m"
#define MAGENTA_TEXT(x) "\033[35;1m" x "\033[0m"
#define CYAN_TEXT(x) "\033[36;1m" x "\033[0m"
#define WHITE_TEXT(x) "\033[37;1m" x "\033[0m"
#define BOLD_BLACK_TEXT(x) "\033[1m\033[30m;1m" x "\033[0m"
#define BOLD_RED_TEXT(x) "\033[1m\033[31m;1m" x "\033[0m"
#define BOLD_GREEN_TEXT(x) "\033[1m\033[32m;1m" x "\033[0m"
#define BOLD_YELLOW_TEXT(x) "\033[1m\033[33m;1m" x "\033[0m"
#define BOLD_BLUE_TEXT(x) "\033[1m\033[34m;1m" x "\033[0m"
#define BOLD_MAGENTA_TEXT(x) "\033[1m\033[35m;1m" x "\033[0m"
#define BOLD_CYAN_TEXT(x) "\033[1m\033[36m;1m" x "\033[0m"
#define BOLD_WHITE_TEXT(x) "\033[1m\033[37m;1m" x "\033[0m"
#endif // COLOR_HSolution
As @LokiAstari says, hard-coding these escape strings into your program is a bad idea, as the escape sequences vary according to the type of terminal. There are libraries such as ncurses and terminfo that do just that.
These macros only work when their arguments are string literals, which may be surprising to callers.
There is also a lot of repetition, much of it caused by lack of orthogonality.
If you wanted to "keep it simple" and do this anyway, you would be better off with
… to be used like
For colour reset use "\033[0;m" instead of "\033[0,"
These macros only work when their arguments are string literals, which may be surprising to callers.
There is also a lot of repetition, much of it caused by lack of orthogonality.
If you wanted to "keep it simple" and do this anyway, you would be better off with
#define COLOR_RESET "\033[0m"
#define BOLD "\033[1m"
#define BLACK_TEXT "\033[30;1m"
#define RED_TEXT "\033[31;1m"
#define GREEN_TEXT "\033[32;1m"
#define YELLOW_TEXT "\033[33;1m"
#define BLUE_TEXT "\033[34;1m"
#define MAGENTA_TEXT "\033[35;1m"
#define CYAN_TEXT "\033[36;1m"
#define WHITE_TEXT "\033[37;1m"… to be used like
fprintf(stdout, "Recognized text: %s\n", text ? text : (RED_TEXT "No text recognized." COLOR_RESET));For colour reset use "\033[0;m" instead of "\033[0,"
Code Snippets
#define COLOR_RESET "\033[0m"
#define BOLD "\033[1m"
#define BLACK_TEXT "\033[30;1m"
#define RED_TEXT "\033[31;1m"
#define GREEN_TEXT "\033[32;1m"
#define YELLOW_TEXT "\033[33;1m"
#define BLUE_TEXT "\033[34;1m"
#define MAGENTA_TEXT "\033[35;1m"
#define CYAN_TEXT "\033[36;1m"
#define WHITE_TEXT "\033[37;1m"fprintf(stdout, "Recognized text: %s\n", text ? text : (RED_TEXT "No text recognized." COLOR_RESET));Context
StackExchange Code Review Q#60627, answer score: 7
Revisions (0)
No revisions yet.