libsmartcols: implement title of table
Reference: https://github.com/karelzak/util-linux/issues/258 Signed-off-by: Igor Gnatenko <i.gnatenko.brain@gmail.com>
This commit is contained in:
parent
acde3a05a9
commit
b3256efff2
|
@ -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);
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Reference in New Issue