aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKarel Zak <kzak@redhat.com>2025-07-15 15:37:56 +0200
committerKarel Zak <kzak@redhat.com>2025-07-15 15:37:56 +0200
commit2a64fccdd3acea0996f0d8b38574eda12dab0eea (patch)
treec1f4ec76d1f199400d2ea0d53b0b991c49e2d5c0
parentb38f39fc9a4f14a9c96ccbf4b1f5c6297b678eaa (diff)
downloadutil-linux-2a64fccdd3acea0996f0d8b38574eda12dab0eea.tar.gz
libsmartcols: add function to set/get header colors
Signed-off-by: Karel Zak <kzak@redhat.com>
-rw-r--r--libsmartcols/docs/libsmartcols-sections.txt2
-rw-r--r--libsmartcols/src/column.c54
-rw-r--r--libsmartcols/src/libsmartcols.h.in4
-rw-r--r--libsmartcols/src/libsmartcols.sym2
4 files changed, 59 insertions, 3 deletions
diff --git a/libsmartcols/docs/libsmartcols-sections.txt b/libsmartcols/docs/libsmartcols-sections.txt
index e7fe4b75d5..da4b8c8f42 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 e83d297fc5..5d5cb0aa6c 100644
--- a/libsmartcols/src/column.c
+++ b/libsmartcols/src/column.c
@@ -320,6 +320,44 @@ const char *scols_column_get_name(struct libscols_column *cl)
}
/**
+ * 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
* @buf: buffer to returns normalized 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 abb9cdd6c1..3fbe668d3f 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 6fe8f61879..1f782fdc6c 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;