]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libsmartcols: (groups) remove hardcoded const numbers
authorKarel Zak <kzak@redhat.com>
Thu, 2 May 2019 11:44:12 +0000 (13:44 +0200)
committerKarel Zak <kzak@redhat.com>
Thu, 2 May 2019 11:44:12 +0000 (13:44 +0200)
Signed-off-by: Karel Zak <kzak@redhat.com>
libsmartcols/src/grouping.c
libsmartcols/src/print.c
libsmartcols/src/smartcolsP.h

index add4b748afa6d9d033e9a2e985f3b597ca708244..d01a7333076bce85d97ab984691d19a3319f89bb 100644 (file)
@@ -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) {
index a017b636b3905256c0aa0e85c85e9fc645dcbe8a..ec2224f3681fd44d20935469e5bd3e53f128d2b5 100644 (file)
@@ -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;
                        }
index 5af5b096e20dc6ca04af2439ea6740f0a058a0ee..bbc8a98a149d32288bbad724b50a99d29cae180a 100644 (file)
@@ -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;