From: Karel Zak Date: Thu, 12 Oct 2023 09:57:10 +0000 (+0200) Subject: libsmartcols: add table cursor X-Git-Tag: v2.40-rc1~194 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=97448130fe342813ebd0453ed0280ac18d287629;p=thirdparty%2Futil-linux.git libsmartcols: add table cursor 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 --- diff --git a/libsmartcols/src/libsmartcols.h.in b/libsmartcols/src/libsmartcols.h.in index f5820e9c76..ab54c0efe5 100644 --- a/libsmartcols/src/libsmartcols.h.in +++ b/libsmartcols/src/libsmartcols.h.in @@ -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); + /* * */ diff --git a/libsmartcols/src/libsmartcols.sym b/libsmartcols/src/libsmartcols.sym index 4499908cfa..4fa6df6040 100644 --- a/libsmartcols/src/libsmartcols.sym +++ b/libsmartcols/src/libsmartcols.sym @@ -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; diff --git a/libsmartcols/src/smartcolsP.h b/libsmartcols/src/smartcolsP.h index 8a7ee9b5d7..89a7f6ec4a 100644 --- a/libsmartcols/src/smartcolsP.h +++ b/libsmartcols/src/smartcolsP.h @@ -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 diff --git a/libsmartcols/src/table.c b/libsmartcols/src/table.c index 8449c4fae5..0be68f2b83 100644 --- a/libsmartcols/src/table.c +++ b/libsmartcols/src/table.c @@ -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