summaryrefslogtreecommitdiffstats
path: root/libsmartcols/src/table_print.c
diff options
context:
space:
mode:
authorKarel Zak2016-08-31 12:42:38 +0200
committerKarel Zak2016-08-31 12:42:38 +0200
commita593128fd0c3d6faefe05a4162e9379c024c1321 (patch)
treed8a808f11a2151b86e3dcecf2a112f6e0e375654 /libsmartcols/src/table_print.c
parentlibsmartcols: fix scols_table_enable_colors() usage in samples (diff)
downloadkernel-qcow2-util-linux-a593128fd0c3d6faefe05a4162e9379c024c1321.tar.gz
kernel-qcow2-util-linux-a593128fd0c3d6faefe05a4162e9379c024c1321.tar.xz
kernel-qcow2-util-linux-a593128fd0c3d6faefe05a4162e9379c024c1321.zip
libsmartcols: fix title output on non-tty
Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'libsmartcols/src/table_print.c')
-rw-r--r--libsmartcols/src/table_print.c24
1 files changed, 15 insertions, 9 deletions
diff --git a/libsmartcols/src/table_print.c b/libsmartcols/src/table_print.c
index d51089b99..d24700c37 100644
--- a/libsmartcols/src/table_print.c
+++ b/libsmartcols/src/table_print.c
@@ -702,7 +702,7 @@ static int print_title(struct libscols_table *tb)
{
int rc, color = 0;
mbs_align_t align;
- size_t len = 0, width;
+ size_t width, bufsz, titlesz;
char *title = NULL, *buf = NULL;
assert(tb);
@@ -713,23 +713,28 @@ static int print_title(struct libscols_table *tb)
DBG(TAB, ul_debugobj(tb, "printing title"));
/* encode data */
- len = mbs_safe_encode_size(strlen(tb->title.data)) + 1;
- if (len == 1)
+ bufsz = mbs_safe_encode_size(strlen(tb->title.data)) + 1;
+ if (bufsz == 1) {
+ DBG(TAB, ul_debugobj(tb, "title is empty string -- ignore"));
return 0;
- buf = malloc(len);
+ }
+ buf = malloc(bufsz);
if (!buf) {
rc = -ENOMEM;
goto done;
}
- if (!mbs_safe_encode_to_buffer(tb->title.data, &len, buf) ||
- !len || len == (size_t) -1) {
+ if (!mbs_safe_encode_to_buffer(tb->title.data, &bufsz, buf) ||
+ !bufsz || bufsz == (size_t) -1) {
rc = -EINVAL;
goto done;
}
/* truncate and align */
- title = malloc(tb->termwidth + len);
+ width = tb->is_term ? tb->termwidth : 80;
+ titlesz = width + bufsz;
+
+ title = malloc(titlesz);
if (!title) {
rc = -EINVAL;
goto done;
@@ -744,8 +749,8 @@ static int print_title(struct libscols_table *tb)
else
align = MBS_ALIGN_LEFT; /* default */
- width = tb->termwidth;
- rc = mbsalign_with_padding(buf, title, tb->termwidth + len,
+ /* copy from buf to title and align to width with title_padding */
+ rc = mbsalign_with_padding(buf, title, titlesz,
&width, align,
0, (int) *tb->symbols->title_padding);
@@ -769,6 +774,7 @@ static int print_title(struct libscols_table *tb)
done:
free(buf);
free(title);
+ DBG(TAB, ul_debugobj(tb, "printing title done [rc=%d]", rc));
return rc;
}