git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>
To: git@vger.kernel.org
Cc: "Junio C Hamano" <gitster@pobox.com>,
	"Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>
Subject: [PATCH v5 02/12] column: add API to print items in columns
Date: Sat,  4 Feb 2012 22:59:06 +0700	[thread overview]
Message-ID: <1328371156-4009-3-git-send-email-pclouds@gmail.com> (raw)
In-Reply-To: <1328371156-4009-1-git-send-email-pclouds@gmail.com>

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@gmail.com>
---
 Makefile |    3 ++-
 column.c |   39 +++++++++++++++++++++++++++++++++++++++
 column.h |   13 +++++++++++++
 3 files changed, 54 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..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
index 55d8067..8e4fdaa 100644
--- a/column.h
+++ b/column.h
@@ -1,6 +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

  parent reply	other threads:[~2012-02-04 15:55 UTC|newest]

Thread overview: 43+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-02-03 13:34 [PATCH v4 00/13] Column display again Nguyễn Thái Ngọc Duy
2012-02-03 13:34 ` [PATCH v4 01/13] Save terminal width before setting up pager Nguyễn Thái Ngọc Duy
2012-02-03 13:34 ` [PATCH v4 02/13] column: add API to print items in columns Nguyễn Thái Ngọc Duy
2012-02-03 22:55   ` Junio C Hamano
2012-02-03 23:16   ` Junio C Hamano
2012-02-03 13:34 ` [PATCH v4 03/13] parseopt: make OPT_INTEGER support hexadecimal as well Nguyễn Thái Ngọc Duy
2012-02-03 22:59   ` Junio C Hamano
2012-02-04  4:55     ` Nguyen Thai Ngoc Duy
2012-02-04  5:32       ` Junio C Hamano
2012-02-04  6:15         ` Nguyen Thai Ngoc Duy
2012-02-03 13:34 ` [PATCH v4 04/13] Add git-column and column mode parsing Nguyễn Thái Ngọc Duy
2012-02-03 13:34 ` [PATCH v4 05/13] Stop starting pager recursively Nguyễn Thái Ngọc Duy
2012-02-03 13:34 ` [PATCH v4 06/13] column: add columnar layout Nguyễn Thái Ngọc Duy
2012-02-03 13:34 ` [PATCH v4 07/13] column: support columns with different widths Nguyễn Thái Ngọc Duy
2012-02-03 13:34 ` [PATCH v4 08/13] column: add column.ui for default column output settings Nguyễn Thái Ngọc Duy
2012-02-03 23:04   ` Junio C Hamano
2012-02-03 13:34 ` [PATCH v4 09/13] help: reuse print_columns() for help -a Nguyễn Thái Ngọc Duy
2012-02-03 23:05   ` Junio C Hamano
2012-02-03 13:34 ` [PATCH v4 10/13] branch: add --column Nguyễn Thái Ngọc Duy
2012-02-03 23:11   ` Junio C Hamano
2012-02-04  5:01     ` Nguyen Thai Ngoc Duy
2012-02-03 13:34 ` [PATCH v4 11/13] status: " Nguyễn Thái Ngọc Duy
2012-02-03 23:19   ` Junio C Hamano
2012-02-03 13:34 ` [PATCH v4 12/13] column: support piping stdout to external git-column process Nguyễn Thái Ngọc Duy
2012-02-03 13:34 ` [PATCH v4 13/13] tag: add --column Nguyễn Thái Ngọc Duy
2012-02-03 23:30   ` Junio C Hamano
2012-02-04 15:59 ` [PATCH v5 00/12] Column display Nguyễn Thái Ngọc Duy
2012-02-04 15:59   ` [PATCH v5 01/12] Save terminal width before setting up pager Nguyễn Thái Ngọc Duy
2012-02-04 15:59   ` Nguyễn Thái Ngọc Duy [this message]
2012-02-04 15:59   ` [PATCH v5 03/12] Add git-column and column mode parsing Nguyễn Thái Ngọc Duy
2012-02-04 16:11     ` [PATCH v6 " Nguyễn Thái Ngọc Duy
2012-02-04 15:59   ` [PATCH v5 04/12] Stop starting pager recursively Nguyễn Thái Ngọc Duy
2012-02-04 15:59   ` [PATCH v5 05/12] column: add columnar layout Nguyễn Thái Ngọc Duy
2012-02-04 15:59   ` [PATCH v5 06/12] column: support columns with different widths Nguyễn Thái Ngọc Duy
2012-02-04 15:59   ` [PATCH v5 07/12] column: add column.ui for default column output settings Nguyễn Thái Ngọc Duy
2012-02-04 16:12     ` [PATCH v6 " Nguyễn Thái Ngọc Duy
2012-02-04 15:59   ` [PATCH v5 08/12] help: reuse print_columns() for help -a Nguyễn Thái Ngọc Duy
2012-02-04 15:59   ` [PATCH v5 09/12] branch: add --column Nguyễn Thái Ngọc Duy
2012-02-06 17:31     ` Junio C Hamano
2012-02-04 15:59   ` [PATCH v5 10/12] status: " Nguyễn Thái Ngọc Duy
2012-02-04 15:59   ` [PATCH v5 11/12] column: support piping stdout to external git-column process Nguyễn Thái Ngọc Duy
2012-02-04 15:59   ` [PATCH v5 12/12] tag: add --column Nguyễn Thái Ngọc Duy
2012-02-06 17:58   ` [PATCH v5 00/12] Column display Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1328371156-4009-3-git-send-email-pclouds@gmail.com \
    --to=pclouds@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).