Rework color option

Rework the color option to support setting ANSI color code values
ranging from 0..255 or "none" for no color or "list" to print a list of
available ANSI colors codes.

Also, disables color when piping.
This commit is contained in:
Martin Lund 2022-06-08 16:15:17 +02:00
parent 2519e2081a
commit 46b5783789
8 changed files with 67 additions and 32 deletions

View file

@ -24,6 +24,7 @@
#include <stdbool.h>
#include "misc.h"
#include "error.h"
#include "options.h"
extern bool print_tainted;
extern char ansi_format[];
@ -32,25 +33,37 @@ extern char ansi_format[];
#define ansi_printf(format, args...) \
{ \
fprintf (stdout, "\r%s" format ANSI_RESET "\r\n", ansi_format, ## args); \
if (option.color < 0) \
fprintf (stdout, "\r" format "\r\n", ## args); \
else \
fprintf (stdout, "\r%s" format ANSI_RESET "\r\n", ansi_format, ## args); \
fflush(stdout); \
}
#define ansi_error_printf(format, args...) \
{ \
fprintf (stderr, "\r%s" format ANSI_RESET "\r\n", ansi_format, ## args); \
if (option.color < 0) \
fprintf (stdout, "\r" format "\r\n", ## args); \
else \
fprintf (stderr, "\r%s" format ANSI_RESET "\r\n", ansi_format, ## args); \
fflush(stderr); \
}
#define ansi_printf_raw(format, args...) \
{ \
fprintf (stdout, "%s" format ANSI_RESET, ansi_format, ## args); \
if (option.color < 0) \
fprintf (stdout, "\r" format "\r\n", ## args); \
else \
fprintf (stdout, "%s" format ANSI_RESET, ansi_format, ## args); \
fflush(stdout); \
}
#define warning_printf(format, args...) \
{ \
ansi_printf("[%s] Warning: " format, current_time(), ## args); \
if (option.color < 0) \
fprintf (stdout, "\r[%s] Warning: " format "\r\n", current_time(), ## args); \
else \
ansi_printf("[%s] Warning: " format, current_time(), ## args); \
fflush(stdout); \
}
@ -88,4 +101,4 @@ extern char ansi_format[];
void print_hex(char c);
void print_normal(char c);
void print_enable_ansi_formatting(void);
void print_init_ansi_formatting(void);