From 707677ce30090d685e71df52bc40684fd87c435c Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Tue, 15 Jul 2025 15:38:46 +0200 Subject: [PATCH] column: add support for color scheme Signed-off-by: Karel Zak --- bash-completion/column | 1 + text-utils/column.1.adoc | 37 ++++++++++++++++++++++++++++++++++++- text-utils/column.c | 37 +++++++++++++++++++++++++++++++++++-- 3 files changed, 72 insertions(+), 3 deletions(-) diff --git a/bash-completion/column b/bash-completion/column index 6e0d2f283..fb209204d 100644 --- a/bash-completion/column +++ b/bash-completion/column @@ -33,6 +33,7 @@ _column_module() -*) OPTS="--columns --table + --table-colorscheme --table-name --table-order --table-columns diff --git a/text-utils/column.1.adoc b/text-utils/column.1.adoc index a331c381d..de530644f 100644 --- a/text-utils/column.1.adoc +++ b/text-utils/column.1.adoc @@ -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. diff --git a/text-utils/column.c b/text-utils/column.c index 7a8b36415..0571c7463 100644 --- a/text-utils/column.c +++ b/text-utils/column.c @@ -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 table name for JSON output\n"), out); fputs(_(" -O, --table-order specify order of output columns\n"), out); + fputs(_(" --table-colorscheme specify color scheme name\n"), out); fputs(_(" -C, --table-column define column\n"), out); fputs(_(" -N, --table-columns comma separated columns names\n"), out); fputs(_(" -l, --table-columns-limit 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); -- 2.47.2