mirror of
https://github.com/tio/tio.git
synced 2026-05-01 14:57:59 +02:00
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.
This commit is contained in:
parent
32e853e46e
commit
274cb63197
7 changed files with 55 additions and 16 deletions
|
|
@ -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
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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 <filename> Log to file\n");
|
||||
printf(" -m, --map <flags> 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");
|
||||
|
|
|
|||
|
|
@ -43,6 +43,7 @@ struct option_t
|
|||
bool list_devices;
|
||||
const char *log_filename;
|
||||
const char *map;
|
||||
int color;
|
||||
};
|
||||
|
||||
extern struct option_t option;
|
||||
|
|
|
|||
|
|
@ -21,11 +21,12 @@
|
|||
|
||||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
#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);
|
||||
}
|
||||
|
|
|
|||
21
src/print.h
21
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); \
|
||||
|
|
|
|||
|
|
@ -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('[');
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue