From 5d201da68f9efab64f9983ab2be1d44e3c09b36d Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Thu, 25 Oct 2018 14:12:16 +0200 Subject: libsmartcols: add is_last_child(), move is_last_column() Signed-off-by: Karel Zak --- libsmartcols/src/smartcolsP.h | 22 ++++++++++++++++++++++ libsmartcols/src/table_print.c | 19 ++----------------- 2 files changed, 24 insertions(+), 17 deletions(-) (limited to 'libsmartcols') diff --git a/libsmartcols/src/smartcolsP.h b/libsmartcols/src/smartcolsP.h index f45ac3a24..aab3890c1 100644 --- a/libsmartcols/src/smartcolsP.h +++ b/libsmartcols/src/smartcolsP.h @@ -213,4 +213,26 @@ static inline int scols_iter_is_last(const struct libscols_iter *itr) return itr->p == itr->head; } +static inline int is_last_child(struct libscols_line *ln) +{ + if (!ln || !ln->parent) + return 1; + + return list_entry_is_last(&ln->ln_children, &ln->parent->ln_branch); +} + + +static inline int is_last_column(struct libscols_column *cl) +{ + struct libscols_column *next; + + if (list_entry_is_last(&cl->cl_columns, &cl->table->tb_columns)) + return 1; + + next = list_entry(cl->cl_columns.next, struct libscols_column, cl_columns); + if (next && scols_column_is_hidden(next) && is_last_column(next)) + return 1; + return 0; +} + #endif /* _LIBSMARTCOLS_PRIVATE_H */ diff --git a/libsmartcols/src/table_print.c b/libsmartcols/src/table_print.c index a15bf90e3..2bdb5d90f 100644 --- a/libsmartcols/src/table_print.c +++ b/libsmartcols/src/table_print.c @@ -196,7 +196,7 @@ static int line_ascii_art_to_buffer(struct libscols_table *tb, if (rc) return rc; - if (list_entry_is_last(&ln->ln_children, &ln->parent->ln_branch)) + if (is_last_child(ln)) art = " "; else art = vertical_symbol(tb); @@ -204,21 +204,6 @@ static int line_ascii_art_to_buffer(struct libscols_table *tb, return buffer_append_data(buf, art); } -static int is_last_column(struct libscols_column *cl) -{ - int rc = list_entry_is_last(&cl->cl_columns, &cl->table->tb_columns); - struct libscols_column *next; - - if (rc) - return 1; - - next = list_entry(cl->cl_columns.next, struct libscols_column, cl_columns); - if (next && scols_column_is_hidden(next) && is_last_column(next)) - return 1; - return 0; -} - - static int has_pending_data(struct libscols_table *tb) { struct libscols_column *cl; @@ -613,7 +598,7 @@ static int cell_to_buffer(struct libscols_table *tb, if (ln->parent && !scols_table_is_json(tb)) { rc = line_ascii_art_to_buffer(tb, ln->parent, buf); - if (!rc && list_entry_is_last(&ln->ln_children, &ln->parent->ln_branch)) + if (!rc && is_last_child(ln)) rc = buffer_append_data(buf, right_symbol(tb)); else if (!rc) rc = buffer_append_data(buf, branch_symbol(tb)); -- cgit v1.2.3-55-g7522