Only apply color formatting when using color option

To help the color blind who may use custom terminal foreground /
background colors.
This commit is contained in:
Martin Lund 2022-02-15 00:36:22 +01:00
parent a699561211
commit 0e62995e6e
7 changed files with 28 additions and 28 deletions

View file

@ -38,7 +38,7 @@ The command-line interface is straightforward as reflected in the output from
-L, --list-devices List available serial devices -L, --list-devices List available serial devices
-l, --log <filename> Log to file -l, --log <filename> Log to file
-m, --map <flags> Map special characters -m, --map <flags> Map special characters
-c, --color <0..255> Colorize tio text (default: 15) -c, --color <0..255> Colorize tio text
-v, --version Display version -v, --version Display version
-h, --help Display help -h, --help Display help

View file

@ -99,7 +99,7 @@ If defining more than one flag, the flags must be comma separated.
.TP .TP
.BR \-c ", " "\-\-color " \fI<0..255> .BR \-c ", " "\-\-color " \fI<0..255>
Colorize tio text using ANSI color code (default: 15). Colorize tio text using ANSI color code.
If color code is negative a list of available ANSI colors will be printed. If color code is negative a list of available ANSI colors will be printed.

View file

@ -62,8 +62,8 @@ int main(int argc, char *argv[])
if (option.log) if (option.log)
log_open(option.log_filename); log_open(option.log_filename);
/* Enable color printing */ /* Enable ANSI text formatting (colors etc.) */
print_set_color_mode(true); print_enable_ansi_formatting();
/* Print launch hints */ /* Print launch hints */
tio_printf("tio v%s", VERSION); tio_printf("tio v%s", VERSION);

View file

@ -50,7 +50,7 @@ struct option_t option =
.list_devices = false, .list_devices = false,
.log_filename = "", .log_filename = "",
.map = "", .map = "",
.color = 15, .color = -1,
}; };
void print_help(char *argv[]) void print_help(char *argv[])
@ -70,7 +70,7 @@ void print_help(char *argv[])
printf(" -L, --list-devices List available serial devices\n"); printf(" -L, --list-devices List available serial devices\n");
printf(" -l, --log <filename> Log to file\n"); printf(" -l, --log <filename> Log to file\n");
printf(" -m, --map <flags> Map special characters\n"); printf(" -m, --map <flags> Map special characters\n");
printf(" -c, --color <0..255> Colorize tio text (default: 15)\n"); printf(" -c, --color <0..255> Colorize tio text\n");
printf(" -v, --version Display version\n"); printf(" -v, --version Display version\n");
printf(" -h, --help Display help\n"); printf(" -h, --help Display help\n");
printf("\n"); printf("\n");

View file

@ -25,8 +25,7 @@
#include "print.h" #include "print.h"
bool print_tainted = false; bool print_tainted = false;
bool print_color_mode = false; char ansi_format[30];
char print_color[20];
void print_hex(char c) void print_hex(char c)
{ {
@ -48,8 +47,16 @@ void print_normal(char c)
fflush(stdout); fflush(stdout);
} }
void print_set_color_mode(bool mode) void print_enable_ansi_formatting()
{ {
print_color_mode = mode; if (option.color < 0)
sprintf(print_color, "\e[1;38;5;%dm", option.color); {
// Enable bold text
sprintf(ansi_format, "\e[1m");
}
else
{
// Enable bold text with user defined ANSI color
sprintf(ansi_format, "\e[1;38;5;%dm", option.color);
}
} }

View file

@ -26,32 +26,25 @@
#include "error.h" #include "error.h"
extern bool print_tainted; extern bool print_tainted;
extern bool print_color_mode; extern char ansi_format[];
extern char print_color[];
#define ANSI_COLOR_RESET "\e[0m" #define ANSI_RESET "\e[0m"
#define color_printf(format, args...) \ #define ansi_printf(format, args...) \
{ \ { \
if (print_color_mode) \ fprintf (stdout, "\r%s" format ANSI_RESET "\r\n", ansi_format, ## args); \
fprintf (stdout, "\r%s" format ANSI_COLOR_RESET "\r\n", print_color, ## args); \
else \
fprintf (stdout, "\r" format "\r\n", ## args); \
fflush(stdout); \ fflush(stdout); \
} }
#define color_printf_raw(format, args...) \ #define ansi_printf_raw(format, args...) \
{ \ { \
if (print_color_mode) \ fprintf (stdout, "%s" format ANSI_RESET, ansi_format, ## args); \
fprintf (stdout, "%s" format ANSI_COLOR_RESET, print_color, ## args); \
else \
fprintf (stdout, format, ## args); \
fflush(stdout); \ fflush(stdout); \
} }
#define warning_printf(format, args...) \ #define warning_printf(format, args...) \
{ \ { \
color_printf("[%s] Warning: " format, current_time(), ## args); \ ansi_printf("[%s] Warning: " format, current_time(), ## args); \
fflush(stdout); \ fflush(stdout); \
} }
@ -59,7 +52,7 @@ extern char print_color[];
{ \ { \
if (print_tainted) \ if (print_tainted) \
putchar('\n'); \ putchar('\n'); \
color_printf("[%s] " format, current_time(), ## args); \ ansi_printf("[%s] " format, current_time(), ## args); \
print_tainted = false; \ print_tainted = false; \
} }
@ -81,4 +74,4 @@ extern char print_color[];
void print_hex(char c); void print_hex(char c);
void print_normal(char c); void print_normal(char c);
void print_set_color_mode(bool mode); void print_enable_ansi_formatting(void);

View file

@ -696,7 +696,7 @@ int tty_connect(void)
now = current_time(); now = current_time();
if (now) if (now)
{ {
color_printf_raw("[%s] ", now); ansi_printf_raw("[%s] ", now);
if (option.log) if (option.log)
{ {
log_write('['); log_write('[');