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

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

View file

@ -50,7 +50,7 @@ struct option_t option =
.list_devices = false,
.log_filename = "",
.map = "",
.color = 15,
.color = -1,
};
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, --log <filename> Log to file\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(" -h, --help Display help\n");
printf("\n");

View file

@ -25,8 +25,7 @@
#include "print.h"
bool print_tainted = false;
bool print_color_mode = false;
char print_color[20];
char ansi_format[30];
void print_hex(char c)
{
@ -48,8 +47,16 @@ void print_normal(char c)
fflush(stdout);
}
void print_set_color_mode(bool mode)
void print_enable_ansi_formatting()
{
print_color_mode = mode;
sprintf(print_color, "\e[1;38;5;%dm", option.color);
if (option.color < 0)
{
// 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"
extern bool print_tainted;
extern bool print_color_mode;
extern char print_color[];
extern char ansi_format[];
#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_COLOR_RESET "\r\n", print_color, ## args); \
else \
fprintf (stdout, "\r" format "\r\n", ## args); \
fprintf (stdout, "\r%s" format ANSI_RESET "\r\n", ansi_format, ## args); \
fflush(stdout); \
}
#define color_printf_raw(format, args...) \
#define ansi_printf_raw(format, args...) \
{ \
if (print_color_mode) \
fprintf (stdout, "%s" format ANSI_COLOR_RESET, print_color, ## args); \
else \
fprintf (stdout, format, ## args); \
fprintf (stdout, "%s" format ANSI_RESET, ansi_format, ## args); \
fflush(stdout); \
}
#define warning_printf(format, args...) \
{ \
color_printf("[%s] Warning: " format, current_time(), ## args); \
ansi_printf("[%s] Warning: " format, current_time(), ## args); \
fflush(stdout); \
}
@ -59,7 +52,7 @@ extern char print_color[];
{ \
if (print_tainted) \
putchar('\n'); \
color_printf("[%s] " format, current_time(), ## args); \
ansi_printf("[%s] " format, current_time(), ## args); \
print_tainted = false; \
}
@ -81,4 +74,4 @@ extern char print_color[];
void print_hex(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();
if (now)
{
color_printf_raw("[%s] ", now);
ansi_printf_raw("[%s] ", now);
if (option.log)
{
log_write('[');