diff options
author | Karel Zak <kzak@redhat.com> | 2024-04-17 09:50:52 +0200 |
---|---|---|
committer | Karel Zak <kzak@redhat.com> | 2024-04-17 09:50:52 +0200 |
commit | 0dca768e5788b527fc8b0e0cdabcab7c499a9bd6 (patch) | |
tree | 98cbc310f48377d509fbb434e50e9f9bcbd88cc1 | |
parent | d98b2e161f8b6b59d645631297f81427d85288ac (diff) | |
parent | 515ae202051ab506c890118fd41df83555cd140f (diff) | |
download | util-linux-0dca768e5788b527fc8b0e0cdabcab7c499a9bd6.tar.gz |
Merge branch 'PR/libsmartcols-fix-wrap' of github.com:karelzak/util-linux-work
* 'PR/libsmartcols-fix-wrap' of github.com:karelzak/util-linux-work:
libsmartcols: (sample) add wrap repeating example
libsmartcols: reset wrap after calculation
-rw-r--r-- | libsmartcols/samples/Makemodule.am | 5 | ||||
-rw-r--r-- | libsmartcols/samples/wrap-repeat.c | 37 | ||||
-rw-r--r-- | libsmartcols/src/calculate.c | 7 | ||||
-rw-r--r-- | libsmartcols/src/print.c | 20 |
4 files changed, 60 insertions, 9 deletions
diff --git a/libsmartcols/samples/Makemodule.am b/libsmartcols/samples/Makemodule.am index b192ebac77..aa08c164bd 100644 --- a/libsmartcols/samples/Makemodule.am +++ b/libsmartcols/samples/Makemodule.am @@ -3,6 +3,7 @@ check_PROGRAMS += \ sample-scols-colors \ sample-scols-title \ sample-scols-wrap \ + sample-scols-wrap-repeat \ sample-scols-continuous \ sample-scols-continuous-json \ sample-scols-fromfile \ @@ -32,6 +33,10 @@ sample_scols_wrap_SOURCES = libsmartcols/samples/wrap.c sample_scols_wrap_LDADD = $(sample_scols_ldadd) sample_scols_wrap_CFLAGS = $(sample_scols_cflags) +sample_scols_wrap_repeat_SOURCES = libsmartcols/samples/wrap-repeat.c +sample_scols_wrap_repeat_LDADD = $(sample_scols_ldadd) +sample_scols_wrap_repeat_CFLAGS = $(sample_scols_cflags) + sample_scols_continuous_SOURCES = libsmartcols/samples/continuous.c sample_scols_continuous_LDADD = $(sample_scols_ldadd) libcommon.la sample_scols_continuous_CFLAGS = $(sample_scols_cflags) diff --git a/libsmartcols/samples/wrap-repeat.c b/libsmartcols/samples/wrap-repeat.c new file mode 100644 index 0000000000..ff5c200e03 --- /dev/null +++ b/libsmartcols/samples/wrap-repeat.c @@ -0,0 +1,37 @@ +#include <stdio.h> +#include <stdlib.h> + +#include "libsmartcols.h" + +int main(void) +{ + struct libscols_table *tb; + struct libscols_line *ln; + struct libscols_column *cl; + + scols_init_debug(0); + + tb = scols_new_table(); + scols_table_enable_maxout(tb, 1); + + cl = scols_table_new_column(tb, "AAA", 0, 0); + cl = scols_table_new_column(tb, "BBB", 0, 0); + cl = scols_table_new_column(tb, "CCC", 0, 0); + cl = scols_table_new_column(tb, "XXX", 0, 0); + scols_column_set_flags(cl, SCOLS_FL_WRAP); + + ln = scols_table_new_line(tb, NULL); + scols_line_set_data(ln, 0, "aaa"); + scols_line_set_data(ln, 1, "4"); + scols_line_set_data(ln, 2, "Foo bar foo."); + scols_line_set_data(ln, 3, "Adcvfr tgbnhy ujmkifas rqweerq adfs eqeqw kjkliobb u8888 kjhjkh."); + + /* repeat 2x */ + scols_table_print_range(tb, ln, ln); + printf("\n"); + scols_table_print_range(tb, ln, ln); + printf("\n"); + + scols_unref_table(tb); + return EXIT_SUCCESS; +} diff --git a/libsmartcols/src/calculate.c b/libsmartcols/src/calculate.c index 84198da3bc..deabb0794c 100644 --- a/libsmartcols/src/calculate.c +++ b/libsmartcols/src/calculate.c @@ -413,10 +413,8 @@ int __scols_calculate(struct libscols_table *tb, struct ul_buffer *buf) size_t colsepsz; int sorted = 0; - DBG(TAB, ul_debugobj(tb, "-----calculate-(termwidth=%zu)-----", tb->termwidth)); tb->is_dummy_print = 1; - colsepsz = scols_table_is_noencoding(tb) ? mbs_width(colsep(tb)) : mbs_safe_width(colsep(tb)); @@ -430,6 +428,11 @@ int __scols_calculate(struct libscols_table *tb, struct ul_buffer *buf) while (scols_table_next_column(tb, &itr, &cl) == 0) { int is_last; + memset(&cl->wstat, 0, sizeof(cl->wstat)); + cl->width = 0; + cl->width_treeart = 0; + scols_column_reset_wrap(cl); + if (scols_column_is_hidden(cl)) continue; diff --git a/libsmartcols/src/print.c b/libsmartcols/src/print.c index a3ef4270a7..89d6aba092 100644 --- a/libsmartcols/src/print.c +++ b/libsmartcols/src/print.c @@ -778,11 +778,11 @@ notree: } } +done: /* reset wrapping after greatest chunk calculation */ if (cal && scols_column_is_wrap(cl)) scols_column_reset_wrap(cl); -done: DBG(COL, ul_debugobj(cl, "__cursor_to_buffer rc=%d", rc)); return rc; } @@ -816,8 +816,12 @@ static int print_line(struct libscols_table *tb, rc = __cursor_to_buffer(tb, buf, 0); if (!rc) rc = print_data(tb, buf); - if (!rc && scols_column_has_pending_wrap(cl)) - pending = 1; + if (!rc) { + if (scols_column_has_pending_wrap(cl)) + pending = 1; + else + scols_column_reset_wrap(cl); + } scols_table_reset_cursor(tb); } fputs_color_line_close(tb); @@ -841,10 +845,12 @@ static int print_line(struct libscols_table *tb, rc = __cursor_to_buffer(tb, buf, 0); if (!rc) rc = print_pending_data(tb, buf); - if (!rc && scols_column_has_pending_wrap(cl)) - pending = 1; - if (!rc && !pending) - scols_column_reset_wrap(cl); + if (!rc) { + if (scols_column_has_pending_wrap(cl)) + pending = 1; + else + scols_column_reset_wrap(cl); + } } else print_empty_cell(tb, cl, ln, NULL, ul_buffer_get_bufsiz(buf)); scols_table_reset_cursor(tb); |