From 2a64fccdd3acea0996f0d8b38574eda12dab0eea Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Tue, 15 Jul 2025 15:37:56 +0200 Subject: [PATCH] libsmartcols: add function to set/get header colors Signed-off-by: Karel Zak --- libsmartcols/docs/libsmartcols-sections.txt | 2 + libsmartcols/src/column.c | 54 +++++++++++++++++++-- libsmartcols/src/libsmartcols.h.in | 4 ++ libsmartcols/src/libsmartcols.sym | 2 + 4 files changed, 59 insertions(+), 3 deletions(-) diff --git a/libsmartcols/docs/libsmartcols-sections.txt b/libsmartcols/docs/libsmartcols-sections.txt index e7fe4b75d..da4b8c8f4 100644 --- a/libsmartcols/docs/libsmartcols-sections.txt +++ b/libsmartcols/docs/libsmartcols-sections.txt @@ -28,6 +28,7 @@ scols_column_get_color scols_column_get_data_type scols_column_get_flags scols_column_get_header +scols_column_get_headercolor scols_column_get_json_type scols_column_get_name scols_column_get_name_as_shellvar @@ -51,6 +52,7 @@ scols_column_set_color scols_column_set_data_func scols_column_set_data_type scols_column_set_flags +scols_column_set_headercolor scols_column_set_json_type scols_column_set_name scols_column_set_properties diff --git a/libsmartcols/src/column.c b/libsmartcols/src/column.c index e83d297fc..5d5cb0aa6 100644 --- a/libsmartcols/src/column.c +++ b/libsmartcols/src/column.c @@ -319,6 +319,44 @@ const char *scols_column_get_name(struct libscols_column *cl) return scols_cell_get_data(&cl->header); } +/** + * scols_column_set_headercolor: + * @cl: a pointer to a struct libscols_column instance + * @color: color name or ESC sequence + * + * The set header color. + * + * Returns: 0, a negative value in case of an error. + * + * Since: 2.42 + */ +int scols_column_set_headercolor(struct libscols_column *cl, const char *color) +{ + struct libscols_cell *hdr = scols_column_get_header(cl); + + if (!hdr) + return -EINVAL; + + return scols_cell_set_color(hdr, color); +} + +/** + * scols_column_get_headercolor: + * @cl: a pointer to a struct libscols_column instance + * + * Returns: The current color setting of the column header or NULL. + * + * Since: 2.42 + */ +const char *scols_column_get_headercolor(struct libscols_column *cl) +{ + struct libscols_cell *hdr = scols_column_get_header(cl); + + if (hdr) + return scols_cell_get_color(hdr); + return NULL; +} + /** * scols_shellvar_name: * @name: raw (column) name @@ -404,7 +442,7 @@ const char *scols_column_get_name_as_shellvar(struct libscols_column *cl) * The default color for data cells and column header. * * If you want to set header specific color then use scols_column_get_header() - * and scols_cell_set_color(). + * and scols_cell_set_color() or scols_column_set_headercolor() since v2.42. * * If you want to set data cell specific color the use scols_line_get_cell() + * scols_cell_set_color(). @@ -877,8 +915,10 @@ int scols_column_is_customwrap(const struct libscols_column *cl) * @cl: a pointer to a struct libscols_column instance * @opts: options string * - * Set properties from string, the string is comma separated list, like - * "trunc,right,json=number", ... + * Set properties from string, the string is comma separated list. Supported + * properties are: trunc, tree, right, strictwidth, noextremes, hidden, wrap, + * wrapnl, wrapzero,json={string,number,float,array-string,array-number,boolean}, + * width=, color=, headercolor= and name=. * * Returns: 0 on success, <0 on error * @@ -964,6 +1004,14 @@ int scols_column_set_properties(struct libscols_column *cl, const char *opts) free(x); } + } else if (value && strncmp(name, "headercolor", namesz) == 0) { + + char *x = strndup(value, valuesz); + if (x) { + scols_column_set_headercolor(cl, x); + free(x); + } + } else if (value && strncmp(name, "name", namesz) == 0) { char *x = strndup(value, valuesz); diff --git a/libsmartcols/src/libsmartcols.h.in b/libsmartcols/src/libsmartcols.h.in index abb9cdd6c..3fbe668d3 100644 --- a/libsmartcols/src/libsmartcols.h.in +++ b/libsmartcols/src/libsmartcols.h.in @@ -250,7 +250,11 @@ extern void scols_unref_column(struct libscols_column *cl); extern struct libscols_column *scols_copy_column(const struct libscols_column *cl); extern int scols_column_set_whint(struct libscols_column *cl, double whint); extern double scols_column_get_whint(const struct libscols_column *cl); + extern struct libscols_cell *scols_column_get_header(struct libscols_column *cl); +extern int scols_column_set_headercolor(struct libscols_column *cl, const char *color); +extern const char *scols_column_get_headercolor(struct libscols_column *cl); + extern int scols_column_set_color(struct libscols_column *cl, const char *color); extern const char *scols_column_get_color(const struct libscols_column *cl); extern struct libscols_table *scols_column_get_table(const struct libscols_column *cl); diff --git a/libsmartcols/src/libsmartcols.sym b/libsmartcols/src/libsmartcols.sym index 6fe8f6187..1f782fdc6 100644 --- a/libsmartcols/src/libsmartcols.sym +++ b/libsmartcols/src/libsmartcols.sym @@ -259,5 +259,7 @@ SMARTCOLS_2.41 { SMARTCOLS_2.42 { scols_filter_has_holder; + scols_column_set_headercolor; + scols_column_get_headercolor; } SMARTCOLS_2.41; -- 2.47.2