]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
column: add support for color scheme
authorKarel Zak <kzak@redhat.com>
Tue, 15 Jul 2025 13:38:46 +0000 (15:38 +0200)
committerKarel Zak <kzak@redhat.com>
Wed, 16 Jul 2025 07:12:29 +0000 (09:12 +0200)
Signed-off-by: Karel Zak <kzak@redhat.com>
bash-completion/column
text-utils/column.1.adoc
text-utils/column.c

index 6e0d2f2837a7b84a814eb106080468926f5c8780..fb209204d5812b719134e8766d8167228f008d40 100644 (file)
@@ -33,6 +33,7 @@ _column_module()
                -*)
                        OPTS="--columns
                                --table
+                               --table-colorscheme
                                --table-name
                                --table-order
                                --table-columns
index a331c381d8a4bb40af8bb6bff203ac900a048519..de530644fc63d222272f94423e003c88a9a8a6c7 100644 (file)
@@ -108,6 +108,9 @@ When not in table mode, use spaces instead of tabulators to align the columns. T
 *-t, --table*::
 Determine the number of columns the input contains and create a table. Columns are by default delimited with whitespace, or with characters supplied using the *--output-separator* option. Table output is useful for pretty-printing.
 
+*--table-colorscheme* _name_::
+Specifies color scheme. The default is name is "column". For more details see the *COLORS* section.
+
 *-C, --table-column* _attributes_::
 Define a column with a comma-separated list of attributes.
 This option can be used more than once, every use defines a single column.
@@ -142,7 +145,14 @@ Define the column color for output on the terminal. The _name_ is a color name
 lightblue, lightcyan, lightgray, lightgreen, lightmagenta, lightred, magenta, 
 red, reset, reverse, and yellow) or ANSI color sequence number(s) separated by 
 a semicolon, but without the 'ESC[' prefix and 'm' suffix. For example, "37;41" 
-defines sequences for a red background and white foreground.
+defines sequences for a red background and white foreground. For more details 
+see the *COLORS* section.
+**colorkey=**_name_;;
+Addresses color as defined in the color scheme. See *--table-colorscheme*.
+**headercolor=**_name_;;
+Like color=, but define the color for column header only.
+**headercolorkey=**_name_;;
+Addresses color for header as defined in the color scheme. See *--table-colorscheme*.
 
 *-N, --table-columns* _names_::
 Specify column names with a comma-separated list. The names are used for the table header
@@ -202,6 +212,31 @@ include::man-common/help-version.adoc[]
 
 include::man-common/colors.adoc[]
 
+The default color scheme name is "column," and it can be overridden by
+____
+*--table-colorscheme name*
+____
+
+Then _name_[.disable|enable|scheme] file may be used in /etc/terminal-colors.d/,
+$XDG_CONFIG_HOME/terminal-colors.d, or $HOME/.config/terminal-colors.d/.
+
+The logical color name (key) in the color scheme may be addressed by column properties
+colorkey= and headercolorkey=. For example
+
+....
+echo 'important red'           > ~/.config/terminal-colors.d/cooltable.scheme
+echo 'important-header 37;41' >> ~/.config/terminal-colors.d/cooltable.scheme
+echo -e "a b c\naa bb cc\naaa bbb ccc" | column -t \
+       --table-colorscheme=cooltable \
+       -C name=AAA,colorkey=important,headercolorkey=important-header \
+       -C name=BBB,color=magenta,headercolor=cyan \
+       -C name=CCC,color=green
+....
+
+This will create a color scheme with the name "cooltable" and colorize the first
+column header and data according to keys from the scheme. The other two columns
+will be colorized by direct color names.
+
 == ENVIRONMENT
 
 The environment variable *COLUMNS* is used to determine the size of the screen if no other information is available.
index 7a8b3641505fbc3c2bae3162836bf74c54d03349..0571c7463deb47fab6092c104f5d4127e8dae3ac 100644 (file)
@@ -84,6 +84,7 @@ struct column_control {
        const char *tab_colnoextrem;    /* --table-noextreme */
        const char *tab_colwrap;        /* --table-wrap */
        const char *tab_colhide;        /* --table-hide */
+       const char *tab_colorscheme;    /* --table-colorscheme */
 
        const char *tree;
        const char *tree_id;
@@ -331,6 +332,20 @@ static char **split_or_error(const char *str, const char *errmsg)
        return res;
 }
 
+/* @key= is expected in the options string */
+static const char *colorseq_from_colorkey(char *opts, const char *key)
+{
+       char *cs = ul_optstr_get_value(opts, key);
+       const char *seq = NULL;
+
+       if (cs) {
+               seq = color_scheme_get_sequence(cs, NULL);
+               free(cs);
+       }
+
+       return seq;
+}
+
 static void init_table(struct column_control *ctl)
 {
        scols_init_debug(0);
@@ -357,6 +372,18 @@ static void init_table(struct column_control *ctl)
 
                        cl = scols_table_new_column(ctl->tab, NULL, 0, 0);
                        scols_column_set_properties(cl, *opts);
+
+                       if (ctl->tab_colorscheme) {
+                               const char *seq;
+
+                               seq = colorseq_from_colorkey(*opts, "colorkey");
+                               if (seq)
+                                       scols_column_set_color(cl, seq);
+
+                               seq = colorseq_from_colorkey(*opts, "headercolorkey");
+                               if (seq)
+                                       scols_column_set_headercolor(cl, seq);
+                       }
                }
 
        } else if (ctl->tab_colnames) {
@@ -901,6 +928,7 @@ static void __attribute__((__noreturn__)) usage(void)
        fputs(_(" -t, --table                      create a table\n"), out);
        fputs(_(" -n, --table-name <name>          table name for JSON output\n"), out);
        fputs(_(" -O, --table-order <columns>      specify order of output columns\n"), out);
+       fputs(_("     --table-colorscheme <name>   specify color scheme name\n"), out);
        fputs(_(" -C, --table-column <properties>  define column\n"), out);
        fputs(_(" -N, --table-columns <names>      comma separated columns names\n"), out);
        fputs(_(" -l, --table-columns-limit <num>  maximal number of input columns\n"), out);
@@ -950,7 +978,8 @@ int main(int argc, char **argv)
        unsigned int eval = 0;          /* exit value */
        int colormode = UL_COLORMODE_UNDEF;
        enum {
-               OPT_COLOR       = CHAR_MAX + 1
+               OPT_COLOR       = CHAR_MAX + 1,
+               OPT_COLORSCHEME,
        };
 
        static const struct option longopts[] =
@@ -965,6 +994,7 @@ int main(int argc, char **argv)
                { "output-width",        required_argument, NULL, 'c' },
                { "separator",           required_argument, NULL, 's' },
                { "table",               no_argument,       NULL, 't' },
+               { "table-colorscheme",   required_argument, NULL,  OPT_COLORSCHEME },
                { "table-columns",       required_argument, NULL, 'N' },
                { "table-column",        required_argument, NULL, 'C' },
                { "table-columns-limit", required_argument, NULL, 'l' },
@@ -1097,6 +1127,9 @@ int main(int argc, char **argv)
                        if (optarg)
                                colormode = colormode_or_err(optarg);
                        break;
+               case OPT_COLORSCHEME:
+                       ctl.tab_colorscheme = optarg;
+                       break;
 
                case 'h':
                        usage();
@@ -1129,7 +1162,7 @@ int main(int argc, char **argv)
                errx(EXIT_FAILURE, _("option --table-columns or --table-column required for --json"));
 
        if (ctl.mode == COLUMN_MODE_TABLE)
-               colors_init(colormode, "column");
+               colors_init(colormode, ctl.tab_colorscheme ? : "column");
 
        if (!*argv)
                eval += read_input(&ctl, stdin);