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 <kzak@redhat.com>
This commit is contained in:
Karel Zak 2019-05-03 17:12:18 +02:00
parent b94932c063
commit ea7fb72eb2
4 changed files with 23 additions and 75 deletions

View File

@ -88,6 +88,7 @@ static int count_column_width(struct libscols_table *tb,
else else
len = mbs_safe_width(data); len = mbs_safe_width(data);
if (len == (size_t) -1) /* ignore broken multibyte strings */ if (len == (size_t) -1) /* ignore broken multibyte strings */
len = 0; len = 0;
cl->width_max = max(len, cl->width_max); 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) { if (extreme_count && cl->width_avg == 0) {
cl->width_avg = extreme_sum / extreme_count; cl->width_avg = extreme_sum / extreme_count;
if (cl->width_avg && cl->width_max > cl->width_avg * 2) 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)); DBG(TAB, ul_debugobj(tb, "-----calculate-(termwidth=%zu)-----", tb->termwidth));
tb->is_dummy_print = 1;
colsepsz = mbs_safe_width(colsep(tb)); colsepsz = mbs_safe_width(colsep(tb));
if (has_groups(tb)) { if (has_groups(tb))
rc = scols_groups_calculate_grpset(tb);
if (rc)
goto done;
group_ncolumns = 1; group_ncolumns = 1;
}
/* set basic columns width /* set basic columns width
*/ */
@ -398,6 +409,7 @@ int __scols_calculate(struct libscols_table *tb, struct libscols_buffer *buf)
} }
} }
done: done:
tb->is_dummy_print = 0;
DBG(TAB, ul_debugobj(tb, "-----final width: %zu (rc=%d)-----", width, rc)); DBG(TAB, ul_debugobj(tb, "-----final width: %zu (rc=%d)-----", width, rc));
ON_DBG(TAB, dbg_columns(tb)); ON_DBG(TAB, dbg_columns(tb));

View File

@ -158,7 +158,6 @@ static inline const char *group_state_to_string(int state)
return grpstates[state]; return grpstates[state];
} }
/* /*
static void grpset_debug(struct libscols_table *tb, struct libscols_line *ln) 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) static int group_state_for_line(struct libscols_group *gr, struct libscols_line *ln)
{ {
if (gr->state == SCOLS_GSTATE_NONE && 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) if (!tb->grpset_size)
prepend = 0; prepend = 0;
/* /*
DBG(TAB, ul_debugobj(tb, "orig grpset:")); DBG(TAB, ul_debugobj(tb, "orig grpset:"));
grpset_debug(tb, NULL); grpset_debug(tb, NULL);
*/ */
if (prepend) { if (prepend) {
for (i = tb->grpset_size - 1; ; i--) { for (i = tb->grpset_size - 1; ; i--) {
if (tb->grpset[i] == NULL) { 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; struct libscols_group **xx;
int state; 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 */ /* new state, note that gr->state still holds the original state */
state = group_state_for_line(gr, ln); 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(gr->state),
group_state_to_string(state))); group_state_to_string(state)));
@ -411,68 +407,6 @@ int scols_groups_update_grpset(struct libscols_table *tb, struct libscols_line *
return rc; 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) void scols_groups_reset_state(struct libscols_table *tb)
{ {
struct libscols_iter itr; struct libscols_iter itr;

View File

@ -101,7 +101,7 @@ static int groups_ascii_art_to_buffer( struct libscols_table *tb,
size_t i, rest = 0; size_t i, rest = 0;
const char *filler = cellpadding_symbol(tb); const char *filler = cellpadding_symbol(tb);
if (!has_groups(tb) || !tb->grpset) if (!has_groups(tb))
return 0; return 0;
DBG(LINE, ul_debugobj(ln, "printing groups chart")); 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); rc = scols_groups_update_grpset(tb, ln);
if (rc) if (rc)
return rc; return rc;
if (tb->is_dummy_print)
return 0; /* allocate grpset[] only */
for (i = 0; i < tb->grpset_size; i += SCOLS_GRPSET_CHUNKSIZ) { for (i = 0; i < tb->grpset_size; i += SCOLS_GRPSET_CHUNKSIZ) {
struct libscols_group *gr = tb->grpset[i]; struct libscols_group *gr = tb->grpset[i];

View File

@ -231,6 +231,7 @@ struct libscols_table {
colors_wanted :1, /* enable colors */ colors_wanted :1, /* enable colors */
is_term :1, /* isatty() */ is_term :1, /* isatty() */
padding_debug :1, /* output visible padding chars */ padding_debug :1, /* output visible padding chars */
is_dummy_print :1, /* printing used for width calcualion only */
maxout :1, /* maximize output */ maxout :1, /* maximize output */
header_repeat :1, /* print header after libscols_table->termheight */ header_repeat :1, /* print header after libscols_table->termheight */
header_printed :1, /* header already printed */ 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_group_remove_members(struct libscols_group *gr);
void scols_unref_group(struct libscols_group *gr); void scols_unref_group(struct libscols_group *gr);
void scols_groups_fix_members_order(struct libscols_table *tb); 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); int scols_groups_update_grpset(struct libscols_table *tb, struct libscols_line *ln);
void scols_groups_reset_state(struct libscols_table *tb); void scols_groups_reset_state(struct libscols_table *tb);
struct libscols_group *scols_grpset_get_printable_children(struct libscols_table *tb); struct libscols_group *scols_grpset_get_printable_children(struct libscols_table *tb);