]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
column: add --color[=<when>] to control colorization
authorKarel Zak <kzak@redhat.com>
Tue, 8 Jul 2025 15:02:52 +0000 (17:02 +0200)
committerKarel Zak <kzak@redhat.com>
Tue, 8 Jul 2025 15:02:52 +0000 (17:02 +0200)
This implements terminal-colors.d(5) behavior.

Signed-off-by: Karel Zak <kzak@redhat.com>
bash-completion/column
meson.build
text-utils/Makemodule.am
text-utils/column.1.adoc
text-utils/column.c

index c9132f54dde6a62b3df1dbf1096d47abf1e478d4..6e0d2f2837a7b84a814eb106080468926f5c8780 100644 (file)
@@ -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) )
index 397ea19549218b63db4a28d12ab9a2a56cc83d25..a2ac71e2d3312a1fef805a532f03d258c1a1dce3 100644 (file)
@@ -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)
index b135e3a028199929255f54c116d0226e5dcbc1dd..c4c3e5e230a36f2b3db61ac0af4ab90a4b1b8f5b 100644 (file)
@@ -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
 
index 355e776facafa23130be61367ce9f6838ae5672a..a331c381d8a4bb40af8bb6bff203ac900a048519 100644 (file)
@@ -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.
index a6757a4f3d80b9a8227489e9d01cc967c246ca0e..7a8b3641505fbc3c2bae3162836bf74c54d03349 100644 (file)
@@ -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 <number>        minimal whitespaces between columns (no tabs)\n"), out);
 
-
+       fputs(USAGE_SEPARATOR, out);
+       fprintf(out, _("     --color[=<when>]             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