]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libsmartcols: rewrite ./sample-scols-wrap
authorKarel Zak <kzak@redhat.com>
Wed, 10 Feb 2016 14:37:30 +0000 (15:37 +0100)
committerKarel Zak <kzak@redhat.com>
Wed, 10 Feb 2016 14:37:30 +0000 (15:37 +0100)
Signed-off-by: Karel Zak <kzak@redhat.com>
libsmartcols/samples/wrap.c

index d24763ef0e0130869cd64e97a67f79e1b7642925..2a476fbe9e3f93e3f79445ef125ec37f20884d3e 100644 (file)
 #include "libsmartcols.h"
 
 
-enum { COL_NAME, COL_DATA };
+enum { COL_NAME, COL_DESC, COL_FOO, COL_LIKE, COL_TEXT };
 
 /* add columns to the @tb */
 static void setup_columns(struct libscols_table *tb)
 {
        if (!scols_table_new_column(tb, "NAME", 0, SCOLS_FL_TREE))
                goto fail;
-       if (!scols_table_new_column(tb, "DATA", 0, SCOLS_FL_WRAP))
+       if (!scols_table_new_column(tb, "DESC", 0, 0))
+               goto fail;
+       if (!scols_table_new_column(tb, "FOO", 0, SCOLS_FL_WRAP))
+               goto fail;
+       if (!scols_table_new_column(tb, "LIKE", 0, SCOLS_FL_RIGHT))
+               goto fail;
+       if (!scols_table_new_column(tb, "TEXT", 0, SCOLS_FL_WRAP))
                goto fail;
        return;
 fail:
@@ -36,18 +42,36 @@ fail:
        err(EXIT_FAILURE, "faild to create output columns");
 }
 
+static char *gen_text(const char *prefix, const char *sub_prefix, char *buf, size_t sz)
+{
+       int x = snprintf(buf, sz,  "%s-%s-", prefix, sub_prefix);
+
+       for ( ; x < sz - 1; x++)
+               buf[x] = *prefix;
+
+       buf[x++] = 'x';
+       buf[x] = '\0';
+       return buf;
+}
+
 static struct libscols_line * add_line(        struct libscols_table *tb,
                                        struct libscols_line *parent,
-                                       const char *name,
-                                       const char *data)
+                                       const char *prefix)
 {
+       char buf[BUFSIZ];
        struct libscols_line *ln = scols_table_new_line(tb, parent);
        if (!ln)
                err(EXIT_FAILURE, "failed to create output line");
 
-       if (scols_line_set_data(ln, COL_NAME, name))
+       if (scols_line_set_data(ln, COL_NAME, gen_text(prefix, "N", buf, 15)))
+               goto fail;
+       if (scols_line_set_data(ln, COL_DESC, gen_text(prefix, "D", buf, 10)))
+               goto fail;
+       if (scols_line_set_data(ln, COL_FOO, gen_text(prefix, "U", buf, 55)))
+               goto fail;
+       if (scols_line_set_data(ln, COL_LIKE, "1"))
                goto fail;
-       if (scols_line_set_data(ln, COL_DATA, data))
+       if (scols_line_set_data(ln, COL_TEXT, gen_text(prefix, "T", buf, 50)))
                goto fail;
        return ln;
 fail:
@@ -58,7 +82,7 @@ fail:
 int main(int argc, char *argv[])
 {
        struct libscols_table *tb;
-       struct libscols_line *ln;
+       struct libscols_line *ln, *xln;
 
        setlocale(LC_ALL, "");  /* just to have enable UTF8 chars */
 
@@ -71,13 +95,15 @@ int main(int argc, char *argv[])
        scols_table_enable_colors(tb, 1);
        setup_columns(tb);
 
-       ln = add_line(tb, NULL, "monohull", "type of boat having only one hull, unlike multihulled boats which can have two or more individual hulls connected to one another.");
-       add_line(tb, ln, "keelboat", "riverine cargo-capable working boat, or a small to mid-sized recreational sailing yacht.");
-       add_line(tb, ln, "dinghy", "type of small boat, often carried or towed for use as a ship's boat by a larger vessel.");
+       ln = add_line(tb, NULL, "A");
+       add_line(tb, ln, "aa");
+       add_line(tb, ln, "ab");
 
-       ln = add_line(tb, NULL, "multihull", "ship, vessel, craft or boat with more than one hull.");
-       add_line(tb, ln, "catamaran", "geometry-stabilized craft; that is, it derives its stability from its wide beam, rather than from a ballasted keel, like a monohull.");
-       add_line(tb, ln, "trimaran ", "multihull boat that comprises a main hull and two smaller outrigger hulls (or \"floats\") which are attached to the main hull with lateral beams.");
+       ln = add_line(tb, NULL, "B");
+       xln = add_line(tb, ln, "ba");
+       add_line(tb, xln, "baa");
+       add_line(tb, xln, "bab");
+       add_line(tb, ln, "bb");
 
        scols_print_table(tb);
        scols_unref_table(tb);