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);
/* 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);
scols_line_refer_column_data;
scols_line_set_column_data;
scols_table_enable_nowrap;
+ scols_table_set_title;
} SMARTCOLS_2.27;
*/
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 */
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)
*
* 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.
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
* 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.
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;
fput_table_open(tb);
+ print_title(tb);
+
rc = print_header(tb, buf);
if (rc)
goto done;