]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libsmartcols: wrap columns correctly with unicode
authorIgor Gnatenko <i.gnatenko.brain@gmail.com>
Fri, 22 Jan 2016 13:29:16 +0000 (14:29 +0100)
committerIgor Gnatenko <i.gnatenko.brain@gmail.com>
Fri, 22 Jan 2016 13:29:16 +0000 (14:29 +0100)
Signed-off-by: Igor Gnatenko <i.gnatenko.brain@gmail.com>
libsmartcols/src/table_print.c

index f1b0d5965f04109b99cea0c510424ce76c66c73d..b676407510d87346be0b0624091b67e51a72373f 100644 (file)
@@ -352,15 +352,25 @@ static int print_data(struct libscols_table *tb,
                        if (color)
                                fputs(UL_COLOR_RESET, tb->out);
                        len = width;
-               } else if (scols_column_is_wrap(cl)) {
+               } else if (len > width && scols_column_is_wrap(cl)) {
                        char *p = data;
+                       i = 0;
 
                        while (*p) {
-                               fprintf(tb->out, "%.*s", (int) width, p);
-                               p += width;
+                               len = width;
+                               p = strdup(p);
+                               bytes = mbs_truncate(p, &len);
+                               if (bytes == (size_t) -1) {
+                                       free(p);
+                                       break;
+                               }
+                               fputs(p, tb->out);
+                               free(p);
+                               i += bytes;
+                               p = data + i;
                                if (*p)
-                                       for (i = 0; i < cl->seqnum; i++)
-                                               print_empty_cell (tb, scols_table_get_column(tb, i),
+                                       for (size_t j = 0; j < cl->seqnum; j++)
+                                               print_empty_cell (tb, scols_table_get_column(tb, j),
                                                                  NULL, buf->bufsz);
                        }
                } else if (color) {