diff --git a/README.md b/README.md index 3993533..6059e54 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,7 @@ The command-line interface is straightforward as reflected in the output from -L, --list-devices List available serial devices -l, --log Log to file -m, --map Map special characters - -c, --color <0..255> Colorize tio text (default: 15) + -c, --color <0..255> Colorize tio text -v, --version Display version -h, --help Display help diff --git a/man/tio.1.in b/man/tio.1.in index d798cb9..7ff3825 100644 --- a/man/tio.1.in +++ b/man/tio.1.in @@ -99,7 +99,7 @@ If defining more than one flag, the flags must be comma separated. .TP .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. diff --git a/src/main.c b/src/main.c index ea68531..d7c2e47 100644 --- a/src/main.c +++ b/src/main.c @@ -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); diff --git a/src/options.c b/src/options.c index f50d974..160faea 100644 --- a/src/options.c +++ b/src/options.c @@ -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 Log to file\n"); printf(" -m, --map 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"); diff --git a/src/print.c b/src/print.c index e55071f..cef23b5 100644 --- a/src/print.c +++ b/src/print.c @@ -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); + } } diff --git a/src/print.h b/src/print.h index a59b182..96774a0 100644 --- a/src/print.h +++ b/src/print.h @@ -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); diff --git a/src/tty.c b/src/tty.c index 3dcb831..afeabaf 100644 --- a/src/tty.c +++ b/src/tty.c @@ -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('[');