From ea7fb72eb25bdfee46d612f91f47c36ba78c4627 Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Fri, 3 May 2019 17:12:18 +0200 Subject: libsmartcols: (groups) use print functions tp calculate grpset Now we have extra code to calculate grpset. It seems better to use only one way how we wall the tree. Signed-off-by: Karel Zak --- libsmartcols/src/calculate.c | 22 ++++++++++---- libsmartcols/src/grouping.c | 70 ++----------------------------------------- libsmartcols/src/print.c | 4 ++- libsmartcols/src/smartcolsP.h | 2 +- 4 files changed, 23 insertions(+), 75 deletions(-) (limited to 'libsmartcols') diff --git a/libsmartcols/src/calculate.c b/libsmartcols/src/calculate.c index 60b6a8315..7eb9c4cf0 100644 --- a/libsmartcols/src/calculate.c +++ b/libsmartcols/src/calculate.c @@ -88,6 +88,7 @@ static int count_column_width(struct libscols_table *tb, else len = mbs_safe_width(data); + if (len == (size_t) -1) /* ignore broken multibyte strings */ len = 0; cl->width_max = max(len, cl->width_max); @@ -105,6 +106,19 @@ static int count_column_width(struct libscols_table *tb, } } + if (scols_column_is_tree(cl) && has_groups(tb)) { + /* We don't fill buffer with groups tree ascii art during width + * calcualtion. The print function only enlarge grpset[] and we + * calculate final width from grpset_size. + */ + size_t gprwidth = tb->grpset_size + 1; + cl->width_treeart += gprwidth; + cl->width_max += gprwidth; + cl->width += gprwidth; + if (extreme_count) + extreme_sum += gprwidth; + } + if (extreme_count && cl->width_avg == 0) { cl->width_avg = extreme_sum / extreme_count; if (cl->width_avg && cl->width_max > cl->width_avg * 2) @@ -145,15 +159,12 @@ int __scols_calculate(struct libscols_table *tb, struct libscols_buffer *buf) DBG(TAB, ul_debugobj(tb, "-----calculate-(termwidth=%zu)-----", tb->termwidth)); + tb->is_dummy_print = 1; colsepsz = mbs_safe_width(colsep(tb)); - if (has_groups(tb)) { - rc = scols_groups_calculate_grpset(tb); - if (rc) - goto done; + if (has_groups(tb)) group_ncolumns = 1; - } /* set basic columns width */ @@ -398,6 +409,7 @@ int __scols_calculate(struct libscols_table *tb, struct libscols_buffer *buf) } } done: + tb->is_dummy_print = 0; DBG(TAB, ul_debugobj(tb, "-----final width: %zu (rc=%d)-----", width, rc)); ON_DBG(TAB, dbg_columns(tb)); diff --git a/libsmartcols/src/grouping.c b/libsmartcols/src/grouping.c index c8515cc92..0e4851e30 100644 --- a/libsmartcols/src/grouping.c +++ b/libsmartcols/src/grouping.c @@ -158,7 +158,6 @@ static inline const char *group_state_to_string(int state) return grpstates[state]; } - /* static void grpset_debug(struct libscols_table *tb, struct libscols_line *ln) { @@ -181,7 +180,6 @@ static void grpset_debug(struct libscols_table *tb, struct libscols_line *ln) } } */ - static int group_state_for_line(struct libscols_group *gr, struct libscols_line *ln) { if (gr->state == SCOLS_GSTATE_NONE && @@ -248,12 +246,10 @@ static struct libscols_group **grpset_locate_freespace(struct libscols_table *tb if (!tb->grpset_size) prepend = 0; - /* DBG(TAB, ul_debugobj(tb, "orig grpset:")); grpset_debug(tb, NULL); */ - if (prepend) { for (i = tb->grpset_size - 1; ; i--) { if (tb->grpset[i] == NULL) { @@ -329,11 +325,11 @@ static int grpset_update(struct libscols_table *tb, struct libscols_line *ln, st struct libscols_group **xx; int state; - DBG(LINE, ul_debugobj(ln, " group [%p] grpset update", gr)); + DBG(LINE, ul_debugobj(ln, " group [%p] grpset update [grpset size=%zu]", gr, tb->grpset_size)); /* new state, note that gr->state still holds the original state */ state = group_state_for_line(gr, ln); - DBG(LINE, ul_debugobj(ln, " state old='%s', new='%s'", + DBG(LINE, ul_debugobj(ln, " state %s --> %s", group_state_to_string(gr->state), group_state_to_string(state))); @@ -411,68 +407,6 @@ int scols_groups_update_grpset(struct libscols_table *tb, struct libscols_line * return rc; } -static int groups_calculate_grpset(struct libscols_table *tb, struct libscols_line *ln) -{ - struct libscols_iter itr; - struct libscols_line *child; - int rc = 0; - - DBG(LINE, ul_debugobj(ln, " grpset calculate")); - - /* current line */ - rc = scols_groups_update_grpset(tb, ln); - if (rc) - goto done; - - /* line's children */ - if (has_children(ln)) { - scols_reset_iter(&itr, SCOLS_ITER_FORWARD); - while (scols_line_next_child(ln, &itr, &child) == 0) { - rc = groups_calculate_grpset(tb, child); - if (rc) - goto done; - } - } - - /* group's children */ - if (is_last_group_member(ln) && has_group_children(ln)) { - scols_reset_iter(&itr, SCOLS_ITER_FORWARD); - while (scols_line_next_group_child(ln, &itr, &child) == 0) { - rc = groups_calculate_grpset(tb, child); - if (rc) - goto done; - } - } -done: - return rc; -} - - -int scols_groups_calculate_grpset(struct libscols_table *tb) -{ - struct libscols_iter itr; - struct libscols_line *ln; - int rc = 0; - - DBG(TAB, ul_debugobj(tb, "grpset calculate [top-level] ->")); - - scols_groups_reset_state(tb); - - scols_reset_iter(&itr, SCOLS_ITER_FORWARD); - while (scols_table_next_line(tb, &itr, &ln) == 0) { - if (ln->parent || ln->parent_group) - continue; - rc = groups_calculate_grpset(tb, ln); - if (rc) - break; - } - - scols_groups_reset_state(tb); - DBG(TAB, ul_debugobj(tb, "<- done grpset calculate [top-level, rc=%d, size=%zu]", - rc, tb->grpset_size)); - return rc; -} - void scols_groups_reset_state(struct libscols_table *tb) { struct libscols_iter itr; diff --git a/libsmartcols/src/print.c b/libsmartcols/src/print.c index 526b0b763..3691dba7b 100644 --- a/libsmartcols/src/print.c +++ b/libsmartcols/src/print.c @@ -101,7 +101,7 @@ static int groups_ascii_art_to_buffer( struct libscols_table *tb, size_t i, rest = 0; const char *filler = cellpadding_symbol(tb); - if (!has_groups(tb) || !tb->grpset) + if (!has_groups(tb)) return 0; DBG(LINE, ul_debugobj(ln, "printing groups chart")); @@ -109,6 +109,8 @@ static int groups_ascii_art_to_buffer( struct libscols_table *tb, rc = scols_groups_update_grpset(tb, ln); if (rc) return rc; + if (tb->is_dummy_print) + return 0; /* allocate grpset[] only */ for (i = 0; i < tb->grpset_size; i += SCOLS_GRPSET_CHUNKSIZ) { struct libscols_group *gr = tb->grpset[i]; diff --git a/libsmartcols/src/smartcolsP.h b/libsmartcols/src/smartcolsP.h index c063db212..5a06d3f7e 100644 --- a/libsmartcols/src/smartcolsP.h +++ b/libsmartcols/src/smartcolsP.h @@ -231,6 +231,7 @@ struct libscols_table { colors_wanted :1, /* enable colors */ is_term :1, /* isatty() */ padding_debug :1, /* output visible padding chars */ + is_dummy_print :1, /* printing used for width calcualion only */ maxout :1, /* maximize output */ header_repeat :1, /* print header after libscols_table->termheight */ header_printed :1, /* header already printed */ @@ -309,7 +310,6 @@ void scols_group_remove_children(struct libscols_group *gr); void scols_group_remove_members(struct libscols_group *gr); void scols_unref_group(struct libscols_group *gr); void scols_groups_fix_members_order(struct libscols_table *tb); -int scols_groups_calculate_grpset(struct libscols_table *tb); int scols_groups_update_grpset(struct libscols_table *tb, struct libscols_line *ln); void scols_groups_reset_state(struct libscols_table *tb); struct libscols_group *scols_grpset_get_printable_children(struct libscols_table *tb); -- cgit v1.2.3-55-g7522