table

Помоћни програм из командне линије за форматирање и приказ CSV-а
git clone https://git.sr.ht/~strahinja/table
Дневник | Датотеке | Референце | ПРОЧИТАЈМЕ | ЛИЦЕНЦА

чување d2c4d1e962de073002232d12fbaf16ef4b5f0eca
родитељ da365265d55a8a3c631237923a866eae25013c0d
Аутор: Страхиња Радић <contact@strahinja.org>
Датум:   Tue, 16 Apr 2024 20:39:19 +0200

Turn off ANSI SGR if stdout is not a tty; add -y | --yes-ansi

Signed-off-by: Страхиња Радић <contact@strahinja.org>

Diffstat:
MTODO | 2+-
Mtable.1.in | 18+++++++++++++++++-
Mtable.c | 30++++++++++++++++++++++++------
измењених датотека: 3, додавања: 42(+), брисања: 8(-)

diff --git a/TODO b/TODO @@ -1,7 +1,7 @@ TODO ==== -[ ] Detect if the output is a terminal and turn off ANSI codes by default if it +[x] Detect if the output is a terminal and turn off ANSI codes by default if it isn't diff --git a/table.1.in b/table.1.in @@ -60,6 +60,7 @@ is a synonym for .OP "\-q \fR|\fP \-\-quotes=" behavior .OP "\-s \fR|\fP \-\-symbols=" set .OP "\-t \fR|\fP \-\-expand-tabs" +.OP "\-y \fR|\fP \-\-yes\-ansi" .RI [ pathname ] .YS . @@ -235,7 +236,7 @@ Turn off codes in the output. By default, the header row is printed in bold using .SM ANSI -codes. +codes if the stdout is a tty. This switch prevents that. . .TP @@ -339,6 +340,21 @@ ignored. .br Print program version and exit. . +.TP +.B \-y +.TQ +.B \-\-yes\-ansi +.br +Explicitly turn on +.SM ANSI SGR +codes in the output. +By default, the header row is printed in bold using +.SM ANSI +codes, unless the stdout is not a tty. +This switch enforces the output of +.SM ANSI SGR +codes, even in that case. +. .SH "SEE ALSO" .BR awk (1), .BR sed (1) diff --git a/table.c b/table.c @@ -7,9 +7,7 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> -#ifdef __OpenBSD__ #include <unistd.h> -#endif #include "defs.h" #include "version.h" @@ -107,8 +105,8 @@ usage(void) " [-f <format>|--format=<format>]" " [-H|--align-header] [-h|--help] [-m|--msdos] [-n|--no-ansi]" " [-p|--pause] [-q <behavior>|--quotes=<behavior>]" - " [-s <set>|--symbols=<set>] [-t|--expand-tabs]" - " [-v|--version] [pathname]\n", + " [-s <set>|--symbols=<set>] [-t|--expand-tabs] [-v|--version]" + " [-y|--yes-ansi] [pathname]\n", PROGRAMNAME); return 0; } @@ -347,7 +345,7 @@ set_format(const char* arg, ULONG** format, size_t* format_size) num = strtol(token, NULL, 10); if (errno) { - perror("strtol"); + perror(PROGRAMNAME ": strtol"); free(token); free(*format); *format = NULL; @@ -367,7 +365,7 @@ set_format(const char* arg, ULONG** format, size_t* format_size) num = strtol(token, NULL, 10); if (errno) { - perror("strtol"); + perror(PROGRAMNAME ": strtol"); free(token); free(*format); *format = NULL; @@ -614,6 +612,18 @@ main(int argc, char** argv) else if (!strcmp(progname, "tablep")) opause = 1; + errno = 0; + if (!isatty(fileno(stdout))) + { + if (errno == ENOTTY) + no_ansi = 1; + else + { + perror(PROGRAMNAME ": isatty"); + exit(1); + } + } + do_arg: arg = *++argv; @@ -710,6 +720,11 @@ do_arg: "'%s'", arg)); } + else if (!strcmp(arg, "yes-ansi")) + { + arg += strlen("yes-ansi"); + no_ansi = 0; + } else if (!strcmp(arg, "help")) return usage(); else @@ -768,6 +783,9 @@ do_arg: case 'v': cmd = CMD_VERSION; break; + case 'y': + no_ansi = 0; + break; default: error(EINVAL, "Invalid argument: -%c", c); usage();