]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libsmartcols: don't calculate with encoding on scols_table_enable_noencoding()
authorKarel Zak <kzak@redhat.com>
Wed, 22 Apr 2020 10:24:33 +0000 (12:24 +0200)
committerKarel Zak <kzak@redhat.com>
Wed, 22 Apr 2020 10:24:33 +0000 (12:24 +0200)
Old:
$ echo -e "$(tput smul)A$(tput rmul) B\nC D" | column --table
A  B
C                 D

Fixed:
$ echo -e "$(tput smul)A$(tput rmul) B\nC D" | ./column --table
A  B
C  D

Addresses: https://github.com/karelzak/util-linux/issues/1011
Signed-off-by: Karel Zak <kzak@redhat.com>
libsmartcols/src/buffer.c
libsmartcols/src/calculate.c
libsmartcols/src/column.c
libsmartcols/src/print.c

index 20953a5dc0214348aa42c9fc31cd88ab8dcbebc9..d376e8fb8144933476d33af5e762085cf4aaf77b 100644 (file)
@@ -122,8 +122,8 @@ char *buffer_get_safe_data(struct libscols_table *tb,
                        goto nothing;
        }
 
-       if (tb->no_encode) {
-               *cells = mbs_safe_width(data);
+       if (scols_table_is_noencoding(tb)) {
+               *cells = mbs_width(data);
                strcpy(buf->encdata, data);
                res = buf->encdata;
        } else {
index 360c7e0c19f6362624149b10a95ece7270f616fa..b6137fd23fe9daec403883e6a27905a5d90a9a89 100644 (file)
@@ -50,6 +50,8 @@ static int count_cell_width(struct libscols_table *tb,
                len = 0;
        else if (scols_column_is_customwrap(cl))
                len = cl->wrap_chunksize(cl, data, cl->wrapfunc_data);
+       else if (scols_table_is_noencoding(tb))
+               len = mbs_width(data);
        else
                len = mbs_safe_width(data);
 
@@ -101,13 +103,18 @@ static int count_column_width(struct libscols_table *tb,
 
        cl->width = 0;
        if (!cl->width_min) {
+               const char *data;
+
                if (cl->width_hint < 1 && scols_table_is_maxout(tb) && tb->is_term) {
                        cl->width_min = (size_t) (cl->width_hint * tb->termwidth);
                        if (cl->width_min && !is_last_column(cl))
                                cl->width_min--;
                }
-               if (scols_cell_get_data(&cl->header)) {
-                       size_t len = mbs_safe_width(scols_cell_get_data(&cl->header));
+
+               data = scols_cell_get_data(&cl->header);
+               if (data) {
+                       size_t len = scols_table_is_noencoding(tb) ?
+                                       mbs_width(data) : mbs_safe_width(data);
                        cl->width_min = max(cl->width_min, len);
                } else
                        no_header = 1;
@@ -189,7 +196,9 @@ int __scols_calculate(struct libscols_table *tb, struct libscols_buffer *buf)
        DBG(TAB, ul_debugobj(tb, "-----calculate-(termwidth=%zu)-----", tb->termwidth));
        tb->is_dummy_print = 1;
 
-       colsepsz = mbs_safe_width(colsep(tb));
+       colsepsz = scols_table_is_noencoding(tb) ?
+                       mbs_width(colsep(tb)) :
+                       mbs_safe_width(colsep(tb));
 
        if (has_groups(tb))
                group_ncolumns = 1;
index 4b42938f68b5de33160a6a86ba22eec22341318d..f95264b363591589a6f52085a56cf7f6ca65302e 100644 (file)
@@ -338,11 +338,15 @@ size_t scols_wrapnl_chunksize(const struct libscols_column *cl __attribute__((un
 
                p = strchr(data, '\n');
                if (p) {
-                       sz = mbs_safe_nwidth(data, p - data, NULL);
+                       sz = cl->table && scols_table_is_noencoding(cl->table) ?
+                                       mbs_nwidth(data, p - data) :
+                                       mbs_safe_nwidth(data, p - data, NULL);
                        p++;
-               } else
-                       sz = mbs_safe_width(data);
-
+               } else {
+                       sz = cl->table && scols_table_is_noencoding(cl->table) ?
+                                       mbs_width(data) :
+                                       mbs_safe_width(data);
+               }
                sum = max(sum, sz);
                data = p;
        }
index c92154219295fc046220a1073ac3794f22252e3c..1172533604f3e1bdd260a2740241f752e434f7c3 100644 (file)
@@ -233,7 +233,9 @@ static void print_empty_cell(struct libscols_table *tb,
                        /* only print symbols->vert if followed by child */
                        if (!list_empty(&ln->ln_branch)) {
                                fputs(vertical_symbol(tb), tb->out);
-                               len_pad = mbs_safe_width(vertical_symbol(tb));
+                               len_pad = scols_table_is_noencoding(tb) ?
+                                               mbs_width(vertical_symbol(tb)) :
+                                               mbs_safe_width(vertical_symbol(tb));
                        }
                } else {
                        /* use the same draw function as though we were intending to draw an L-shape */
@@ -393,7 +395,9 @@ static int print_pending_data(
            && (nextchunk = cl->wrap_nextchunk(cl, data, cl->wrapfunc_data))) {
                bytes = nextchunk - data;
 
-               len = mbs_safe_nwidth(data, bytes, NULL);
+               len = scols_table_is_noencoding(tb) ?
+                               mbs_nwidth(data, bytes) :
+                               mbs_safe_nwidth(data, bytes, NULL);
        } else
                bytes = mbs_truncate(data, &len);
 
@@ -511,7 +515,10 @@ static int print_data(struct libscols_table *tb,
            && (nextchunk = cl->wrap_nextchunk(cl, data, cl->wrapfunc_data))) {
                set_pending_data(cl, nextchunk, bytes - (nextchunk - data));
                bytes = nextchunk - data;
-               len = mbs_safe_nwidth(data, bytes, NULL);
+
+               len = scols_table_is_noencoding(tb) ?
+                               mbs_nwidth(data, bytes) :
+                               mbs_safe_nwidth(data, bytes, NULL);
        }
 
        if (is_last