diff --git a/README.md b/README.md index 25f1365..2ac5cbb 100644 --- a/README.md +++ b/README.md @@ -47,7 +47,7 @@ The command-line interface is straightforward as reflected in the output from -l, --log Enable log to file --log-filename Set log filename -m, --map Map special characters - -c, --color Colorize tio text + -c, --color 0..255|none|list Colorize tio text (default: 15) -S, --socket Listen on socket -x, --hex Enable hexadecimal mode -v, --version Display version diff --git a/man/tio.1.in b/man/tio.1.in index 7719944..bbfd338 100644 --- a/man/tio.1.in +++ b/man/tio.1.in @@ -123,11 +123,13 @@ If defining more than one flag, the flags must be comma separated. Enable hexadecimal mode. .TP -.BR \-c ", " "\-\-color " \fI +.BR \-c ", " "\-\-color " \fI0..255|none|list -Colorize tio text using ANSI color code ranging from 0 to 255. +Colorize tio text using ANSI color code value ranging from 0 to 255 or use "none" for no color. -If color code is negative a list of available ANSI colors will be printed. +Use "list" to print a list of available ANSI color codes. + +Default value is 15. .TP .BR \-S ", " "\-\-socket \fI\fR\fB diff --git a/src/bash-completion/tio.in b/src/bash-completion/tio.in index 1a07632..9c02c74 100644 --- a/src/bash-completion/tio.in +++ b/src/bash-completion/tio.in @@ -84,7 +84,7 @@ _tio() return 0 ;; -c | --color) - COMPREPLY=( $(compgen -W "$(seq 0 255)" -- ${cur}) ) + COMPREPLY=( $(compgen -W "$(seq 0 255) none list" -- ${cur}) ) return 0 ;; -S | --socket) diff --git a/src/configfile.c b/src/configfile.c index 2f0f965..ef7b690 100644 --- a/src/configfile.c +++ b/src/configfile.c @@ -166,7 +166,23 @@ static int data_handler(void *user, const char *section, const char *name, } else if (!strcmp(name, "color")) { + if (!strcmp(value, "list")) + { + // Ignore + return 0; + } + + if (!strcmp(value, "none")) + { + option.color = -1; // No color + return 0; + } + option.color = atoi(value); + if ((option.color < 0) || (option.color > 255)) + { + option.color = -1; // No color + } } else if (!strcmp(name, "socket")) { diff --git a/src/main.c b/src/main.c index c7b4363..972320b 100644 --- a/src/main.c +++ b/src/main.c @@ -70,6 +70,11 @@ int main(int argc, char *argv[]) { stdout_configure(); } + else + { + // No color when piping + option.color = -1; + } /* Add log exit handler */ atexit(&log_exit); @@ -78,8 +83,8 @@ int main(int argc, char *argv[]) if (option.log) log_open(option.log_filename); - /* Enable ANSI text formatting (colors etc.) */ - print_enable_ansi_formatting(); + /* Initialize ANSI text formatting (colors etc.) */ + print_init_ansi_formatting(); /* Print launch hints */ tio_printf("tio v%s", VERSION); diff --git a/src/options.c b/src/options.c index 769ca77..e3b7895 100644 --- a/src/options.c +++ b/src/options.c @@ -60,7 +60,7 @@ struct option_t option = .log_filename = NULL, .socket = NULL, .map = "", - .color = -1, + .color = 15, .hex_mode = false, }; @@ -83,7 +83,7 @@ void print_help(char *argv[]) printf(" -l, --log Enable log to file\n"); printf(" --log-filename Set log filename\n"); printf(" -m, --map Map special characters\n"); - printf(" -c, --color Colorize tio text\n"); + printf(" -c, --color 0..255|none|list Colorize tio text (default: 15)\n"); printf(" -S, --socket Listen on socket\n"); printf(" -x, --hex Enable hexadecimal mode\n"); printf(" -v, --version Display version\n"); @@ -284,13 +284,7 @@ void options_parse(int argc, char *argv[]) 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) + if (!strcmp(optarg, "list")) { // Print available color codes printf("Available color codes:\n"); @@ -300,6 +294,19 @@ void options_parse(int argc, char *argv[]) } exit(EXIT_SUCCESS); } + + if (!strcmp(optarg, "none")) + { + option.color = -1; + break; + } + + option.color = string_to_long(optarg); + if ((option.color < 0) || (option.color > 255)) + { + printf("Error: Invalid color code\n"); + exit(EXIT_FAILURE); + } break; case 'x': diff --git a/src/print.c b/src/print.c index 88ba881..2a43c51 100644 --- a/src/print.c +++ b/src/print.c @@ -47,16 +47,8 @@ void print_normal(char c) fflush(stdout); } -void print_enable_ansi_formatting() +void print_init_ansi_formatting() { - 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); - } + // Set 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 1c6b1e5..4546bad 100644 --- a/src/print.h +++ b/src/print.h @@ -24,6 +24,7 @@ #include #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);