]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libsmartcols: add table cursor
authorKarel Zak <kzak@redhat.com>
Thu, 12 Oct 2023 09:57:10 +0000 (11:57 +0200)
committerKarel Zak <kzak@redhat.com>
Mon, 23 Oct 2023 19:54:00 +0000 (21:54 +0200)
The cursor makes it simple for callbacks to access information about
the current cell, line and column. It will reduce number of necessary
arguments for callbacks.

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

index f5820e9c76c1e5cf16fa2c37711c79096a1f4120..ab54c0efe53ffec80545f48367ff99a1b79e1a7d 100644 (file)
@@ -310,6 +310,12 @@ extern int scols_table_reduce_termwidth(struct libscols_table *tb, size_t reduce
 
 extern int scols_sort_table(struct libscols_table *tb, struct libscols_column *cl);
 extern int scols_sort_table_by_tree(struct libscols_table *tb);
+
+extern int scols_table_get_cursor(struct libscols_table *tb,
+                           struct libscols_line **ln,
+                           struct libscols_column **cl,
+                           struct libscols_cell **ce);
+
 /*
  *
  */
index 4499908cfa11cf532989e1863401b789bbf1c3de..4fa6df60405d0c3b045c0e733fbc047d70bdbfe3 100644 (file)
@@ -210,8 +210,11 @@ SMARTCOLS_2.38 {
        scols_table_enable_shellvar;
 } SMARTCOLS_2.35;
 
-
 SMARTCOLS_2.39 {
        scols_column_set_properties;
        scols_table_get_column_by_name;
 } SMARTCOLS_2.38;
+
+SMARTCOLS_2.40 {
+       scols_table_get_cursor;
+} SMARTCOLS_2.39;
index 8a7ee9b5d7244bd082076bb6d9e80ad402aba28d..89a7f6ec4a64e6fb6f97bc70532be28991d7549d 100644 (file)
@@ -247,6 +247,10 @@ struct libscols_table {
 
        const char *cur_color;  /* current active color when printing */
 
+       struct libscols_cell *cur_cell;         /* currently used cell */
+       struct libscols_line *cur_line;         /* currently used line */
+       struct libscols_column *cur_column;     /* currently used column */
+
        /* flags */
        unsigned int    ascii           :1,     /* don't use unicode */
                        colors_wanted   :1,     /* enable colors */
@@ -306,6 +310,11 @@ int scols_line_next_group_child(struct libscols_line *ln,
 int scols_table_next_group(struct libscols_table *tb,
                           struct libscols_iter *itr,
                           struct libscols_group **gr);
+int scols_table_set_cursor(struct libscols_table *tb,
+                           struct libscols_line *ln,
+                           struct libscols_column *cl,
+                           struct libscols_cell *ce);
+
 
 /*
  * grouping.c
index 8449c4fae56d2e956ac459c9b271c34f2f346e80..0be68f2b835bde657e50a6f450eb03343587d451 100644 (file)
@@ -521,6 +521,50 @@ size_t scols_table_get_nlines(const struct libscols_table *tb)
        return tb->nlines;
 }
 
+
+int scols_table_set_cursor(struct libscols_table *tb,
+                          struct libscols_line *ln,
+                          struct libscols_column *cl,
+                          struct libscols_cell *ce)
+{
+       if (!tb)
+               return -EINVAL;
+
+       tb->cur_line = ln;
+       tb->cur_column = cl;
+       tb->cur_cell = ce;
+
+       return 0;
+}
+
+/**
+ * scols_table_get_cursor:
+ * @tb: table
+ * @ln: returns current line (optional)
+ * @cl: returns current column (optional)
+ * @ce: returns current cell (optional)
+ *
+ * Returns: 0 on success, negative number in case of error.
+ *
+ * Since: 2.40
+ */
+int scols_table_get_cursor(struct libscols_table *tb,
+                          struct libscols_line **ln,
+                          struct libscols_column **cl,
+                          struct libscols_cell **ce)
+{
+       if (!tb)
+               return -EINVAL;
+
+       if (ln)
+               *ln = tb->cur_line;
+       if (cl)
+               *cl = tb->cur_column;
+       if (ce)
+               *ce = tb->cur_cell;
+       return 0;
+}
+
 /**
  * scols_table_set_stream:
  * @tb: table