[PATCH v4 02/13] column: add API to print items in columns

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Simple code that print line-by-line and wants to columnize can do
this:

print_cell(...);
print_cell(...);
...
print_columns(...);

if column layout is disabled, print_cell() prints directly and
print_columns() becomes no-op. Otherwise print_cell() saves all items
in a string list for print_columns() to process later.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@xxxxxxxxx>
---
 Makefile |    3 ++-
 column.c |   54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 column.h |   17 +++++++++++++++++
 3 files changed, 73 insertions(+), 1 deletions(-)
 create mode 100644 column.c

diff --git a/Makefile b/Makefile
index cbbc699..5f0531b 100644
--- a/Makefile
+++ b/Makefile
@@ -637,6 +637,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
@@ -2114,7 +2115,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
-help.o pager.o: column.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..4ae98f3
--- /dev/null
+++ b/column.c
@@ -0,0 +1,54 @@
+#include "cache.h"
+#include "column.h"
+#include "string-list.h"
+
+#define MODE(mode) ((mode) & COL_MODE)
+
+struct string_list_item *add_cell_to_list(struct string_list *list,
+					  int mode,
+					  const char *string)
+{
+	if (mode & COL_ENABLED)
+		return string_list_append(list, string);
+	return NULL;
+}
+
+void print_cell(struct string_list *list, int mode, const char *string)
+{
+	if (!add_cell_to_list(list, mode, string))
+		printf("%s\n", string);
+}
+
+/* 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, 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
index 55d8067..e0f8c26 100644
--- a/column.h
+++ b/column.h
@@ -1,6 +1,23 @@
 #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 struct string_list_item *add_cell_to_list(struct string_list *list,
+						 int mode,
+						 const char *string);
+/* add cell to list, or print it out if column mode is disabled */
+extern void print_cell(struct string_list *list, int mode, const char *string);
+extern void print_columns(const struct string_list *list,
+			  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


[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]