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/src/table_print.c | |
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/src/table_print.c')
-rw-r--r-- | libsmartcols/src/table_print.c | 78 |
1 files changed, 43 insertions, 35 deletions
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) |