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

@ -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':