From 274cb63197a63e167882cd4219dacdae3c14f1ef Mon Sep 17 00:00:00 2001 From: Martin Lund Date: Mon, 14 Feb 2022 09:08:54 +0100 Subject: [PATCH] Add '-c, --color' option Allow user to select which ANSI color code to use to colorize the tio text. To successfully set the color the color code must be in the range 0..255. If color code is negative tio will print all available ANSI colors. The default color is changed to bold white to make tio defaults usable for most users, including color blind users. --- man/tio.1.in | 9 ++++++++- src/bash-completion/tio.in | 5 +++++ src/options.c | 26 ++++++++++++++++++++++++-- src/options.h | 1 + src/print.c | 4 +++- src/print.h | 21 +++++++++++---------- src/tty.c | 5 +++-- 7 files changed, 55 insertions(+), 16 deletions(-) diff --git a/man/tio.1.in b/man/tio.1.in index 1acf0c3..d798cb9 100644 --- a/man/tio.1.in +++ b/man/tio.1.in @@ -11,7 +11,7 @@ tio \- a simple TTY terminal I/O tool .SH "DESCRIPTION" .PP .B tio -is a simple TTY terminal tool which features a straightforward commandline +is a simple TTY terminal tool which features a straightforward command-line interface to easily connect to TTY devices for basic I/O operations. .SH "OPTIONS" @@ -96,6 +96,13 @@ Map NL to CR-NL on output. If defining more than one flag, the flags must be comma separated. .RE +.TP +.BR \-c ", " "\-\-color " \fI<0..255> + +Colorize tio text using ANSI color code (default: 15). + +If color code is negative a list of available ANSI colors will be printed. + .TP .BR \-v ", " \-\-version diff --git a/src/bash-completion/tio.in b/src/bash-completion/tio.in index ffbf7fc..2457f75 100644 --- a/src/bash-completion/tio.in +++ b/src/bash-completion/tio.in @@ -22,6 +22,7 @@ _tio() -v --version \ -t --timestamp \ -L --list-devices \ + -c --color \ -h --help" # Complete the arguments to the options. @@ -75,6 +76,10 @@ _tio() COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) return 0 ;; + -c | --color) + COMPREPLY=( $(compgen -W "$(seq 0 255)" -- ${cur}) ) + return 0 + ;; -v | --version) COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) return 0 diff --git a/src/options.c b/src/options.c index b507e09..f50d974 100644 --- a/src/options.c +++ b/src/options.c @@ -49,7 +49,8 @@ struct option_t option = .timestamp = false, .list_devices = false, .log_filename = "", - .map = "" + .map = "", + .color = 15, }; void print_help(char *argv[]) @@ -69,6 +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(" -v, --version Display version\n"); printf(" -h, --help Display help\n"); printf("\n"); @@ -120,6 +122,7 @@ void parse_options(int argc, char *argv[]) {"list-devices", no_argument, 0, 'L'}, {"log", required_argument, 0, 'l'}, {"map", required_argument, 0, 'm'}, + {"color", required_argument, 0, 'c'}, {"version", no_argument, 0, 'v'}, {"help", no_argument, 0, 'h'}, {0, 0, 0, 0 } @@ -129,7 +132,7 @@ void parse_options(int argc, char *argv[]) int option_index = 0; /* Parse argument using getopt_long */ - c = getopt_long(argc, argv, "b:d:f:s:p:o:netLl:m:vh", long_options, &option_index); + c = getopt_long(argc, argv, "b:d:f:s:p:o:netLl:m:c:vh", long_options, &option_index); /* Detect the end of the options */ if (c == -1) @@ -196,6 +199,25 @@ void parse_options(int argc, char *argv[]) option.map = optarg; break; + case 'c': + option.color = string_to_long(optarg); + if (option.color > 255) + { + printf("Error: Invalid color code\n"); + exit(EXIT_FAILURE); + } + if (option.color < 0) + { + // Print available color codes + printf("Available color codes:\n"); + for (int i=0; i<=255; i++) + { + printf(" \e[1;38;5;%dmThis is color code %d\e[0m\n", i, i); + } + exit(EXIT_SUCCESS); + } + break; + case 'v': printf("tio v%s\n", VERSION); printf("Copyright (c) 2014-2022 Martin Lund\n"); diff --git a/src/options.h b/src/options.h index acbf867..8ff8eea 100644 --- a/src/options.h +++ b/src/options.h @@ -43,6 +43,7 @@ struct option_t bool list_devices; const char *log_filename; const char *map; + int color; }; extern struct option_t option; diff --git a/src/print.c b/src/print.c index 5412335..e55071f 100644 --- a/src/print.c +++ b/src/print.c @@ -21,11 +21,12 @@ #include #include +#include "options.h" #include "print.h" bool print_tainted = false; bool print_color_mode = false; -const char *print_color = ANSI_COLOR_YELLOW; +char print_color[20]; void print_hex(char c) { @@ -50,4 +51,5 @@ void print_normal(char c) void print_set_color_mode(bool mode) { print_color_mode = mode; + sprintf(print_color, "\e[1;38;5;%dm", option.color); } diff --git a/src/print.h b/src/print.h index 972b364..a59b182 100644 --- a/src/print.h +++ b/src/print.h @@ -27,17 +27,9 @@ extern bool print_tainted; extern bool print_color_mode; -extern const char *print_color; +extern char print_color[]; -#define ANSI_COLOR_GRAY "\x1b[1;30m" -#define ANSI_COLOR_RED "\x1b[1;31m" -#define ANSI_COLOR_GREEN "\x1b[1;32m" -#define ANSI_COLOR_YELLOW "\x1b[1;33m" -#define ANSI_COLOR_BLUE "\x1b[1;34m" -#define ANSI_COLOR_PINK "\x1b[1;35m" -#define ANSI_COLOR_CYAN "\x1b[1;36m" -#define ANSI_COLOR_WHITE "\x1b[1;37m" -#define ANSI_COLOR_RESET "\x1b[0m" +#define ANSI_COLOR_RESET "\e[0m" #define color_printf(format, args...) \ { \ @@ -48,6 +40,15 @@ extern const char *print_color; fflush(stdout); \ } +#define color_printf_raw(format, args...) \ +{ \ + if (print_color_mode) \ + fprintf (stdout, "%s" format ANSI_COLOR_RESET, print_color, ## args); \ + else \ + fprintf (stdout, format, ## args); \ + fflush(stdout); \ +} + #define warning_printf(format, args...) \ { \ color_printf("[%s] Warning: " format, current_time(), ## args); \ diff --git a/src/tty.c b/src/tty.c index 6fe8a9b..3dcb831 100644 --- a/src/tty.c +++ b/src/tty.c @@ -694,8 +694,9 @@ int tty_connect(void) if (next_timestamp && input_char != '\n' && input_char != '\r') { now = current_time(); - if (now) { - fprintf(stdout, ANSI_COLOR_GRAY "[%s] " ANSI_COLOR_RESET, now); + if (now) + { + color_printf_raw("[%s] ", now); if (option.log) { log_write('[');