From: Igor Gnatenko Date: Thu, 21 Jan 2016 09:02:31 +0000 (+0100) Subject: libsmartcols: implement title of table X-Git-Tag: v2.28-rc1~174^2~5 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b3256efff24c57f92a4f0357396fc47fa044e1f7;p=thirdparty%2Futil-linux.git libsmartcols: implement title of table Reference: https://github.com/karelzak/util-linux/issues/258 Signed-off-by: Igor Gnatenko --- diff --git a/libsmartcols/src/libsmartcols.h.in b/libsmartcols/src/libsmartcols.h.in index 0331f3713f..236475a394 100644 --- a/libsmartcols/src/libsmartcols.h.in +++ b/libsmartcols/src/libsmartcols.h.in @@ -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); diff --git a/libsmartcols/src/libsmartcols.sym b/libsmartcols/src/libsmartcols.sym index e1e4382eb3..18eb5fc269 100644 --- a/libsmartcols/src/libsmartcols.sym +++ b/libsmartcols/src/libsmartcols.sym @@ -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; diff --git a/libsmartcols/src/smartcolsP.h b/libsmartcols/src/smartcolsP.h index 9f63c3ad1e..2e6673aa03 100644 --- a/libsmartcols/src/smartcolsP.h +++ b/libsmartcols/src/smartcolsP.h @@ -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) diff --git a/libsmartcols/src/table.c b/libsmartcols/src/table.c index 7885ba2499..b8221f954b 100644 --- a/libsmartcols/src/table.c +++ b/libsmartcols/src/table.c @@ -3,6 +3,7 @@ * * Copyright (C) 2010-2014 Karel Zak * Copyright (C) 2014 Ondrej Oprala + * Copytight (C) 2016 Igor Gnatenko * * 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 diff --git a/libsmartcols/src/table_print.c b/libsmartcols/src/table_print.c index 8c08e50260..2a42c23b0f 100644 --- a/libsmartcols/src/table_print.c +++ b/libsmartcols/src/table_print.c @@ -2,6 +2,7 @@ * table.c - functions handling the data at the table level * * Copyright (C) 2010-2014 Karel Zak + * Copyright (C) 2016 Igor Gnatenko * * 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;