diff options
author | Karel Zak | 2017-07-31 11:45:23 +0200 |
---|---|---|
committer | Karel Zak | 2017-07-31 11:45:23 +0200 |
commit | 28a3cc83c56eca679302d8d51a5056b836e3c86e (patch) | |
tree | c3dd7e88405c2fd080c9ddd4bd078bd4ff39e81a /libsmartcols/src/table_print.c | |
parent | agetty: fix --login-pause logic (diff) | |
download | kernel-qcow2-util-linux-28a3cc83c56eca679302d8d51a5056b836e3c86e.tar.gz kernel-qcow2-util-linux-28a3cc83c56eca679302d8d51a5056b836e3c86e.tar.xz kernel-qcow2-util-linux-28a3cc83c56eca679302d8d51a5056b836e3c86e.zip |
libsmartcols: add scols_table_enable_noencoding()
Now the library encode all output. It seems too strict and difficult
to use the library for some use-cases -- for example if you want to
use the library for already colorized output (by esc.sequences).
Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'libsmartcols/src/table_print.c')
-rw-r--r-- | libsmartcols/src/table_print.c | 54 |
1 files changed, 36 insertions, 18 deletions
diff --git a/libsmartcols/src/table_print.c b/libsmartcols/src/table_print.c index 115212101..472c3616c 100644 --- a/libsmartcols/src/table_print.c +++ b/libsmartcols/src/table_print.c @@ -132,7 +132,9 @@ static char *buffer_get_data(struct libscols_buffer *buf) } /* encode data by mbs_safe_encode() to avoid control and non-printable chars */ -static char *buffer_get_safe_data(struct libscols_buffer *buf, size_t *cells, +static char *buffer_get_safe_data(struct libscols_table *tb, + struct libscols_buffer *buf, + size_t *cells, const char *safechars) { char *data = buffer_get_data(buf); @@ -147,7 +149,14 @@ static char *buffer_get_safe_data(struct libscols_buffer *buf, size_t *cells, goto nothing; } - res = mbs_safe_encode_to_buffer(data, cells, buf->encdata, safechars); + if (tb->no_encode) { + *cells = mbs_safe_width(data); + strcpy(buf->encdata, data); + res = buf->encdata; + } else { + res = mbs_safe_encode_to_buffer(data, cells, buf->encdata, safechars); + } + if (!res || !*cells || *cells == (size_t) -1) goto nothing; return res; @@ -251,7 +260,7 @@ static void print_empty_cell(struct libscols_table *tb, line_ascii_art_to_buffer(tb, ln, art); if (!list_empty(&ln->ln_branch) && has_pending_data(tb)) buffer_append_data(art, vertical_symbol(tb)); - data = buffer_get_safe_data(art, &len_pad, NULL); + data = buffer_get_safe_data(tb, art, &len_pad, NULL); if (data && len_pad) fputs(data, tb->out); free_buffer(art); @@ -475,7 +484,7 @@ static int print_data(struct libscols_table *tb, /* Encode. Note that 'len' and 'width' are number of cells, not bytes. */ - data = buffer_get_safe_data(buf, &len, scols_column_get_safechars(cl)); + data = buffer_get_safe_data(tb, buf, &len, scols_column_get_safechars(cl)); if (!data) data = ""; bytes = strlen(data); @@ -763,21 +772,30 @@ static int print_title(struct libscols_table *tb) DBG(TAB, ul_debugobj(tb, "printing title")); /* encode data */ - 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(bufsz); - if (!buf) { - rc = -ENOMEM; - goto done; - } + if (tb->no_encode) { + bufsz = strlen(tb->title.data) + 1; + buf = strdup(tb->title.data); + if (!buf) { + rc = -ENOMEM; + goto done; + } + } else { + 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(bufsz); + if (!buf) { + rc = -ENOMEM; + goto done; + } - if (!mbs_safe_encode_to_buffer(tb->title.data, &bufsz, buf, NULL) || - !bufsz || bufsz == (size_t) -1) { - rc = -EINVAL; - goto done; + if (!mbs_safe_encode_to_buffer(tb->title.data, &bufsz, buf, NULL) || + !bufsz || bufsz == (size_t) -1) { + rc = -EINVAL; + goto done; + } } /* truncate and align */ |