]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libsmartcols: fix title output on non-tty
authorKarel Zak <kzak@redhat.com>
Wed, 31 Aug 2016 10:42:38 +0000 (12:42 +0200)
committerKarel Zak <kzak@redhat.com>
Wed, 31 Aug 2016 10:42:38 +0000 (12:42 +0200)
Signed-off-by: Karel Zak <kzak@redhat.com>
libsmartcols/src/table.c
libsmartcols/src/table_print.c

index 0c8198213e8698bb417129d392a1ba476a7631c2..00188fb69ef8d74b1523d9041d723c1cabd94c96 100644 (file)
@@ -116,6 +116,10 @@ int scols_table_set_name(struct libscols_table *tb, const char *str)
  * scols_table_get_title:
  * @tb: a pointer to a struct libscols_table instance
  *
+ * The returned pointer is possible to modify by cell functions. Note that
+ * title output alignment on non-tty is hardcoded to 80 output chars. For the
+ * regular terminal it's based on terminal width.
+ *
  * Returns: Title of the table, or %NULL in case of blank title.
  *
  * Since: 2.28
index d51089b995d012dfaf1cb0c1115134a991143078..d24700c37832c0cff0e94736a80f41754c04f3e7 100644 (file)
@@ -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;
 }