libsmartcols: use libscols_cell for title

References: https://github.com/karelzak/util-linux/issues/270
Signed-off-by: Karel Zak <kzak@redhat.com>
This commit is contained in:
Karel Zak 2016-02-09 11:21:21 +01:00
parent f4a1d26609
commit e865838d6d
8 changed files with 78 additions and 107 deletions

View File

@ -4,10 +4,12 @@ libscols_cell
scols_cell_copy_content
scols_cell_get_color
scols_cell_get_data
scols_cell_get_flags
scols_cell_get_userdata
scols_cell_refer_data
scols_cell_set_color
scols_cell_set_data
scols_cell_set_flags
scols_cell_set_userdata
scols_cmpstr_cells
scols_reset_cell
@ -132,10 +134,7 @@ scols_table_set_line_separator
scols_table_set_name
scols_table_set_stream
scols_table_set_symbols
scols_table_set_title
scols_table_get_title
scols_table_get_title_position
scols_table_get_title_color
scols_sort_table
scols_unref_table
</SECTION>

View File

@ -56,6 +56,7 @@ int main(int argc, char *argv[])
{
struct libscols_table *tb;
struct libscols_symbols *sy;
struct libscols_cell *title;
setlocale(LC_ALL, ""); /* just to have enable UTF8 chars */
@ -70,8 +71,12 @@ int main(int argc, char *argv[])
add_line(tb, "foo", "bla bla bla");
add_line(tb, "bar", "alb alb alb");
title = scols_table_get_title(tb);
/* right */
scols_table_set_title(tb, "This is right title", SCOLS_TITLE_RIGHT, "red");
scols_cell_set_data(title, "This is right title");
scols_cell_set_color(title, "red");
scols_cell_set_flags(title, SCOLS_CELL_FL_RIGHT);
scols_print_table(tb);
/* center */
@ -81,12 +86,16 @@ int main(int argc, char *argv[])
scols_table_set_symbols(tb, sy);
scols_symbols_set_title_padding(sy, "=");
scols_table_set_title(tb, "This is center title (with padding)", SCOLS_TITLE_CENTER, "green");
scols_cell_set_data(title, "This is center title (with padding)");
scols_cell_set_color(title, "green");
scols_cell_set_flags(title, SCOLS_CELL_FL_CENTER);
scols_print_table(tb);
/* left */
scols_symbols_set_title_padding(sy, "-");
scols_table_set_title(tb, "This is left title (with padding)", SCOLS_TITLE_LEFT, "blue");
scols_cell_set_data(title, "This is left title (with padding)");
scols_cell_set_color(title, "blue");
scols_cell_set_flags(title, SCOLS_CELL_FL_LEFT);
scols_print_table(tb);
scols_unref_table(tb);

View File

@ -208,6 +208,35 @@ const char *scols_cell_get_color(const struct libscols_cell *ce)
return ce ? ce->color : NULL;
}
/**
* scols_cell_set_flags:
* @ce: a pointer to a struct libscols_cell instance
* @flags: SCOLS_CELL_FL_* flags
*
* Note that cells in the table are always aligned by column flags. The cell
* flags are used for table title only (now).
*
* Returns: 0, a negative value in case of an error.
*/
int scols_cell_set_flags(struct libscols_cell *ce, int flags)
{
if (!ce)
return -EINVAL;
ce->flags = flags;
return 0;
}
/**
* scols_cell_get_flags:
* @ce: a pointer to a struct libscols_cell instance
*
* Returns: the current flags or -1 in case of an error.
*/
int scols_cell_get_flags(const struct libscols_cell *ce)
{
return ce ? ce->flags : -1;
}
/**
* scols_cell_copy_content:
* @dest: a pointer to a struct libscols_cell instance

View File

@ -88,12 +88,12 @@ enum {
};
/*
* Position of table's title
* Cell flags, see scols_cell_set_flags() before use
*/
enum {
SCOLS_TITLE_LEFT = 0,
SCOLS_TITLE_CENTER,
SCOLS_TITLE_RIGHT
SCOLS_CELL_FL_LEFT = 0,
SCOLS_CELL_FL_CENTER,
SCOLS_CELL_FL_RIGHT
};
extern struct libscols_iter *scols_new_iter(int direction);
@ -128,6 +128,9 @@ extern const char *scols_cell_get_data(const struct libscols_cell *ce);
extern int scols_cell_set_color(struct libscols_cell *ce, const char *color);
extern const char *scols_cell_get_color(const struct libscols_cell *ce);
extern int scols_cell_set_flags(struct libscols_cell *ce, int flags);
extern int scols_cell_get_flags(const struct libscols_cell *ce);
extern void *scols_cell_get_userdata(struct libscols_cell *ce);
extern int scols_cell_set_userdata(struct libscols_cell *ce, void *data);
@ -189,10 +192,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, unsigned int position, const char *color);
extern const char *scols_table_get_title(struct libscols_table *tb);
extern unsigned int scols_table_get_title_position(struct libscols_table *tb);
extern const char *scols_table_get_title_color(struct libscols_table *tb);
extern struct libscols_cell *scols_table_get_title(struct libscols_table *tb);
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);

View File

@ -129,8 +129,7 @@ global:
scols_line_set_column_data;
scols_symbols_set_title_padding;
scols_table_enable_nowrap;
scols_table_set_title;
scols_table_get_title;
scols_table_get_title_position;
scols_table_get_title_color;
scols_cell_get_flags;
scols_cell_set_flags;
} SMARTCOLS_2.27;

View File

@ -62,6 +62,7 @@ struct libscols_cell {
char *data;
char *color;
void *userdata;
int flags;
};
@ -127,7 +128,6 @@ enum {
struct libscols_table {
int refcount;
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 */
@ -141,6 +141,7 @@ struct libscols_table {
struct list_head tb_columns;
struct list_head tb_lines;
struct libscols_symbols *symbols;
struct libscols_cell title; /* optional table title (for humans) */
int indent; /* indention counter */
int indent_last_sep;/* last printed has been line separator */
@ -153,8 +154,6 @@ 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)

View File

@ -88,6 +88,7 @@ void scols_unref_table(struct libscols_table *tb)
scols_table_remove_lines(tb);
scols_table_remove_columns(tb);
scols_unref_symbols(tb->symbols);
scols_reset_cell(&tb->title);
free(tb->linesep);
free(tb->colsep);
free(tb->name);
@ -124,53 +125,6 @@ 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.
*
* Since: 2.28
*/
int scols_table_set_title(struct libscols_table *tb, const char *title, unsigned int position, const char *color)
{
char *p = NULL;
char *q = NULL;
if (!tb)
return -EINVAL;
if (title) {
p = strdup(title);
if (!p)
return -ENOMEM;
}
if (color) {
if (isalpha(*color)) {
color = color_sequence_from_colorname(color);
if (!color)
return -EINVAL;
}
q = strdup(color);
if (!q)
return -ENOMEM;
}
free(tb->title);
free(tb->title_color);
tb->title = p;
tb->title_color = q;
tb->title_pos = position;
return 0;
}
/**
* scols_table_get_title:
* @tb: a pointer to a struct libscols_table instance
@ -179,35 +133,9 @@ int scols_table_set_title(struct libscols_table *tb, const char *title, unsigned
*
* Since: 2.28
*/
const char *scols_table_get_title(struct libscols_table *tb)
struct libscols_cell *scols_table_get_title(struct libscols_table *tb)
{
return tb->title;
}
/**
* scols_table_get_title_position:
* @tb: a pointer to a struct libscols_table instance
*
* Returns: Title's position of the table.
*
* Since: 2.28
*/
unsigned int scols_table_get_title_position(struct libscols_table *tb)
{
return tb->title_pos;
}
/**
* scols_table_get_title_color:
* @tb: a pointer to a struct libscols_table instance
*
* Returns: Title's color of the table, or %NULL in case of not set color.
*
* Since: 2.28
*/
const char *scols_table_get_title_color(struct libscols_table *tb)
{
return tb->title_color;
return &tb->title;
}
/**

View File

@ -575,19 +575,19 @@ static int print_line(struct libscols_table *tb,
static int print_title(struct libscols_table *tb)
{
int rc;
int rc, align;
size_t len = 0, width;
char *title = NULL, *buf = NULL;
assert(tb);
if (!tb->title)
if (!tb->title.data)
return 0;
DBG(TAB, ul_debugobj(tb, "printing title"));
/* encode data */
len = mbs_safe_encode_size(strlen(tb->title)) + 1;
len = mbs_safe_encode_size(strlen(tb->title.data)) + 1;
if (len == 1)
return 0;
buf = malloc(len);
@ -596,7 +596,7 @@ static int print_title(struct libscols_table *tb)
goto done;
}
if (!mbs_safe_encode_to_buffer(tb->title, &len, buf) ||
if (!mbs_safe_encode_to_buffer(tb->title.data, &len, buf) ||
!len || len == (size_t) -1) {
rc = -EINVAL;
goto done;
@ -609,23 +609,31 @@ static int print_title(struct libscols_table *tb)
goto done;
}
if (tb->title.flags & SCOLS_CELL_FL_LEFT)
align = MBS_ALIGN_LEFT;
else if (tb->title.flags & SCOLS_CELL_FL_RIGHT)
align = MBS_ALIGN_RIGHT;
else if (tb->title.flags & SCOLS_CELL_FL_CENTER)
align = MBS_ALIGN_CENTER;
else
align = MBS_ALIGN_LEFT; /* default */
width = tb->termwidth;
rc = mbsalign_with_padding(
buf, title, tb->termwidth + len, &width,
tb->title_pos == SCOLS_TITLE_LEFT ? MBS_ALIGN_LEFT :
tb->title_pos == SCOLS_TITLE_RIGHT ? MBS_ALIGN_RIGHT :
MBS_ALIGN_CENTER, 0, (int) *tb->symbols->title_padding);
rc = mbsalign_with_padding(buf, title, tb->termwidth + len,
&width, align,
0, (int) *tb->symbols->title_padding);
if (rc == (size_t) -1) {
rc = -EINVAL;
goto done;
}
if (tb->title_color)
fputs(tb->title_color, tb->out);
if (tb->title.color)
fputs(tb->title.color, tb->out);
fputs(title, tb->out);
if (tb->title_color)
if (tb->title.color)
fputs(UL_COLOR_RESET, tb->out);
fputc('\n', tb->out);
rc = 0;