]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libsmartcols: support multibyte titles, rename wrap to padding
authorKarel Zak <kzak@redhat.com>
Fri, 22 Jan 2016 15:51:59 +0000 (16:51 +0100)
committerKarel Zak <kzak@redhat.com>
Fri, 22 Jan 2016 15:51:59 +0000 (16:51 +0100)
- 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>
include/mbsalign.h
lib/mbsalign.c
libsmartcols/docs/libsmartcols-sections.txt
libsmartcols/src/libsmartcols.h.in
libsmartcols/src/libsmartcols.sym
libsmartcols/src/smartcolsP.h
libsmartcols/src/symbols.c
libsmartcols/src/table.c
libsmartcols/src/table_print.c

index 5eaf606e57182290d0c3106203b18e8eb56edf0e..74d25da11c872315474ec01b17ccf68961ef26e7 100644 (file)
@@ -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);
 
index 2816cebbf3199377cd889ff7f5ac4d96ca552c6e..a9cf845215280c42ade569d70e3f1d213312d670 100644 (file)
@@ -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:
index 8c5d335fba036fdc9d108eae3da7a883a339ae2e..d0886513a9b8fab64156a1de9d0ca9299e06243b 100644 (file)
@@ -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>
 
index 70f5df9eaa15840c0d93ad5b282eb93f5cef705a..2f700d0cde600d3d45db3d13304b31c315d0aa7a 100644 (file)
@@ -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);
index 3923c9db1d82725ea94641b9c469d49ca3324519..6d33899ff5f2b27cf200e56c06459c78ea8da7ad 100644 (file)
@@ -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;
index 68a2a2eae557f8a0f3acdfcc10505d4fb8dfb296..99383633b019a8c86b082f6622ea97fe6eeabc13 100644 (file)
@@ -52,7 +52,7 @@ struct libscols_symbols {
        char    *branch;
        char    *vert;
        char    *right;
-       char    *title_wrap;
+       char    *title_padding;
 };
 
 /*
index dfe2ba7b6c1d8b6c37584c1107958e550e90425f..cdf06ab6611d770fe9f8d6c89a9d9840be527b87 100644 (file)
@@ -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;
 
index a3f4809157e519c76960ce82eaff3539dd328a0f..6657dec7a2e058caf5104ce119ab8ee6ea070649 100644 (file)
@@ -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;
index c2794c998e229b11000d3c00a9bb77d396693d3a..6f2ece7d34e7e28da23e0dd3400e541f7d80dc27 100644 (file)
@@ -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)