diff options
-rw-r--r-- | include/mbsalign.h | 4 | ||||
-rw-r--r-- | lib/mbsalign.c | 20 | ||||
-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 |
9 files changed, 75 insertions, 52 deletions
diff --git a/include/mbsalign.h b/include/mbsalign.h index 5eaf606e5..74d25da11 100644 --- a/include/mbsalign.h +++ b/include/mbsalign.h @@ -46,6 +46,10 @@ extern size_t mbsalign (const char *src, char *dest, size_t dest_size, size_t *width, mbs_align_t align, int flags); +extern size_t mbsalign_with_padding (const char *src, char *dest, size_t dest_size, + size_t *width, mbs_align_t align, int flags, + int padchar); + extern size_t mbs_safe_nwidth(const char *buf, size_t bufsz, size_t *sz); extern size_t mbs_safe_width(const char *s); diff --git a/lib/mbsalign.c b/lib/mbsalign.c index 2816cebbf..a9cf84521 100644 --- a/lib/mbsalign.c +++ b/lib/mbsalign.c @@ -315,16 +315,23 @@ done: A pointer to the terminating NUL is returned. */ static char* -mbs_align_pad (char *dest, const char* dest_end, size_t n_spaces) +mbs_align_pad (char *dest, const char* dest_end, size_t n_spaces, int padchar) { /* FIXME: Should we pad with "figure space" (\u2007) if non ascii data present? */ for (/* nothing */; n_spaces && (dest < dest_end); n_spaces--) - *dest++ = ' '; + *dest++ = padchar; *dest = '\0'; return dest; } +size_t +mbsalign (const char *src, char *dest, size_t dest_size, + size_t *width, mbs_align_t align, int flags) +{ + return mbsalign_with_padding(src, dest, dest_size, width, align, flags, ' '); +} + /* Align a string, SRC, in a field of *WIDTH columns, handling multi-byte characters; write the result into the DEST_SIZE-byte buffer, DEST. ALIGNMENT specifies whether to left- or right-justify or to center. @@ -339,8 +346,9 @@ mbs_align_pad (char *dest, const char* dest_end, size_t n_spaces) Update *WIDTH to indicate how many columns were used before padding. */ size_t -mbsalign (const char *src, char *dest, size_t dest_size, - size_t *width, mbs_align_t align, int flags) +mbsalign_with_padding (const char *src, char *dest, size_t dest_size, + size_t *width, mbs_align_t align, int flags, + int padchar) { size_t ret = -1; size_t src_size = strlen (src) + 1; @@ -452,10 +460,10 @@ mbsalign_unibyte: abort(); } - dest = mbs_align_pad (dest, dest_end, start_spaces); + dest = mbs_align_pad (dest, dest_end, start_spaces, padchar); space_left = dest_end - dest; dest = mempcpy (dest, str_to_print, min (n_used_bytes, space_left)); - mbs_align_pad (dest, dest_end, end_spaces); + mbs_align_pad (dest, dest_end, end_spaces, padchar); } #ifdef HAVE_WIDECHAR mbsalign_cleanup: 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) |