From: Karel Zak Date: Tue, 8 Jul 2025 15:02:52 +0000 (+0200) Subject: column: add --color[=] to control colorization X-Git-Url: http://git.ipfire.org/gitweb/gitweb.cgi?a=commitdiff_plain;h=ed6bc65cac382d58e5e3881149a9cbc7486cca03;p=thirdparty%2Futil-linux.git column: add --color[=] to control colorization This implements terminal-colors.d(5) behavior. Signed-off-by: Karel Zak --- diff --git a/bash-completion/column b/bash-completion/column index c9132f54d..6e0d2f283 100644 --- a/bash-completion/column +++ b/bash-completion/column @@ -9,6 +9,10 @@ _column_module() COMPREPLY=( $(compgen -W "number" -- $cur) ) return 0 ;; + '--color') + COMPREPLY=( $(compgen -W "auto never always" -- $cur) ) + return 0 + ;; '-s'|'--separator'|'-o'|'--output-separator'|'-n'|'--table-name'|'-O') COMPREPLY=( $(compgen -W "string" -- $cur) ) return 0 @@ -51,6 +55,7 @@ _column_module() --output-separator --fillrows --use-spaces + --color --help --version" COMPREPLY=( $(compgen -W "${OPTS[*]}" -- $cur) ) diff --git a/meson.build b/meson.build index 397ea1954..a2ac71e2d 100644 --- a/meson.build +++ b/meson.build @@ -1342,7 +1342,8 @@ exe = executable( column_sources, include_directories : includes, link_with : [lib_common, - lib_smartcols], + lib_smartcols, + lib_tcolors], install_dir : usrbin_exec_dir, install : true) if not is_disabler(exe) diff --git a/text-utils/Makemodule.am b/text-utils/Makemodule.am index b135e3a02..c4c3e5e23 100644 --- a/text-utils/Makemodule.am +++ b/text-utils/Makemodule.am @@ -34,7 +34,7 @@ usrbin_exec_PROGRAMS += column MANPAGES += text-utils/column.1 dist_noinst_DATA += text-utils/column.1.adoc column_SOURCES = text-utils/column.c -column_LDADD = $(LDADD) libcommon.la libsmartcols.la +column_LDADD = $(LDADD) libcommon.la libsmartcols.la libtcolors.la column_CFLAGS = $(AM_CFLAGS) -I$(ul_libsmartcols_incdir) endif diff --git a/text-utils/column.1.adoc b/text-utils/column.1.adoc index 355e776fa..a331c381d 100644 --- a/text-utils/column.1.adoc +++ b/text-utils/column.1.adoc @@ -195,8 +195,13 @@ Specify the column that contains each line's parent ID for a child-parent relati *-x, --fillrows*:: Fill rows before filling columns. +*--color*[**=**_when_]:: +Colorize the output. The optional argument _when_ can be *auto*, *never* or *always*. If the _when_ argument is omitted, it defaults to *auto*. The colors can be disabled; for the current built-in default see the *--help* output. See also the *COLORS* section. + include::man-common/help-version.adoc[] +include::man-common/colors.adoc[] + == ENVIRONMENT The environment variable *COLUMNS* is used to determine the size of the screen if no other information is available. diff --git a/text-utils/column.c b/text-utils/column.c index a6757a4f3..7a8b36415 100644 --- a/text-utils/column.c +++ b/text-utils/column.c @@ -54,6 +54,7 @@ #include "strv.h" #include "optutils.h" #include "mbsalign.h" +#include "colors.h" #include "libsmartcols.h" @@ -346,7 +347,7 @@ static void init_table(struct column_control *ctl) scols_table_enable_noencoding(ctl->tab, 1); scols_table_enable_maxout(ctl->tab, ctl->maxout ? 1 : 0); - scols_table_enable_colors(ctl->tab, 1); + scols_table_enable_colors(ctl->tab, colors_wanted() ? 1 : 0); if (ctl->tab_columns) { char **opts; @@ -926,7 +927,10 @@ static void __attribute__((__noreturn__)) usage(void) fputs(_(" -x, --fillrows fill rows before columns\n"), out); fputs(_(" -S, --use-spaces minimal whitespaces between columns (no tabs)\n"), out); - + fputs(USAGE_SEPARATOR, out); + fprintf(out, _(" --color[=] colorize output (%s, %s or %s)\n"), "auto", "always", "never"); + fprintf(out, + " %s\n", USAGE_COLORS_DEFAULT); fputs(USAGE_SEPARATOR, out); fprintf(out, USAGE_HELP_OPTIONS(34)); fprintf(out, USAGE_MAN_TAIL("column(1)")); @@ -944,10 +948,15 @@ int main(int argc, char **argv) int c; unsigned int eval = 0; /* exit value */ + int colormode = UL_COLORMODE_UNDEF; + enum { + OPT_COLOR = CHAR_MAX + 1 + }; static const struct option longopts[] = { { "columns", required_argument, NULL, 'c' }, /* deprecated */ + { "color", optional_argument, NULL, OPT_COLOR }, { "fillrows", no_argument, NULL, 'x' }, { "help", no_argument, NULL, 'h' }, { "json", no_argument, NULL, 'J' }, @@ -1083,6 +1092,11 @@ int main(int argc, char **argv) case 'x': ctl.mode = COLUMN_MODE_FILLROWS; break; + case OPT_COLOR: + colormode = UL_COLORMODE_AUTO; + if (optarg) + colormode = colormode_or_err(optarg); + break; case 'h': usage(); @@ -1114,6 +1128,9 @@ int main(int argc, char **argv) if (!ctl.tab_colnames && !ctl.tab_columns && ctl.json) errx(EXIT_FAILURE, _("option --table-columns or --table-column required for --json")); + if (ctl.mode == COLUMN_MODE_TABLE) + colors_init(colormode, "column"); + if (!*argv) eval += read_input(&ctl, stdin); else