]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libsmartcols: implement title of table
authorIgor Gnatenko <i.gnatenko.brain@gmail.com>
Thu, 21 Jan 2016 09:02:31 +0000 (10:02 +0100)
committerIgor Gnatenko <i.gnatenko.brain@gmail.com>
Thu, 21 Jan 2016 15:41:12 +0000 (16:41 +0100)
Reference: https://github.com/karelzak/util-linux/issues/258
Signed-off-by: Igor Gnatenko <i.gnatenko.brain@gmail.com>
libsmartcols/src/libsmartcols.h.in
libsmartcols/src/libsmartcols.sym
libsmartcols/src/smartcolsP.h
libsmartcols/src/table.c
libsmartcols/src/table_print.c

index 0331f3713ff6ad05fe87a4a40fc3bb619b9b10da..236475a394382ddafd32384b3a51d8b65d00e9da 100644 (file)
@@ -86,6 +86,15 @@ enum {
        SCOLS_FL_HIDDEN      = (1 << 5),   /* maintain data, but don't print */
 };
 
+/*
+ * Position of table's title
+ */
+enum {
+       SCOLS_TITLE_LEFT = 0,
+       SCOLS_TITLE_CENTER,
+       SCOLS_TITLE_RIGHT
+};
+
 extern struct libscols_iter *scols_new_iter(int direction);
 extern void scols_free_iter(struct libscols_iter *itr);
 extern void scols_reset_iter(struct libscols_iter *itr, int direction);
@@ -177,6 +186,7 @@ extern struct libscols_line *scols_copy_line(struct libscols_line *ln);
 /* table */
 extern int scols_table_colors_wanted(struct libscols_table *tb);
 extern int scols_table_set_name(struct libscols_table *tb, const char *name);
+extern int scols_table_set_title(struct libscols_table *tb, const char *title, int position, const char *color);
 extern int scols_table_is_raw(struct libscols_table *tb);
 extern int scols_table_is_ascii(struct libscols_table *tb);
 extern int scols_table_is_json(struct libscols_table *tb);
index e1e4382eb3fb34b440e3cfe3b835e3b2c131a440..18eb5fc2695d29a960c7a50b404882fbfe4d9103 100644 (file)
@@ -127,4 +127,5 @@ global:
        scols_line_refer_column_data;
        scols_line_set_column_data;
        scols_table_enable_nowrap;
+       scols_table_set_title;
 } SMARTCOLS_2.27;
index 9f63c3ad1eafdbd96c096d6ba6a3433db854483a..2e6673aa035d10aa0b21719c03a876aa159f2cf0 100644 (file)
@@ -125,7 +125,8 @@ enum {
  */
 struct libscols_table {
        int     refcount;
-       char    *name;          /* optional table table */
+       char    *name;          /* optional table name (for JSON) */
+       char    *title;         /* optional table title (for humans) */
        size_t  ncols;          /* number of columns */
        size_t  ntreecols;      /* number of columns with SCOLS_FL_TREE */
        size_t  nlines;         /* number of lines */
@@ -151,6 +152,8 @@ struct libscols_table {
                        maxout          :1,     /* maximalize output */
                        no_headings     :1,     /* don't print header */
                        no_wrap         :1;     /* never wrap lines */
+       unsigned int    title_pos;              /* title position */
+       char            *title_color;           /* title color */
 };
 
 #define IS_ITER_FORWARD(_i)    ((_i)->direction == SCOLS_ITER_FORWARD)
index 7885ba249921c864830c33a3e29f9b80313c3ce3..b8221f954bcc54ae897b60d3bceee97f14c3b61a 100644 (file)
@@ -3,6 +3,7 @@
  *
  * Copyright (C) 2010-2014 Karel Zak <kzak@redhat.com>
  * Copyright (C) 2014 Ondrej Oprala <ooprala@redhat.com>
+ * Copytight (C) 2016 Igor Gnatenko <i.gnatenko.brain@gmail.com>
  *
  * This file may be redistributed under the terms of the
  * GNU Lesser General Public License.
@@ -121,6 +122,52 @@ int scols_table_set_name(struct libscols_table *tb, const char *name)
        return 0;
 }
 
+/**
+ * scols_table_set_title:
+ * @tb: a pointer to a struct libscols_table instance
+ * @title: a title
+ * @position: a position
+ * @color: color name or ESC sequence
+ *
+ * The table title is used to print header of table.
+ *
+ * Returns: 0, a negative number in case of an error.
+ */
+int scols_table_set_title(struct libscols_table *tb, const char *title, int position, const char *color)
+{
+       char *p = NULL;
+
+       if (!tb)
+               return -EINVAL;
+
+       if (title) {
+               p = strdup(title);
+               if (!p)
+                       return -ENOMEM;
+       }
+       free(tb->title);
+       tb->title = p;
+
+       tb->title_pos = position;
+
+       p = NULL;
+       if (color) {
+               if (isalpha(*color)) {
+                       color = color_sequence_from_colorname(color);
+
+                       if (!color)
+                               return -EINVAL;
+               }
+               p = strdup(color);
+               if (!p)
+                       return -ENOMEM;
+       }
+       free(tb->title_color);
+       tb->title_color = p;
+
+       return 0;
+}
+
 /**
  * scols_table_add_column:
  * @tb: a pointer to a struct libscols_table instance
index 8c08e502604ee6fe433d2b7d7fb9ce2d97e60af9..2a42c23b0f474583d8db478dc60f4f8dab8e740f 100644 (file)
@@ -2,6 +2,7 @@
  * table.c - functions handling the data at the table level
  *
  * Copyright (C) 2010-2014 Karel Zak <kzak@redhat.com>
+ * Copyright (C) 2016 Igor Gnatenko <i.gnatenko.brain@gmail.com>
  *
  * This file may be redistributed under the terms of the
  * GNU Lesser General Public License.
@@ -544,6 +545,55 @@ static int print_line(struct libscols_table *tb,
        return 0;
 }
 
+static void print_title(struct libscols_table *tb)
+{
+       int i = 0;
+       size_t len;
+
+       assert(tb);
+
+       if (!tb->title)
+               return;
+
+       len = strlen(tb->title);
+
+       DBG(TAB, ul_debugobj(tb, "printing title"));
+
+       if (tb->title_color)
+               fputs(tb->title_color, tb->out);
+
+       switch (tb->title_pos) {
+       case SCOLS_TITLE_LEFT:
+               fputs(tb->title, tb->out);
+
+               for (i = len; i < tb->termwidth; i++)
+                       fputs(" ", tb->out);
+
+               break;
+       case SCOLS_TITLE_CENTER:
+               for (i = 0; i <= (tb->termwidth - len) / 2; i++)
+                       fputs(" ", tb->out);
+
+               fputs(tb->title, tb->out);
+               i += len;
+
+               for (; i < tb->termwidth; i++)
+                       fputs(" ", tb->out);
+
+               break;
+       case SCOLS_TITLE_RIGHT:
+               for (i = 0; i < tb->termwidth - len; i++)
+                       fputs(" ", tb->out);
+
+               fputs(tb->title, tb->out);
+
+               break;
+       }
+
+       if (tb->title_color)
+               fputs(UL_COLOR_RESET, tb->out);
+}
+
 static int print_header(struct libscols_table *tb, struct libscols_buffer *buf)
 {
        int rc = 0;
@@ -1074,6 +1124,8 @@ int scols_print_table(struct libscols_table *tb)
 
        fput_table_open(tb);
 
+       print_title(tb);
+
        rc = print_header(tb, buf);
        if (rc)
                goto done;