Rework color option

Rework the color option to support setting ANSI color code values
ranging from 0..255 or "none" for no color or "list" to print a list of
available ANSI colors codes.

Also, disables color when piping.
This commit is contained in:
Martin Lund 2022-06-08 16:15:17 +02:00
parent 2519e2081a
commit 46b5783789
8 changed files with 67 additions and 32 deletions

View file

@ -47,7 +47,7 @@ The command-line interface is straightforward as reflected in the output from
-l, --log Enable log to file
--log-filename <filename> Set log filename
-m, --map <flags> Map special characters
-c, --color <code> Colorize tio text
-c, --color 0..255|none|list Colorize tio text (default: 15)
-S, --socket <socket> Listen on socket
-x, --hex Enable hexadecimal mode
-v, --version Display version

View file

@ -123,11 +123,13 @@ If defining more than one flag, the flags must be comma separated.
Enable hexadecimal mode.
.TP
.BR \-c ", " "\-\-color " \fI<code>
.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<socket>\fR\fB

View file

@ -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)

View file

@ -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"))
{

View file

@ -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);

View file

@ -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 <filename> Set log filename\n");
printf(" -m, --map <flags> Map special characters\n");
printf(" -c, --color <code> Colorize tio text\n");
printf(" -c, --color 0..255|none|list Colorize tio text (default: 15)\n");
printf(" -S, --socket <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':

View file

@ -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
// Set bold text with user defined ANSI color
sprintf(ansi_format, "\e[1;38;5;%dm", option.color);
}
}

View file

@ -24,6 +24,7 @@
#include <stdbool.h>
#include "misc.h"
#include "error.h"
#include "options.h"
extern bool print_tainted;
extern char ansi_format[];
@ -32,24 +33,36 @@ extern char ansi_format[];
#define ansi_printf(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...) \
{ \
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...) \
{ \
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...) \
{ \
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);