diff options
author | Karel Zak | 2016-01-22 16:51:59 +0100 |
---|---|---|
committer | Karel Zak | 2016-01-22 16:51:59 +0100 |
commit | 57867795aa7dead56a7339a13f08ea1f3a5de615 (patch) | |
tree | a1b1f247bcb0c70cc135efd98c43c28b0b0234f5 /libsmartcols | |
parent | libsmartcols: add title to the test application (diff) | |
download | kernel-qcow2-util-linux-57867795aa7dead56a7339a13f08ea1f3a5de615.tar.gz kernel-qcow2-util-linux-57867795aa7dead56a7339a13f08ea1f3a5de615.tar.xz kernel-qcow2-util-linux-57867795aa7dead56a7339a13f08ea1f3a5de615.zip |
libsmartcols: support multibyte titles, rename wrap to padding
- let's support multibyte table titles
- use lib/mbalign.c to align the title
- rename title_wrap to title_padding (we already use "wrap" on another
places for another things)
Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'libsmartcols')
-rw-r--r-- | libsmartcols/docs/libsmartcols-sections.txt | 2 | ||||
-rw-r--r-- | libsmartcols/src/libsmartcols.h.in | 2 | ||||
-rw-r--r-- | libsmartcols/src/libsmartcols.sym | 2 | ||||
-rw-r--r-- | libsmartcols/src/smartcolsP.h | 2 | ||||
-rw-r--r-- | libsmartcols/src/symbols.c | 15 | ||||
-rw-r--r-- | libsmartcols/src/table.c | 2 | ||||
-rw-r--r-- | libsmartcols/src/table_print.c | 78 |
7 files changed, 57 insertions, 46 deletions
diff --git a/libsmartcols/docs/libsmartcols-sections.txt b/libsmartcols/docs/libsmartcols-sections.txt index 8c5d335fb..d0886513a 100644 --- a/libsmartcols/docs/libsmartcols-sections.txt +++ b/libsmartcols/docs/libsmartcols-sections.txt @@ -81,7 +81,7 @@ scols_ref_symbols scols_symbols_set_branch scols_symbols_set_right scols_symbols_set_vertical -scols_symbols_set_title_wrap +scols_symbols_set_title_padding scols_unref_symbols </SECTION> diff --git a/libsmartcols/src/libsmartcols.h.in b/libsmartcols/src/libsmartcols.h.in index 70f5df9ea..2f700d0cd 100644 --- a/libsmartcols/src/libsmartcols.h.in +++ b/libsmartcols/src/libsmartcols.h.in @@ -115,7 +115,7 @@ extern struct libscols_symbols *scols_copy_symbols(const struct libscols_symbols extern int scols_symbols_set_branch(struct libscols_symbols *sb, const char *str); extern int scols_symbols_set_vertical(struct libscols_symbols *sb, const char *str); extern int scols_symbols_set_right(struct libscols_symbols *sb, const char *str); -extern int scols_symbols_set_title_wrap(struct libscols_symbols *sb, const char *str); +extern int scols_symbols_set_title_padding(struct libscols_symbols *sb, const char *str); /* cell.c */ extern int scols_reset_cell(struct libscols_cell *ce); diff --git a/libsmartcols/src/libsmartcols.sym b/libsmartcols/src/libsmartcols.sym index 3923c9db1..6d33899ff 100644 --- a/libsmartcols/src/libsmartcols.sym +++ b/libsmartcols/src/libsmartcols.sym @@ -126,7 +126,7 @@ SMARTCOLS_2.28 { global: scols_line_refer_column_data; scols_line_set_column_data; - scols_symbols_set_title_wrap; + scols_symbols_set_title_padding; scols_table_enable_nowrap; scols_table_set_title; } SMARTCOLS_2.27; diff --git a/libsmartcols/src/smartcolsP.h b/libsmartcols/src/smartcolsP.h index 68a2a2eae..99383633b 100644 --- a/libsmartcols/src/smartcolsP.h +++ b/libsmartcols/src/smartcolsP.h @@ -52,7 +52,7 @@ struct libscols_symbols { char *branch; char *vert; char *right; - char *title_wrap; + char *title_padding; }; /* diff --git a/libsmartcols/src/symbols.c b/libsmartcols/src/symbols.c index dfe2ba7b6..cdf06ab66 100644 --- a/libsmartcols/src/symbols.c +++ b/libsmartcols/src/symbols.c @@ -62,7 +62,7 @@ void scols_unref_symbols(struct libscols_symbols *sy) free(sy->branch); free(sy->vert); free(sy->right); - free(sy->title_wrap); + free(sy->title_padding); free(sy); } } @@ -143,13 +143,16 @@ int scols_symbols_set_right(struct libscols_symbols *sb, const char *str) } /** - * scols_symbols_set_title_wrap: + * scols_symbols_set_title_padding: * @sb: a pointer to a struct libscols_symbols instance * @str: a string which will represent the symbols which wraps title output * + * The current implemetation uses only the first byte from the padding string. + * A multibyte chars are not supported yet. + * * Returns: 0, a negative value in case of an error. */ -int scols_symbols_set_title_wrap(struct libscols_symbols *sb, const char *str) +int scols_symbols_set_title_padding(struct libscols_symbols *sb, const char *str) { char *p = NULL; @@ -162,8 +165,8 @@ int scols_symbols_set_title_wrap(struct libscols_symbols *sb, const char *str) if (!p) return -ENOMEM; } - free(sb->title_wrap); - sb->title_wrap = p; + free(sb->title_padding); + sb->title_padding = p; return 0; } @@ -192,7 +195,7 @@ struct libscols_symbols *scols_copy_symbols(const struct libscols_symbols *sb) if (!rc) rc = scols_symbols_set_right(ret, sb->right); if (!rc) - rc = scols_symbols_set_title_wrap(ret, sb->title_wrap); + rc = scols_symbols_set_title_padding(ret, sb->title_padding); if (!rc) return ret; diff --git a/libsmartcols/src/table.c b/libsmartcols/src/table.c index a3f480915..6657dec7a 100644 --- a/libsmartcols/src/table.c +++ b/libsmartcols/src/table.c @@ -736,7 +736,7 @@ int scols_table_set_symbols(struct libscols_table *tb, scols_symbols_set_vertical(tb->symbols, "| "); scols_symbols_set_right(tb->symbols, "`-"); } - scols_symbols_set_title_wrap(tb->symbols, " "); + scols_symbols_set_title_padding(tb->symbols, " "); } return 0; diff --git a/libsmartcols/src/table_print.c b/libsmartcols/src/table_print.c index c2794c998..6f2ece7d3 100644 --- a/libsmartcols/src/table_print.c +++ b/libsmartcols/src/table_print.c @@ -545,58 +545,66 @@ static int print_line(struct libscols_table *tb, return 0; } -static void print_title(struct libscols_table *tb) +static int print_title(struct libscols_table *tb) { - int i = 0; - size_t len; + int rc; + size_t len = 0, width; + char *title = NULL, *buf = NULL; assert(tb); if (!tb->title) - return; - - len = strlen(tb->title); - - if (len > tb->termwidth) - len = tb->termwidth; + return 0; 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->symbols->title_wrap, tb->out); - - break; - case SCOLS_TITLE_CENTER: - for (i = 0; i < (tb->termwidth - len) / 2; i++) - fputs(tb->symbols->title_wrap, tb->out); + /* encode data */ + len = mbs_safe_encode_size(strlen(tb->title)) + 1; + if (len == 1) + return 0; + buf = malloc(len); + if (!buf) { + rc = -ENOMEM; + goto done; + } - fputs(tb->title, tb->out); - i += len; + if (!mbs_safe_encode_to_buffer(tb->title, &len, buf) || + !len || len == (size_t) -1) { + rc = -EINVAL; + goto done; + } - for (; i < tb->termwidth; i++) - fputs(tb->symbols->title_wrap, tb->out); + /* truncate and align */ + title = malloc(tb->termwidth + len); + if (!title) { + rc = -EINVAL; + goto done; + } - break; - case SCOLS_TITLE_RIGHT: - for (i = 0; i < tb->termwidth - len; i++) - fputs(tb->symbols->title_wrap, tb->out); + 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); + if (rc == (size_t) -1) { + rc = -EINVAL; + goto done; + } - fputs(tb->title, tb->out); + if (tb->title_color) + fputs(tb->title_color, tb->out); - break; - } + fputs(title, tb->out); if (tb->title_color) fputs(UL_COLOR_RESET, tb->out); - fputc('\n', tb->out); + rc = 0; +done: + free(buf); + free(title); + return rc; } static int print_header(struct libscols_table *tb, struct libscols_buffer *buf) |