]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libsmartcols: add scols_column_set_properties()
authorKarel Zak <kzak@redhat.com>
Wed, 23 Mar 2022 10:59:27 +0000 (11:59 +0100)
committerKarel Zak <kzak@redhat.com>
Wed, 30 Mar 2022 09:14:31 +0000 (11:14 +0200)
This function can set all column properties from comma separated
string.

Signed-off-by: Karel Zak <kzak@redhat.com>
libsmartcols/docs/libsmartcols-sections.txt
libsmartcols/src/column.c
libsmartcols/src/libsmartcols.h.in
libsmartcols/src/libsmartcols.sym
libsmartcols/src/table.c

index aa7bb1547584f730dfc601a6099bffdbba49c354..42a401e161080db25b5653fdda78aa895a41bffe 100644 (file)
@@ -42,6 +42,7 @@ scols_column_set_color
 scols_column_set_flags
 scols_column_set_json_type
 scols_column_set_name
+scols_column_set_properties
 scols_column_set_safechars
 scols_column_set_whint
 scols_column_set_wrapfunc
index d5a00f073901238e4fb340cdd5b21a5c85d8dd80..0283b976f41ee82d37a1272062e2dda96e10eca3 100644 (file)
@@ -23,7 +23,7 @@
 #include <ctype.h>
 
 #include "mbsalign.h"
-
+#include "strutils.h"
 #include "smartcolsP.h"
 
 /**
@@ -641,3 +641,96 @@ int scols_column_is_customwrap(const struct libscols_column *cl)
                && cl->wrap_chunksize
                && cl->wrap_nextchunk ? 1 : 0;
 }
+
+/**
+ * scols_column_set_properties:
+ * @cl: a pointer to a struct libscols_column instance
+ * @opts: options string
+ *
+ * Set properties from string, the string is comma seprated list, like
+ * "trunc,right,json=number", ...
+ *
+ * Returns: 0 on success, <0 on error
+ *
+ * Since: 2.39
+ */
+int scols_column_set_properties(struct libscols_column *cl, const char *opts)
+{
+       char *str = (char *) opts;
+       char *name, *value;
+       size_t namesz, valuesz;
+       unsigned int flags = 0;
+       int rc = 0;
+
+       DBG(COL, ul_debugobj(cl, "apply properties '%s'", opts));
+
+       while (rc == 0
+              && !ul_optstr_next(&str, &name, &namesz, &value, &valuesz)) {
+
+               if (strncmp(name, "trunc", namesz) == 0)
+                       flags |= SCOLS_FL_TRUNC;
+
+               else if (strncmp(name, "tree", namesz) == 0)
+                       flags |= SCOLS_FL_TREE;
+
+               else if (strncmp(name, "right", namesz) == 0)
+                       flags |= SCOLS_FL_RIGHT;
+
+               else if (strncmp(name, "strictwidth", namesz) == 0)
+                       flags |= SCOLS_FL_STRICTWIDTH;
+
+               else if (strncmp(name, "noextremes", namesz) == 0)
+                       flags |= SCOLS_FL_STRICTWIDTH;
+
+               else if (strncmp(name, "hidden", namesz) == 0)
+                       flags |= SCOLS_FL_HIDDEN;
+
+               else if (strncmp(name, "wrap", namesz) == 0)
+                       flags |= SCOLS_FL_WRAP;
+
+               else if (value && strncmp(name, "json", namesz) == 0) {
+
+                       if (strncmp(value, "string", valuesz) == 0)
+                               rc = scols_column_set_json_type(cl, SCOLS_JSON_STRING);
+                       else if (strncmp(value, "number", valuesz) == 0)
+                               rc = scols_column_set_json_type(cl, SCOLS_JSON_NUMBER);
+                       else if (strncmp(value, "array-string", valuesz) == 0)
+                               rc = scols_column_set_json_type(cl, SCOLS_JSON_ARRAY_STRING);
+                       else if (strncmp(value, "array-number", valuesz) == 0)
+                               rc = scols_column_set_json_type(cl, SCOLS_JSON_ARRAY_NUMBER);
+                       else if (strncmp(value, "boolean", valuesz) == 0)
+                               rc = scols_column_set_json_type(cl, SCOLS_JSON_BOOLEAN);
+
+               } else if (value && strncmp(name, "width", namesz) == 0) {
+
+                       char *end = NULL;
+                       double x = strtod(value, &end);
+                       if (errno || str == end)
+                               return -EINVAL;
+
+                       rc = scols_column_set_whint(cl, x);
+
+               } else if (value && strncmp(name, "color", namesz) == 0) {
+
+                       char *x = strndup(value, valuesz);
+                       if (x) {
+                               scols_column_set_color(cl, x);
+                               free(x);
+                       }
+
+               } else if (value && strncmp(name, "name", namesz) == 0) {
+
+                       char *x = strndup(value, valuesz);
+                       if (x) {
+                               scols_column_set_name(cl, x);
+                               free(x);
+                       }
+               }
+       }
+
+       if (!rc && flags)
+               rc = scols_column_set_flags(cl, flags);
+
+       return rc;
+}
+
index 6b64bc81f8eea6d0030bfdfff260a63f4e8c4e3a..f5820e9c76c1e5cf16fa2c37711c79096a1f4120 100644 (file)
@@ -194,6 +194,8 @@ extern int scols_column_set_name(struct libscols_column *cl, const char *name);
 extern const char *scols_column_get_name(struct libscols_column *cl);
 extern const char *scols_column_get_name_as_shellvar(struct libscols_column *cl);
 
+extern int scols_column_set_properties(struct libscols_column *cl, const char *opts);
+
 extern int scols_column_set_cmpfunc(struct libscols_column *cl,
                        int (*cmp)(struct libscols_cell *a,
                                   struct libscols_cell *b, void *),
@@ -290,6 +292,7 @@ extern const char *scols_table_get_line_separator(const struct libscols_table *t
 extern size_t scols_table_get_ncols(const struct libscols_table *tb);
 extern size_t scols_table_get_nlines(const struct libscols_table *tb);
 extern struct libscols_column *scols_table_get_column(struct libscols_table *tb, size_t n);
+struct libscols_column *scols_table_get_column_by_name(struct libscols_table *tb, const char *name);
 extern int scols_table_add_line(struct libscols_table *tb, struct libscols_line *ln);
 extern int scols_table_remove_line(struct libscols_table *tb, struct libscols_line *ln);
 extern void scols_table_remove_lines(struct libscols_table *tb);
index 309535f7b939c5c87b36f150397c021687727402..4499908cfa11cf532989e1863401b789bbf1c3de 100644 (file)
@@ -209,3 +209,9 @@ SMARTCOLS_2.38 {
        scols_table_is_shellvar;
        scols_table_enable_shellvar;
 } SMARTCOLS_2.35;
+
+
+SMARTCOLS_2.39 {
+       scols_column_set_properties;
+       scols_table_get_column_by_name;
+} SMARTCOLS_2.38;
index 30e9194fc556db73571fb69299e7bc1b538e4e88..cab2b455e76e0c412817fdfeb8e28a584c344c49 100644 (file)
@@ -425,7 +425,7 @@ struct libscols_column *scols_table_new_column(struct libscols_table *tb,
        if (!cl)
                return NULL;
 
-       if (scols_column_set_name(cl, name))
+       if (name && scols_column_set_name(cl, name))
                goto err;
        scols_column_set_whint(cl, whint);
        scols_column_set_flags(cl, flags);
@@ -607,6 +607,35 @@ struct libscols_column *scols_table_get_column(struct libscols_table *tb,
        return NULL;
 }
 
+/**
+ * scols_table_get_column_ny_name
+ * @tb: table
+ * @name: column name
+ *
+ * Returns: pointer to column or NULL
+ *
+ * Since: 2.39
+ */
+struct libscols_column *scols_table_get_column_by_name(
+                               struct libscols_table *tb, const char *name)
+{
+       struct libscols_iter itr;
+       struct libscols_column *cl;
+
+       if (!tb || !name)
+               return NULL;
+
+       scols_reset_iter(&itr, SCOLS_ITER_FORWARD);
+       while (scols_table_next_column(tb, &itr, &cl) == 0) {
+               const char *cn = scols_column_get_name(cl);
+
+               if (cn && strcmp(cn, name) == 0)
+                       return cl;
+       }
+       return NULL;
+}
+
+
 /**
  * scols_table_add_line:
  * @tb: table