table

Помоћни програм из командне линије за форматирање и приказ CSV-а
Дневник | Датотеке | Референце | ПРОЧИТАЈМЕ | ЛИЦЕНЦА

чување 59c24cae04424110caf2f95aeb98ef7300ed2210
родитељ c10abd5246b61e8674a3f2836e97abc7fc28ebbc
Аутор: Страхиња Радић <contact@strahinja.org>
Датум:   Wed, 17 May 2023 19:26:25 +0200

Add -H

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

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

diff --git a/TODO b/TODO @@ -1,7 +1,7 @@ TODO ==== -[ ] Don't apply alignment to header row (switch) +[x] Don't apply alignment to header row (switch) Legend diff --git a/table.1.in b/table.1.in @@ -52,6 +52,7 @@ is a synonym for .OP "\-c \fR|\fP \-\-columns=" cols .OP "\-d \fR|\fP \-\-delim=" 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" @@ -111,11 +112,12 @@ Set maximum table width in columns (default 80). .TQ .BI \-\-delimiter= delim .br -Set field delimiter (default ","). First line of the input determines the number -of table columns. The first character of +Set field delimiter (default ","). +First line of the input determines the number of table columns. +The first character of .I delim -becomes the delimiter. Special characters, like ";", need to be quoted or -escaped: +becomes the delimiter. +Special characters, like ";", need to be quoted or escaped: . .CDS 12 $ table -d';' @@ -139,8 +141,9 @@ so are not considered as "first characters" for the assignment. .TQ .BI \-\-format= format .br -Set widths for individual columns as ratio. Parameter \fIformat\fP should be a -string of weights separated by colons. For example: +Set widths for individual columns as ratio. +Parameter \fIformat\fP should be a string of weights separated by colons. +For example: . .CDS 12 $ table -c 50 -s aa examples/quotes-english.csv @@ -170,6 +173,14 @@ $ table -f 1:5:5:1 -c 50 -s aa examples/quotes-english.csv .CDE . .TP +.B \-H +.TQ +.B \-\-align\-header +.br +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 @@ -181,8 +192,9 @@ Print this usage information screen. .TQ .B \-\-msdos .br -Ignore carriage return (\\r) characters in input. The default behavior is to -treat them as part of the text, which can produce unexpected results. +Ignore carriage return (\\r) characters in input. +The default behavior is to treat them as part of the text, which can produce +unexpected results. . .TP .B \-n @@ -191,9 +203,11 @@ treat them as part of the text, which can produce unexpected results. .br Turn off .SM ANSI SGR -codes in the output. By default, the header row is printed in bold using +codes in the output. +By default, the header row is printed in bold using .SM ANSI -codes. This switch prevents that. +codes. +This switch prevents that. . .TP .B \-p diff --git a/table.c b/table.c @@ -77,6 +77,7 @@ ULONG* format = NULL; size_t format_size = 0; ULONG format_value = 0; Alignment* align = NULL; +int align_header = 0; size_t align_size = 0; int border_mode = 0; int handle_ansi = 1; @@ -98,7 +99,7 @@ usage() printf("Usage: %s [-a <alignment>|--align=<alignment>]" " [-b|--border-mode] [-c <cols>|--columns=<cols>] [-d" " <delim>|--delimiter=<delim>] [-f <format>|--format=<format>]" - " [-h|--help] [-m|--msdos] [-n|--no-ansi]" + " [-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]\n", @@ -441,7 +442,6 @@ print_aligned(const u8* s, size_t rune_length, size_t to_size, Alignment align, printf(" "); (*current_rune_column)++; } - if (align == ALIGN_LEFT) { output_runes = 0; @@ -594,6 +594,11 @@ do_arg: { if (!strcmp(arg, "version")) cmd = CMD_VERSION; + else if (!strcmp(arg, "align-header")) + { + arg += strlen("align-header"); + align_header = 1; + } else if (startswith(arg, "align=")) { arg += strlen("align="); @@ -693,6 +698,9 @@ do_arg: case 'f': cmd = CMD_FORMAT; break; + case 'H': + align_header = 1; + break; case 'h': return usage(); break; @@ -1003,9 +1011,10 @@ continue_line: ANSI_SGR_BOLD_OFF); print_aligned(outbuf, outbufrunelen, colwidth, - current_table_column < align_size + (lineno == 0 && !align_header) ? ALIGN_LEFT + : (current_table_column < align_size ? align[current_table_column] - : ALIGN_LEFT, + : ALIGN_LEFT), &current_rune_column); while (within_column(column_start, current_rune_column)) @@ -1085,9 +1094,10 @@ done_line:; ANSI_SGR_BOLD_OFF); print_aligned(outbuf, outbufrunelen, colwidth, - current_table_column < align_size + (lineno == 0 && !align_header) ? ALIGN_LEFT + : (current_table_column < align_size ? align[current_table_column] - : ALIGN_LEFT, + : ALIGN_LEFT), &current_rune_column); *outbuf = 0;