From 4525b6cf1632b6912621761a7de5c97bde1b5da3 Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Thu, 2 May 2019 13:44:12 +0200 Subject: libsmartcols: (groups) remove hardcoded const numbers Signed-off-by: Karel Zak --- libsmartcols/src/grouping.c | 24 ++++++++++++------------ libsmartcols/src/print.c | 6 +++--- libsmartcols/src/smartcolsP.h | 5 +++++ 3 files changed, 20 insertions(+), 15 deletions(-) (limited to 'libsmartcols') diff --git a/libsmartcols/src/grouping.c b/libsmartcols/src/grouping.c index add4b748a..d01a73330 100644 --- a/libsmartcols/src/grouping.c +++ b/libsmartcols/src/grouping.c @@ -220,25 +220,28 @@ static int group_state_for_line(struct libscols_group *gr, struct libscols_line return SCOLS_GSTATE_NONE; } -/* For now we assume that each active group is just 3 columns width. Later we can make it dynamic... +/* + * apply new @state to the chunk (addresesd by @xx) of grpset used for the group (@gr) */ static void grpset_apply_group_state(struct libscols_group **xx, int state, struct libscols_group *gr) { + size_t i; + DBG(GROUP, ul_debugobj(gr, " applying state to grpset")); /* gr->state holds the old state, @state is the new state */ - if (state == SCOLS_GSTATE_NONE) - xx[0] = xx[1] = xx[2] = NULL; - else - xx[0] = xx[1] = xx[2] = gr; + for (i = 0; i < SCOLS_GRPSET_CHUNKSIZ; i++) + xx[i] = state == SCOLS_GSTATE_NONE ? NULL : gr; + gr->state = state; } -static struct libscols_group **grpset_locate_freespace(struct libscols_table *tb, size_t wanted, int prepend) +static struct libscols_group **grpset_locate_freespace(struct libscols_table *tb, int chunks, int prepend) { size_t i, avail = 0; struct libscols_group **tmp, **first = NULL; + const size_t wanted = chunks * SCOLS_GRPSET_CHUNKSIZ; if (!tb->grpset_size) prepend = 0; @@ -268,8 +271,8 @@ static struct libscols_group **grpset_locate_freespace(struct libscols_table *tb } } - DBG(TAB, ul_debugobj(tb, " realocate grpset [sz: old=%zu, new=%zu]", - tb->grpset_size, tb->grpset_size + wanted)); + DBG(TAB, ul_debugobj(tb, " realocate grpset [sz: old=%zu, new=%zu, new_chunks=%d]", + tb->grpset_size, tb->grpset_size + wanted, chunks)); tmp = realloc(tb->grpset, (tb->grpset_size + wanted) * sizeof(struct libscols_group *)); if (!tmp) @@ -310,9 +313,6 @@ static struct libscols_group **grpset_locate_group(struct libscols_table *tb, st } - -#define SCOLS_GRPSET_MINSZ 3 - static int grpset_update(struct libscols_table *tb, struct libscols_line *ln, struct libscols_group *gr) { struct libscols_group **xx; @@ -349,7 +349,7 @@ static int grpset_update(struct libscols_table *tb, struct libscols_line *ln, st /* locate place in grpset where we draw the group */ if (!tb->grpset || gr->state == SCOLS_GSTATE_NONE) - xx = grpset_locate_freespace(tb, SCOLS_GRPSET_MINSZ, 1); + xx = grpset_locate_freespace(tb, 1, 1); else xx = grpset_locate_group(tb, gr); if (!xx) { diff --git a/libsmartcols/src/print.c b/libsmartcols/src/print.c index a017b636b..ec2224f36 100644 --- a/libsmartcols/src/print.c +++ b/libsmartcols/src/print.c @@ -110,11 +110,11 @@ static int groups_ascii_art_to_buffer( struct libscols_table *tb, if (rc) return rc; - for (i = 0; i < tb->grpset_size; i+=3) { + for (i = 0; i < tb->grpset_size; i += SCOLS_GRPSET_CHUNKSIZ) { struct libscols_group *gr = tb->grpset[i]; if (!gr) { - buffer_append_ntimes(buf, 3, cellpadding_symbol(tb)); + buffer_append_ntimes(buf, SCOLS_GRPSET_CHUNKSIZ, cellpadding_symbol(tb)); continue; } @@ -144,7 +144,7 @@ static int groups_ascii_art_to_buffer( struct libscols_table *tb, case SCOLS_GSTATE_LAST_CHILD: buffer_append_data(buf, cellpadding_symbol(tb)); buffer_append_data(buf, grp_c_last_symbol(tb)); - if (grpset_is_empty(tb, i + 3, &rest)) { + if (grpset_is_empty(tb, i + SCOLS_GRPSET_CHUNKSIZ, &rest)) { buffer_append_ntimes(buf, rest+1, grp_horizontal_symbol(tb)); filled = 1; } diff --git a/libsmartcols/src/smartcolsP.h b/libsmartcols/src/smartcolsP.h index 5af5b096e..bbc8a98a1 100644 --- a/libsmartcols/src/smartcolsP.h +++ b/libsmartcols/src/smartcolsP.h @@ -143,6 +143,11 @@ enum { SCOLS_GSTATE_CONT_CHILDREN }; +/* + * Every group needs at least 3 columns + */ +#define SCOLS_GRPSET_CHUNKSIZ 3 + struct libscols_group { int refcount; -- cgit v1.2.3-55-g7522