table

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

чување 9ad6c46a15cb4b08ff314606d132bc7a8462277d
родитељ 351c1d82e0b0152be92e5779ef0c8ec705886f13
Аутор: Страхиња Радић <contact@strahinja.org>
Датум:   Tue, 30 Apr 2024 15:07:20 +0200

Add -V; table.1.in: Convert to mdoc(7)

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

Diffstat:
MREADME | 3++-
Mdefs.h | 3++-
Mtable.1.in | 462++++++++++++++++++++++++++++---------------------------------------------------
Mtable.c | 44++++++++++++++++++++++++++++++--------------
измењених датотека: 4, додавања: 199(+), брисања: 313(-)

diff --git a/README b/README @@ -9,7 +9,8 @@ For installation instructions, see the file INSTALL. Examples -------- -Periodically display CPU load information in a single-line table +Periodically display CPU load information in a single-line table (requires a +mounted Linux-style /proc file system) $ watch "table /proc/loadavg -d ' ' -c 50 -n -s ss" diff --git a/defs.h b/defs.h @@ -25,7 +25,8 @@ typedef enum { CMD_FORMAT, CMD_QUOTES, CMD_SYMBOLS, - CMD_VERSION + CMD_VERSION, + CMD_FULL_VERSION } Command; enum { Q_PROCESS, Q_IGNORE }; diff --git a/table.1.in b/table.1.in @@ -1,181 +1,111 @@ -'\" t -.\" Manpage for table(1) -.\" vim: set filetype=groff: -. -.mso an-ext.tmac -.de CDS -.EX -.RS \\$1 -.sp 1 -.. -.de CDSN -.EX -.RS \\$1 +.\" This program is licensed under the terms of GNU GPL v3 or (at your option) +.\" any later version. Copyright (C) 2020-2024 Страхиња Радић. +.\" See the file LICENSE for exact copyright and license details. +.Dd %DATE% +.Dt TABLE 1 +.Os +.Sh NAME +.Nm table +.Nd Command line utility to format and display CSV +.Sh SYNOPSIS +.Nm +.Fl h | \-help | Fl V | \-full\-version | Fl v | \-version .sp 1 -.. -.de CDE +.Nm +.Oo Fl bDHmnpty | Fl a Ar alignment | Fl c Ar cols | Fl d Ar delim | +.Fl f Ar format | Fl q Ar behavior | Fl s Ar set Oc Op Ar pathname .sp 1 -.RE -.EE -.. -.de CDEN -.RE -.EE -.. -. -.TH TABLE "1" "%DATE%" "%VERSION%" "General Commands Manual" -.SH NAME -table \- Command line utility to format and display CSV. -. -.SH COMMAND ALIASES -.B tablep +.Nm +.Oo Fl "\-align=" Ns Ar alignment | Fl \-border\-mode | +.Fl \-columns= Ns Ar cols | Fl \-align\-dot | Fl \-delimiter= Ns Ar delim | +.Fl \-format= Ns Ar format | Fl \-align\-header | Fl \-msdos | Fl \-no\-ansi | +.Fl \-pause | Fl \-quotes= Ns Ar behavior | Fl \-symbols= Ns Ar set | +.Fl \-expand\-tabs | Fl \-yes\-ansi Oc Op Ar pathname +.Ss Command aliases +.Nm tablep is a synonym for -.BR "table -p" . -.br -.B tsvtable +.Ql "table -p" . +.Nm tsvtable is a synonym for -.BR "table -d \(dq$(printf '\et')\(dq -q ignore" . -. -.SH SYNOPSIS -. -.SY table -.OP "\-h \fR|\fP \-\-help" -.YS -. -.SY table -.OP "\-v \fR|\fP \-\-version" -.YS -. -.SY table -.OP "\-a \fR|\fP \-\-align=" alignment -.OP "\-b \fR|\fP \-\-border\-mode" -.OP "\-c \fR|\fP \-\-columns=" cols -.OP "\-D \fR|\fP \-\-align-dot" -.OP "\-d \fR|\fP \-\-delimiter=" delim -.OP "\-f \fR|\fP \-\-format=" format -.OP "\-H \fR|\fP \-\-align\-header" -.OP "\-m \fR|\fP \-\-msdos" -.OP "\-n \fR|\fP \-\-no\-ansi" -.OP "\-p \fR|\fP \-\-pause" -.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 -. -.SH COPYRIGHT -table Copyright \(co 2020-2024 Strahinya Radich. -.br -This program is licensed under GNU GPL v3 or later. See the file -.I LICENSE -in the table repository for details. -. -.SH DESCRIPTION -.B table -is a command line utility to format and display -.SM CSV. -It parses the given file or standard input as -.SM CSV -and prints out a table using Unicode characters for box -drawing. -. -.PP +.Ql "table -d \(dq$(printf '\et')\(dq -q ignore" . +. +.Sh DESCRIPTION +.Nm +is a command line utility to format and display CSV. +It parses the given file or standard input as CSV and prints out a table using +Unicode characters for box drawing. +.Pp When specifying multiple -.I pathname +.Ar pathname arguments, -.B table +.Nm prints a warning to stderr. Only the first such argument is processed. If -.I pathname +.Ar pathname is omitted, -.B table +.Nm reads standard input. . -.SH OPTIONS -. -.TP -.BI \-a " alignment" -.TQ -.BI \-\-align= alignment -.br +.Bl -tag -width Ds +.It Fl a Ar alignment | Fl \-align= Ns Ar alignment Set column alignments. -.I alignment +.Ar alignment is a string consisting of characters -.BR l ", " c " or " r , +.Cm l , +.Cm c +or +.Cm r , which respectively set the alignment of the corresponding column to left, center or right. -For example, \f[CR]\-arccc\fR sets the alignment for the first column to right, -and the next three columns to center. -. -.TP -.B \-b -.TQ -.B \-\-border-mode -.br +For example, +.Fl a Ns Cm rccc +sets the alignment for the first column to right, and the next three columns to +center. +.It Fl b | Fl \-border-mode Border mode: force single column. -. -.TP -.BI \-c " cols" -.TQ -.BI \-\-columns= cols -.br +.It Fl c Ar cols | Fl \-columns= Ns Ar cols Set maximum table width in columns (default 80). -. -.TP -.BI \-D -.TQ -.BI \-\-align\-dot -.br -Set alignment character to dot "." instead of a space " ". -. -.TP -.BI \-d " delim" -.TQ -.BI \-\-delimiter= delim -.br -Set field delimiter (default ","). +.It Fl D | Fl \-align\-dot +Set alignment character to dot +.Dq "." +instead of a space +.Dq " " . +.It Fl d Ar delim | Fl \-delimiter= Ns Ar delim +Set field delimiter (default +.Do , Dc Ns ). First line of the input determines the number of table columns. The first character of -.I delim +.Ar delim becomes the delimiter. -Special characters, like ";", need to be quoted or escaped: -. -.CDS 12 -$ table -d';' -.CDEN -. -.IP +Special characters, like +.Dq ";" , +need to be quoted or escaped: +.Dl "$ table -d';'" or -. -.CDSN 12 -$ table -d\\; -.CDEN -. -.IP -In the examples above, "'" and "\\" are parsed by the shell and are not passed -to -.BR table , -so are not considered as "first characters" for the assignment. -. -.IP +.Dl "$ table -d\[rs];" +In the examples above, +.Dq "'" +and +.Dq "\[rs]" +are parsed by the shell and are not passed to +.Nm , +so are not considered as +.Dq first characters +for the assignment. +.Pp Note that the -.B \-b +.Fl b parameter takes precedence: if it is present, -.B \-d +.Fl d is ignored. -. -.TP -.BI \-f " format" -.TQ -.BI \-\-format= format -.br +.It Fl f Ar format | Fl \-format= Ns Ar format Set widths for individual columns as ratio. -Parameter \fIformat\fP should be a string of weights separated by colons. +Parameter +.Ar format +should be a string of weights separated by colons. For example: -. -.CDS 12 +.Bd -literal $ table -c 50 -s aa examples/quotes-english.csv +-----------+-----------+-----------+-----------+ |ID |Name |Surname |Age | @@ -200,85 +130,51 @@ $ table -f 1:5:5:1 -c 50 -s aa examples/quotes-english.csv |003 |Richard |Smith |33 | |007 |Bond, James |Bond |46 | +----+------------------+------------------+----+ -.CDE -. -.TP -.B \-H -.TQ -.B \-\-align\-header -.br +.Ed +.It Fl H | Fl \-align\-header Makes the column alignment also apply to header row. -The default behavior is to print header cells left-aligned. -. -.TP -.BR \-h -.TQ -.B \-\-help -.br +The default behavior is to print header cells left\-aligned. +.It Fl h | Fl \-help Print the usage information screen. -. -.TP -.B \-m -.TQ -.B \-\-msdos -.br -Ignore carriage return (\\r) characters in input. +.It Fl m | Fl \-msdos +Ignore carriage return (\[rs]r) characters in input. The default behavior is to treat them as part of the text, which can produce unexpected results. -. -.TP -.B \-n -.TQ -.B \-\-no\-ansi -.br -Turn off -.SM ANSI SGR -codes in the output. -By default, the header row is printed in bold using -.SM ANSI -codes if the stdout is a tty. +.It Fl n | Fl \-no\-ansi +Turn off ANSI SGR codes in the output. +By default, the header row is printed in bold using ANSI codes if the stdout is +a tty. This switch prevents that. -. -.TP -.B \-p -.TQ -.B \-\-pause -.br -Presents the message "Press Enter to continue" and waits for input after -outputting the table. This accounts for executing -.B table +.It Fl p | Fl \-pause +Presents the message +.Dq Press Enter to continue +and waits for input after outputting the table. +This accounts for executing +.Nm from file managers which don't pause after executing programs or as a single command from terminal emulators. -. -.TP -.BI \-q " behavior" -.TQ -.BI \-\-quotes= behavior -.br -.I behavior +.It Fl q Ar behavior | Fl \-quotes= Ns Ar behavior +.Ar behavior can be one of the following: -. -.RS -.IP \fBprocess\fP +.Bl -tag -width Dsprocess +.It Cm process Treats everything between the quotes as a single column. Treats consecutive quotes within the quoted text as literal quotes. This is the default. -.IP \fBignore\fP +.It Cm ignore Ignores the special meaning of quotes (like TSV). -This is the default if -.B table +This is the default if +.Nm is called as -.BR tsvtable . -.RE -. -.TP -.BI \-s " set" -.TQ -.BI \-\-symbols= set -.br -Use table symbol set \fIset\fP for table lines, where \fIset\fP is one of -the following (format: <\fIborder\fP><\fIinner border\fP>): -. +.Nm tsvtable . +.El +.It Fl s Ar set | Fl \-symbols= Ns Ar set +Use table symbol set +.Ar set +for table lines, where +.Ar set +is one of the following (format: +.Ao Ar border Ac Ns Ao Ar inner border Ac Ns ): .TS box tab(@); c l. @@ -306,103 +202,75 @@ _ _ \fIed\fR@empty-double. .TE -. -.TP -.B \-t -.TQ -.B \-\-expand\-tabs -.br +.It Fl t | Fl \-expand\-tabs Expand tabs to spaces, honoring standard modulo 8 tab stops. -Default behavior is to output tab characters as-is. -. -.IP +Default behavior is to output tab characters as\-is. +.Pp Note that the -.B \-d +.Fl d parameter takes precedence, unless -.B \-b -is also used: if the delimiter is set to the -.SM TAB -character (0x09), -.B \-t +.Fl b +is also used: if the delimiter is set to the TAB character (0x09), +.Fl t is ignored. When -.B \-b +.Fl b is used in addition, -.B \-t +.Fl t is honored and -.B \-d +.Fl d ignored. -. -.TP -.B \-v -.TQ -.B \-\-version -.br +.It Fl V | Fl \-full\-version +Print full version (like +.Fl v Ns ), +followed by copyright notice, and exit. +.It Fl v | Fl \-version 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) -. -.SH EXAMPLES -. -.LP -Periodically display -.SM CPU -load information in a single-line table -. -.CDS 4 -$ watch "table /proc/loadavg -d ' ' -c 50 -n -s ss" -.CDE -. -.LP +.It Fl y | Fl \-yes\-ansi +Explicitly turn on ANSI SGR codes in the output. +By default, the header row is printed in bold using ANSI codes, unless the +stdout is not a tty. +This switch enforces the output of ANSI SGR codes, even in that case. +.El +. +.Sh EXAMPLES +.Bl -bullet -width 0 +.It +Periodically display CPU load information in a single\-line table (requires a +mounted Linux\-style +.Pa /proc +file system) +.Bd -literal -offset 0 +$ watch \[dq]table /proc/loadavg -d ' ' -c 50 -n -s ss\[dq] +.Ed +.It Format a long list of files in the current directory in a double line table with -single-line column divisors -. -.CDS 4 +single\-line column divisors +.Bd -literal -offset 0 $ LC_ALL=C ls -l | tail -n +2 | sed -e 's/ \\+/ /g' | table -d ' ' -n -.CDE -. -.LP +.Ed +.It Print information from -.I /etc/passwd +.Pa /etc/passwd in a table: -. -.CDS 4 +.Bd -literal -offset 0 $ table -n -d : -f 3:1:1:1:4:4:4 /etc/passwd -.CDE -. -.LP +.Ed +.It Same as above, without borders and with different column alignment: -. -.CDS 4 +.Bd -literal -offset 0 $ table -n -H -d: -arccclll -see -f3:1:1:1:4:4:4 /etc/passwd -.CDE -. -.SH AUTHOR +.Ed +.El . -Strahinya Radich, -.UR https://\:strahinja.org -.UE +.Sh "SEE ALSO" +.Xr awk 1 , +.Xr sed 1 . -.SH BUGS +.Sh AUTHORS +.An "Strahinya Radich" Aq contact@strahinja.org , +2020\-2024 . -.PP +.Sh BUGS Bugs can be reported using the ticket tracker at: -.UR https://\:todo.sr.ht/\:~strahinja/\:table -.UE +.Lk https://\:todo.sr.ht/\:~strahinja/\:table diff --git a/table.c b/table.c @@ -12,6 +12,13 @@ #include "defs.h" #include "version.h" +#define COPYRIGHT \ + (" This program is licensed under the terms of GNU GPL v3" \ + " or (at your option)\n" \ + " any later version. Copyright (C) 2020-2024 Strahinya Radich.\n" \ + " See the file LICENSE for exact copyright and license " \ + "details.") + #define CHECKEXITNOMEM(ptr) \ do \ { \ @@ -90,24 +97,26 @@ int quotes = Q_PROCESS; char alignment_char = ' '; int -version(void) +version(const int full) { - printf("%s %s\n", PROGRAMNAME, VERSION); + printf("%s %s, committed on %s\n", PROGRAMNAME, VERSION, DATE); + if (full) + puts(COPYRIGHT); return 0; } int usage(void) { - printf("Usage: %s [-a <alignment>|--align=<alignment>]" - " [-b|--border-mode] [-c <cols>|--columns=<cols>]" - " [-D|--align-dot] [-d <delim>|--delimiter=<delim>]" - " [-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]" - " [-y|--yes-ansi] [pathname]\n", - PROGRAMNAME); + printf("Usage:\t%s -h | --help | -V | --full-version | -v | --version\n" + "\t%s [-bDHmnpty | -a <alignment> | -c <cols> | -d <delim> |" + " -f <format> | -q <behavior> | -s <set>] [pathname]\n" + "\t%s [--align=<alignment> | --border-mode | --columns=<cols> |" + " --align-dot | --delimiter=<delim> | --format=<format> |" + " --align-header | --msdos | --no-ansi | --pause |" + " --quotes=<behavior> | --symbols=<set> | --expand-tabs |" + " --yes-ansi] [pathname]\n", + PROGRAMNAME, PROGRAMNAME, PROGRAMNAME); return 0; } @@ -640,7 +649,9 @@ do_arg: char c = *arg++; if (c == '-') { - if (!strcmp(arg, "version")) + if (!strcmp(arg, "full-version")) + cmd = CMD_FULL_VERSION; + else if (!strcmp(arg, "version")) cmd = CMD_VERSION; else if (!strcmp(arg, "align-header")) { @@ -784,6 +795,9 @@ do_arg: case 't': expand_tabs = 1; break; + case 'V': + cmd = CMD_FULL_VERSION; + break; case 'v': cmd = CMD_VERSION; break; @@ -849,8 +863,10 @@ do_arg: goto do_arg; done_arg: - if (cmd == CMD_VERSION) - return version(); + if (cmd == CMD_FULL_VERSION) + return version(1); + else if (cmd == CMD_VERSION) + return version(0); FILE* input = NULL; if (filename)