Simple code that print line-by-line and wants to columnize can do this: struct string_list list; string_list_append(&list, ...); string_list_append(&list, ...); ... print_columns(&list, ...); Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@xxxxxxxxx> --- Makefile | 2 ++ column.c | 39 +++++++++++++++++++++++++++++++++++++++ column.h | 19 +++++++++++++++++++ 3 files changed, 60 insertions(+), 0 deletions(-) create mode 100644 column.c create mode 100644 column.h diff --git a/Makefile b/Makefile index a0de4e9..4e9501b 100644 --- a/Makefile +++ b/Makefile @@ -646,6 +646,7 @@ LIB_OBJS += bulk-checkin.o LIB_OBJS += bundle.o LIB_OBJS += cache-tree.o LIB_OBJS += color.o +LIB_OBJS += column.o LIB_OBJS += combine-diff.o LIB_OBJS += commit.o LIB_OBJS += compat/obstack.o @@ -2166,6 +2167,7 @@ builtin/prune.o builtin/reflog.o reachable.o: reachable.h builtin/commit.o builtin/revert.o wt-status.o: wt-status.h builtin/tar-tree.o archive-tar.o: tar.h connect.o transport.o url.o http-backend.o: url.h +column.o help.o pager.o: column.h http-fetch.o http-walker.o remote-curl.o transport.o walker.o: walker.h http.o http-walker.o http-push.o http-fetch.o remote-curl.o: http.h url.h diff --git a/column.c b/column.c new file mode 100644 index 0000000..742ae18 --- /dev/null +++ b/column.c @@ -0,0 +1,39 @@ +#include "cache.h" +#include "column.h" +#include "string-list.h" + +#define MODE(mode) ((mode) & COL_MODE) + +/* Display without layout when COL_ENABLED is not set */ +static void display_plain(const struct string_list *list, + const char *indent, const char *nl) +{ + int i; + + for (i = 0; i < list->nr; i++) + printf("%s%s%s", indent, list->items[i].string, nl); +} + +void print_columns(const struct string_list *list, unsigned int mode, + struct column_options *opts) +{ + const char *indent = "", *nl = "\n"; + int padding = 1, width = term_columns(); + + if (!list->nr) + return; + if (opts) { + if (opts->indent) + indent = opts->indent; + if (opts->nl) + nl = opts->nl; + if (opts->width) + width = opts->width; + padding = opts->padding; + } + if (width <= 1 || !(mode & COL_ENABLED)) { + display_plain(list, indent, nl); + return; + } + die("BUG: invalid mode %d", MODE(mode)); +} diff --git a/column.h b/column.h new file mode 100644 index 0000000..8e4fdaa --- /dev/null +++ b/column.h @@ -0,0 +1,19 @@ +#ifndef COLUMN_H +#define COLUMN_H + +#define COL_MODE 0x000F +#define COL_ENABLED (1 << 4) + +struct column_options { + int width; + int padding; + const char *indent; + const char *nl; +}; + +extern int term_columns(void); +extern void print_columns(const struct string_list *list, + unsigned int mode, + struct column_options *opts); + +#endif -- 1.7.8.36.g69ee2 -- To unsubscribe from this list: send the line "unsubscribe git" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html