mirror of
https://github.com/tio/tio.git
synced 2026-05-01 14:57:59 +02:00
Improve printed output
Get rid of inconsistencies in the printed output (error printing, colors, etc.). Prepare for user configurable color. Cleanup.
This commit is contained in:
parent
5efd519d2e
commit
b6eac3f001
8 changed files with 122 additions and 66 deletions
16
src/error.c
16
src/error.c
|
|
@ -32,10 +32,14 @@ char error[2][1000];
|
|||
|
||||
void error_exit(void)
|
||||
{
|
||||
/* Always print errors but only print silent errors when in no autoconnect
|
||||
* mode */
|
||||
if (error[0][0] != 0)
|
||||
printf("\rError: %s\r\n", error[0]);
|
||||
else if ((error[1][0] != 0) && (option.no_autoconnect))
|
||||
printf("\rError: %s\r\n", error[1]);
|
||||
if (error[0][0] != 0)
|
||||
{
|
||||
/* Print error */
|
||||
tio_printf("Error: %s", error[0]);
|
||||
}
|
||||
else if ((error[1][0] != 0) && (option.no_autoconnect))
|
||||
{
|
||||
/* Print silent error */
|
||||
tio_printf("Error: %s", error[1]);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
11
src/error.h
11
src/error.h
|
|
@ -19,20 +19,11 @@
|
|||
* 02110-1301, USA.
|
||||
*/
|
||||
|
||||
#ifndef ERROR_H
|
||||
#define ERROR_H
|
||||
#pragma once
|
||||
|
||||
#define TIO_SUCCESS 0
|
||||
#define TIO_ERROR 1
|
||||
|
||||
extern char error[2][1000];
|
||||
|
||||
#define error_printf(format, args...) \
|
||||
snprintf (error[0], 1000, format, ## args);
|
||||
|
||||
#define error_printf_silent(format, args...) \
|
||||
snprintf (error[1], 1000, format, ## args);
|
||||
|
||||
void error_exit(void);
|
||||
|
||||
#endif
|
||||
|
|
|
|||
11
src/main.c
11
src/main.c
|
|
@ -33,7 +33,7 @@ int main(int argc, char *argv[])
|
|||
{
|
||||
int status = 0;
|
||||
|
||||
/* Install error exit handler */
|
||||
/* Add error exit handler */
|
||||
atexit(&error_exit);
|
||||
|
||||
/* Parse options */
|
||||
|
|
@ -55,13 +55,20 @@ int main(int argc, char *argv[])
|
|||
/* Configure output terminal */
|
||||
stdout_configure();
|
||||
|
||||
/* Install log exit handler */
|
||||
/* Add log exit handler */
|
||||
atexit(&log_exit);
|
||||
|
||||
/* Create log file */
|
||||
if (option.log)
|
||||
log_open(option.log_filename);
|
||||
|
||||
/* Enable color printing */
|
||||
print_set_color_mode(true);
|
||||
|
||||
/* Print launch hints */
|
||||
tio_printf("tio v%s", VERSION);
|
||||
tio_printf("Press ctrl-t q to quit");
|
||||
|
||||
/* Connect to tty device */
|
||||
if (option.no_autoconnect)
|
||||
status = tty_connect();
|
||||
|
|
|
|||
|
|
@ -9,7 +9,8 @@ tio_sources = [
|
|||
'main.c',
|
||||
'options.c',
|
||||
'misc.c',
|
||||
'tty.c'
|
||||
'tty.c',
|
||||
'print.c'
|
||||
]
|
||||
|
||||
if tcgets2 != ''
|
||||
|
|
|
|||
|
|
@ -87,7 +87,7 @@ long string_to_long(char *string)
|
|||
result = strtol(string, &end_token, 10);
|
||||
if ((errno != 0) || (*end_token != 0))
|
||||
{
|
||||
error_printf("Invalid digit");
|
||||
printf("Error: Invalid digit\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
|
|
@ -232,7 +232,7 @@ void parse_options(int argc, char *argv[])
|
|||
|
||||
if (strlen(option.tty_device) == 0)
|
||||
{
|
||||
error_printf("Missing device name");
|
||||
printf("Error: Missing device name\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
|
|
|
|||
53
src/print.c
Normal file
53
src/print.c
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
/*
|
||||
* tio - a simple TTY terminal I/O application
|
||||
*
|
||||
* Copyright (c) 2014-2022 Martin Lund
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation; either version 2
|
||||
* of the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
||||
* 02110-1301, USA.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
#include "print.h"
|
||||
|
||||
bool print_tainted = false;
|
||||
bool print_color_mode = false;
|
||||
const char *print_color = ANSI_COLOR_YELLOW;
|
||||
|
||||
void print_hex(char c)
|
||||
{
|
||||
if ((c == '\n') || (c == '\r'))
|
||||
{
|
||||
printf("%c", c);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("%02x ", (unsigned char) c);
|
||||
}
|
||||
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
void print_normal(char c)
|
||||
{
|
||||
putchar(c);
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
void print_set_color_mode(bool mode)
|
||||
{
|
||||
print_color_mode = mode;
|
||||
}
|
||||
52
src/print.h
52
src/print.h
|
|
@ -19,8 +19,15 @@
|
|||
* 02110-1301, USA.
|
||||
*/
|
||||
|
||||
#ifndef PRINT_H
|
||||
#define PRINT_H
|
||||
#pragma once
|
||||
|
||||
#include <stdbool.h>
|
||||
#include "misc.h"
|
||||
#include "error.h"
|
||||
|
||||
extern bool print_tainted;
|
||||
extern bool print_color_mode;
|
||||
extern const char *print_color;
|
||||
|
||||
#define ANSI_COLOR_GRAY "\x1b[1;30m"
|
||||
#define ANSI_COLOR_RED "\x1b[1;31m"
|
||||
|
|
@ -33,25 +40,44 @@
|
|||
#define ANSI_COLOR_RESET "\x1b[0m"
|
||||
|
||||
#define color_printf(format, args...) \
|
||||
{ \
|
||||
fprintf (stdout, "\r" ANSI_COLOR_YELLOW format ANSI_COLOR_RESET "\r\n", ## args); \
|
||||
fflush(stdout); \
|
||||
}
|
||||
{ \
|
||||
if (print_color_mode) \
|
||||
fprintf (stdout, "\r%s" format ANSI_COLOR_RESET "\r\n", print_color, ## args); \
|
||||
else \
|
||||
fprintf (stdout, "\r" format "\r\n", ## args); \
|
||||
fflush(stdout); \
|
||||
}
|
||||
|
||||
#define warning_printf(format, args...) \
|
||||
{ \
|
||||
fprintf (stdout, "\rWarning: " format "\r\n", ## args); \
|
||||
fflush(stdout); \
|
||||
}
|
||||
{ \
|
||||
color_printf("[%s] Warning: " format, current_time(), ## args); \
|
||||
fflush(stdout); \
|
||||
}
|
||||
|
||||
#define tio_printf(format, args...) \
|
||||
{ \
|
||||
if (print_tainted) \
|
||||
putchar('\n'); \
|
||||
color_printf("[%s] " format, current_time(), ## args); \
|
||||
print_tainted = false; \
|
||||
}
|
||||
|
||||
#define error_printf(format, args...) \
|
||||
snprintf(error[0], 1000, format, ## args);
|
||||
|
||||
#define error_printf_silent(format, args...) \
|
||||
snprintf(error[1], 1000, format, ## args);
|
||||
|
||||
#ifdef DEBUG
|
||||
#define debug_printf(format, args...) \
|
||||
fprintf (stdout, "[debug] " format, ## args)
|
||||
fprintf (stdout, "[debug] " format, ## args)
|
||||
#define debug_printf_raw(format, args...) \
|
||||
fprintf (stdout, "" format, ## args)
|
||||
fprintf (stdout, "" format, ## args)
|
||||
#else
|
||||
#define debug_printf(format, args...)
|
||||
#define debug_printf_raw(format, args...)
|
||||
#endif
|
||||
|
||||
#endif
|
||||
void print_hex(char c);
|
||||
void print_normal(char c);
|
||||
void print_set_color_mode(bool mode);
|
||||
|
|
|
|||
38
src/tty.c
38
src/tty.c
|
|
@ -54,7 +54,6 @@ extern int setspeed2(int fd, int baudrate);
|
|||
static struct termios tio, tio_old, stdout_new, stdout_old, stdin_new, stdin_old;
|
||||
static unsigned long rx_total = 0, tx_total = 0;
|
||||
static bool connected = false;
|
||||
static bool tainted = false;
|
||||
static bool print_mode = NORMAL;
|
||||
static bool standard_baudrate = true;
|
||||
static void (*print)(char c);
|
||||
|
|
@ -64,29 +63,6 @@ static bool map_o_cr_nl = false;
|
|||
static bool map_o_nl_crnl = false;
|
||||
static bool map_o_del_bs = false;
|
||||
|
||||
#define tio_printf(format, args...) \
|
||||
{ \
|
||||
if (tainted) putchar('\n'); \
|
||||
color_printf("[%s] " format, current_time(), ## args); \
|
||||
tainted = false; \
|
||||
}
|
||||
|
||||
static void print_hex(char c)
|
||||
{
|
||||
|
||||
if ((c == '\n') || (c == '\r'))
|
||||
printf("%c", c);
|
||||
else
|
||||
printf("%02x ", (unsigned char) c);
|
||||
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
static void print_normal(char c)
|
||||
{
|
||||
putchar(c);
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
static void toggle_line(const char *line_name, int mask)
|
||||
{
|
||||
|
|
@ -327,10 +303,6 @@ void stdout_configure(void)
|
|||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
/* Print launch hints */
|
||||
tio_printf("tio v%s", VERSION);
|
||||
tio_printf("Press ctrl-t q to quit");
|
||||
|
||||
/* At start use normal print function */
|
||||
print = print_normal;
|
||||
|
||||
|
|
@ -579,8 +551,10 @@ void tty_wait_for_device(void)
|
|||
last_errno = 0;
|
||||
return;
|
||||
}
|
||||
else if (last_errno != errno) {
|
||||
tio_printf("%s: %s. Waiting...", option.tty_device, strerror(errno));
|
||||
else if (last_errno != errno)
|
||||
{
|
||||
warning_printf("Could not open tty device (%s)", strerror(errno));
|
||||
tio_printf("Waiting for tty device..");
|
||||
last_errno = errno;
|
||||
}
|
||||
}
|
||||
|
|
@ -659,7 +633,7 @@ int tty_connect(void)
|
|||
/* Print connect status */
|
||||
tio_printf("Connected");
|
||||
connected = true;
|
||||
tainted = false;
|
||||
print_tainted = false;
|
||||
|
||||
if (option.timestamp)
|
||||
next_timestamp = true;
|
||||
|
|
@ -754,7 +728,7 @@ int tty_connect(void)
|
|||
if (option.log)
|
||||
log_write(input_char);
|
||||
|
||||
tainted = true;
|
||||
print_tainted = true;
|
||||
|
||||
if (input_char == '\n' && option.timestamp)
|
||||
next_timestamp = true;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue