All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v6 00/11] Column display
@ 2012-02-25 11:41 Nguyễn Thái Ngọc Duy
  2012-02-25 11:41 ` [PATCH v6 01/11] column: add API to print items in columns Nguyễn Thái Ngọc Duy
                   ` (12 more replies)
  0 siblings, 13 replies; 66+ messages in thread
From: Nguyễn Thái Ngọc Duy @ 2012-02-25 11:41 UTC (permalink / raw)
  To: git; +Cc: Junio C Hamano, Nguyễn Thái Ngọc Duy

Compared to v5 [1] this only has minor changes:

 - drop term_columns() patch because master already has that
 - style fixes
 - incorporate Ramsay's fix in option parsing code
 - make git_column_config() return either 0 or -1

[1] http://thread.gmane.org/gmane.comp.version-control.git/189758/focus=189875

Nguyễn Thái Ngọc Duy (11):
  column: add API to print items in columns
  Add git-column and column mode parsing
  Stop starting pager recursively
  column: add columnar layout
  column: support columns with different widths
  column: add column.ui for default column output settings
  help: reuse print_columns() for help -a
  branch: add --column
  status: add --column
  column: support piping stdout to external git-column process
  tag: add --column

 .gitignore                   |    1 +
 Documentation/config.txt     |   38 ++++
 Documentation/git-branch.txt |    9 +
 Documentation/git-column.txt |   53 +++++
 Documentation/git-status.txt |    7 +
 Documentation/git-tag.txt    |    9 +
 Makefile                     |    3 +
 builtin.h                    |    1 +
 builtin/branch.c             |   32 +++-
 builtin/column.c             |   62 ++++++
 builtin/commit.c             |    6 +
 builtin/tag.c                |   26 ++-
 column.c                     |  478 ++++++++++++++++++++++++++++++++++++++++++
 column.h                     |   40 ++++
 command-list.txt             |    1 +
 git.c                        |    1 +
 help.c                       |   48 ++---
 pager.c                      |    2 +-
 parse-options.h              |    2 +
 t/t3200-branch.sh            |   77 +++++++
 t/t7004-tag.sh               |   44 ++++
 t/t7508-status.sh            |   24 ++
 t/t9002-column.sh            |  161 ++++++++++++++
 wt-status.c                  |   28 +++-
 wt-status.h                  |    1 +
 25 files changed, 1110 insertions(+), 44 deletions(-)
 create mode 100644 Documentation/git-column.txt
 create mode 100644 builtin/column.c
 create mode 100644 column.c
 create mode 100644 column.h
 create mode 100755 t/t9002-column.sh

-- 
1.7.8.36.g69ee2

^ permalink raw reply	[flat|nested] 66+ messages in thread

* [PATCH v6 01/11] column: add API to print items in columns
  2012-02-25 11:41 [PATCH v6 00/11] Column display Nguyễn Thái Ngọc Duy
@ 2012-02-25 11:41 ` Nguyễn Thái Ngọc Duy
  2012-02-25 11:41 ` [PATCH v6 02/11] Add git-column and column mode parsing Nguyễn Thái Ngọc Duy
                   ` (11 subsequent siblings)
  12 siblings, 0 replies; 66+ messages in thread
From: Nguyễn Thái Ngọc Duy @ 2012-02-25 11:41 UTC (permalink / raw)
  To: git; +Cc: Junio C Hamano, Nguyễn Thái Ngọc Duy

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 |    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

^ permalink raw reply related	[flat|nested] 66+ messages in thread

* [PATCH v6 02/11] Add git-column and column mode parsing
  2012-02-25 11:41 [PATCH v6 00/11] Column display Nguyễn Thái Ngọc Duy
  2012-02-25 11:41 ` [PATCH v6 01/11] column: add API to print items in columns Nguyễn Thái Ngọc Duy
@ 2012-02-25 11:41 ` Nguyễn Thái Ngọc Duy
  2012-02-27 20:09   ` Ramsay Jones
  2012-02-25 11:41 ` [PATCH v6 03/11] Stop starting pager recursively Nguyễn Thái Ngọc Duy
                   ` (10 subsequent siblings)
  12 siblings, 1 reply; 66+ messages in thread
From: Nguyễn Thái Ngọc Duy @ 2012-02-25 11:41 UTC (permalink / raw)
  To: git; +Cc: Junio C Hamano, Nguyễn Thái Ngọc Duy

A column option string consists of many token separated by either
space of commas. A token belongs to one of three groups:

 - enabling: always, never and auto
 - layout mode: to be implemented
 - other tuning, which could be negated be prefix 'no'

A command line option without argument (e.g. --column) will enable
column output and reuse existing settings (layout mode and options..).
--no-column disables columnar output.

Thanks-to: Ramsay Jones <ramsay@ramsay1.demon.co.uk>
Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 .gitignore                   |    1 +
 Documentation/git-column.txt |   49 +++++++++++++++
 Makefile                     |    1 +
 builtin.h                    |    1 +
 builtin/column.c             |   41 +++++++++++++
 column.c                     |  133 ++++++++++++++++++++++++++++++++++++++++++
 column.h                     |   11 ++++
 command-list.txt             |    1 +
 git.c                        |    1 +
 parse-options.h              |    2 +
 t/t9002-column.sh            |   27 +++++++++
 11 files changed, 268 insertions(+), 0 deletions(-)
 create mode 100644 Documentation/git-column.txt
 create mode 100644 builtin/column.c
 create mode 100755 t/t9002-column.sh

diff --git a/.gitignore b/.gitignore
index 87fcc5f..2540264 100644
--- a/.gitignore
+++ b/.gitignore
@@ -26,6 +26,7 @@
 /git-cherry-pick
 /git-clean
 /git-clone
+/git-column
 /git-commit
 /git-commit-tree
 /git-config
diff --git a/Documentation/git-column.txt b/Documentation/git-column.txt
new file mode 100644
index 0000000..508b85f
--- /dev/null
+++ b/Documentation/git-column.txt
@@ -0,0 +1,49 @@
+git-column(1)
+=============
+
+NAME
+----
+git-column - Display data in columns
+
+SYNOPSIS
+--------
+[verse]
+'git column' [--mode=<mode> | --rawmode=<n>] [--width=<width>]
+	     [--indent=<string>] [--nl=<string>] [--pading=<n>]
+
+DESCRIPTION
+-----------
+This command formats its input into multiple columns.
+
+OPTIONS
+-------
+--mode=<mode>::
+	Specify layout mode. See configuration variable column.ui for option
+	syntax.
+
+--rawmode=<n>::
+	Same as --mode but take mode encoded as a number. This is mainly used
+	by other commands that have already parsed layout mode.
+
+--width=<width>::
+	Specify the terminal width. By default 'git column' will detect the
+	terminal width, or fall back to 80 if it is unable to do so.
+
+--indent=<string>::
+	String to be printed at the beginning of each line.
+
+--nl=<N>::
+	String to be printed at the end of each line,
+	including newline character.
+
+--padding=<N>::
+	The number of spaces between columns. One space by default.
+
+
+Author
+------
+Written by Nguyen Thai Ngoc Duy <pclouds@gmail.com>
+
+GIT
+---
+Part of the linkgit:git[1] suite
diff --git a/Makefile b/Makefile
index 4e9501b..0998f0d 100644
--- a/Makefile
+++ b/Makefile
@@ -775,6 +775,7 @@ BUILTIN_OBJS += builtin/checkout-index.o
 BUILTIN_OBJS += builtin/checkout.o
 BUILTIN_OBJS += builtin/clean.o
 BUILTIN_OBJS += builtin/clone.o
+BUILTIN_OBJS += builtin/column.o
 BUILTIN_OBJS += builtin/commit-tree.o
 BUILTIN_OBJS += builtin/commit.o
 BUILTIN_OBJS += builtin/config.o
diff --git a/builtin.h b/builtin.h
index 857b9c8..338f540 100644
--- a/builtin.h
+++ b/builtin.h
@@ -61,6 +61,7 @@ extern int cmd_cherry(int argc, const char **argv, const char *prefix);
 extern int cmd_cherry_pick(int argc, const char **argv, const char *prefix);
 extern int cmd_clone(int argc, const char **argv, const char *prefix);
 extern int cmd_clean(int argc, const char **argv, const char *prefix);
+extern int cmd_column(int argc, const char **argv, const char *prefix);
 extern int cmd_commit(int argc, const char **argv, const char *prefix);
 extern int cmd_commit_tree(int argc, const char **argv, const char *prefix);
 extern int cmd_config(int argc, const char **argv, const char *prefix);
diff --git a/builtin/column.c b/builtin/column.c
new file mode 100644
index 0000000..3b0f74e
--- /dev/null
+++ b/builtin/column.c
@@ -0,0 +1,41 @@
+#include "builtin.h"
+#include "cache.h"
+#include "strbuf.h"
+#include "parse-options.h"
+#include "string-list.h"
+#include "column.h"
+
+static const char * const builtin_column_usage[] = {
+	"git column [options]",
+	NULL
+};
+static unsigned int colopts;
+
+int cmd_column(int argc, const char **argv, const char *prefix)
+{
+	struct string_list list = STRING_LIST_INIT_DUP;
+	struct strbuf sb = STRBUF_INIT;
+	struct column_options copts;
+	struct option options[] = {
+		OPT_COLUMN(0, "mode", &colopts, "layout to use"),
+		OPT_INTEGER(0, "rawmode", &colopts, "layout to use"),
+		OPT_INTEGER(0, "width", &copts.width, "Maximum width"),
+		OPT_STRING(0, "indent", &copts.indent, "string", "Padding space on left border"),
+		OPT_INTEGER(0, "nl", &copts.nl, "Padding space on right border"),
+		OPT_INTEGER(0, "padding", &copts.padding, "Padding space between columns"),
+		OPT_END()
+	};
+
+	memset(&copts, 0, sizeof(copts));
+	copts.width = term_columns();
+	copts.padding = 1;
+	argc = parse_options(argc, argv, "", options, builtin_column_usage, 0);
+	if (argc)
+		usage_with_options(builtin_column_usage, options);
+
+	while (!strbuf_getline(&sb, stdin, '\n'))
+		string_list_append(&list, sb.buf);
+
+	print_columns(&list, colopts, &copts);
+	return 0;
+}
diff --git a/column.c b/column.c
index 742ae18..e62edf7 100644
--- a/column.c
+++ b/column.c
@@ -1,6 +1,7 @@
 #include "cache.h"
 #include "column.h"
 #include "string-list.h"
+#include "parse-options.h"
 
 #define MODE(mode) ((mode) & COL_MODE)
 
@@ -37,3 +38,135 @@ void print_columns(const struct string_list *list, unsigned int mode,
 	}
 	die("BUG: invalid mode %d", MODE(mode));
 }
+
+struct colopt {
+	enum {
+		ENABLE,
+		MODE,
+		OPTION
+	} type;
+	const char *name;
+	int value;
+};
+
+/*
+ * Set COL_ENABLED and COL_ENABLED_SET. If 'set' is -1, check if
+ * stdout is tty.
+ */
+static int set_enable_bit(unsigned int *mode, int set, int stdout_is_tty)
+{
+	if (set < 0) {	/* auto */
+		if (stdout_is_tty < 0)
+			stdout_is_tty = isatty(1);
+		set = stdout_is_tty || (pager_in_use() && pager_use_color);
+	}
+	if (set)
+		*mode = *mode | COL_ENABLED | COL_ENABLED_SET;
+	else
+		*mode = (*mode & ~COL_ENABLED) | COL_ENABLED_SET;
+	return 0;
+}
+
+/*
+ * Set COL_MODE_*. mode is intially copied from column.ui. If
+ * COL_ENABLED_SET is not set, then neither 'always', 'never' nor
+ * 'auto' has been used. Default to 'always'.
+ */
+static int set_mode(unsigned int *mode, unsigned int value)
+{
+	*mode = (*mode & ~COL_MODE) | value;
+	if (!(*mode & COL_ENABLED_SET))
+		*mode |= COL_ENABLED | COL_ENABLED_SET;
+
+	return 0;
+}
+
+/* Set or unset other COL_* */
+static int set_option(unsigned int *mode, unsigned int opt, int set)
+{
+	if (set)
+		*mode |= opt;
+	else
+		*mode &= ~opt;
+	return 0;
+}
+
+static int parse_option(const char *arg, int len,
+			unsigned int *mode, int stdout_is_tty)
+{
+	struct colopt opts[] = {
+		{ ENABLE, "always",  1 },
+		{ ENABLE, "never",   0 },
+		{ ENABLE, "auto",   -1 },
+	};
+	int i;
+
+	for (i = 0; i < ARRAY_SIZE(opts); i++) {
+		int set = 1, arg_len = len, name_len;
+		const char *arg_str = arg;
+
+		if (opts[i].type == OPTION) {
+			if (arg_len > 2 && !strncmp(arg_str, "no", 2)) {
+				arg_str += 2;
+				arg_len -= 2;
+				set = 0;
+			} else {
+				set = 1;
+			}
+		}
+
+		name_len = strlen(opts[i].name);
+		if (arg_len != name_len ||
+		    strncmp(arg_str, opts[i].name, name_len))
+			continue;
+
+		switch (opts[i].type) {
+		case ENABLE:
+			return set_enable_bit(mode, opts[i].value,
+					      stdout_is_tty);
+		case MODE:
+			return set_mode(mode, opts[i].value);
+		case OPTION:
+			return set_option(mode, opts[i].value, set);
+		default:
+			die("BUG: Unknown option type %d", opts[i].type);
+		}
+	}
+
+	return error("unsupported style '%s'", arg);
+}
+
+int git_config_column(unsigned int *mode, const char *value,
+		      int stdout_is_tty)
+{
+	const char *sep = " ,";
+
+	while (*value) {
+		int len = strcspn(value, sep);
+		if (len) {
+			if (parse_option(value, len, mode, stdout_is_tty))
+				return -1;
+
+			value += len;
+		}
+		value += strspn(value, sep);
+	}
+	return 0;
+}
+
+int parseopt_column_callback(const struct option *opt,
+			     const char *arg, int unset)
+{
+	unsigned int *mode = opt->value;
+	*mode |= COL_PARSEOPT;
+	if (unset) {
+		*mode = (*mode & ~COL_ENABLED) | COL_ENABLED_SET;
+		return 0;
+	}
+	if (arg)
+		return git_config_column(mode, arg, -1);
+
+	/* no arg, turn it on */
+	*mode |= COL_ENABLED | COL_ENABLED_SET;
+	return 0;
+}
diff --git a/column.h b/column.h
index 8e4fdaa..67b1c4f 100644
--- a/column.h
+++ b/column.h
@@ -3,6 +3,11 @@
 
 #define COL_MODE          0x000F
 #define COL_ENABLED      (1 << 4)
+#define COL_ENABLED_SET  (1 << 5)  /* Has COL_ENABLED been set by config? */
+#define COL_PARSEOPT     (1 << 8)  /* --column is given */
+
+#define explicitly_enable_column(c) \
+	(((c) & (COL_PARSEOPT | COL_ENABLED)) == (COL_PARSEOPT | COL_ENABLED))
 
 struct column_options {
 	int width;
@@ -15,5 +20,11 @@ extern int term_columns(void);
 extern void print_columns(const struct string_list *list,
 			  unsigned int mode,
 			  struct column_options *opts);
+extern int git_config_column(unsigned int *mode, const char *value,
+			     int stdout_is_tty);
+
+struct option;
+extern int parseopt_column_callback(const struct option *opt,
+				    const char *arg, int unset);
 
 #endif
diff --git a/command-list.txt b/command-list.txt
index a36ee9b..fe06f15 100644
--- a/command-list.txt
+++ b/command-list.txt
@@ -20,6 +20,7 @@ git-cherry-pick                         mainporcelain
 git-citool                              mainporcelain
 git-clean                               mainporcelain
 git-clone                               mainporcelain common
+git-column                              purehelpers
 git-commit                              mainporcelain common
 git-commit-tree                         plumbingmanipulators
 git-config                              ancillarymanipulators
diff --git a/git.c b/git.c
index 3805616..419e3cc 100644
--- a/git.c
+++ b/git.c
@@ -348,6 +348,7 @@ static void handle_internal_command(int argc, const char **argv)
 		{ "cherry-pick", cmd_cherry_pick, RUN_SETUP | NEED_WORK_TREE },
 		{ "clean", cmd_clean, RUN_SETUP | NEED_WORK_TREE },
 		{ "clone", cmd_clone },
+		{ "column", cmd_column },
 		{ "commit", cmd_commit, RUN_SETUP | NEED_WORK_TREE },
 		{ "commit-tree", cmd_commit_tree, RUN_SETUP },
 		{ "config", cmd_config, RUN_SETUP_GENTLY },
diff --git a/parse-options.h b/parse-options.h
index 2e811dc..56fcafd 100644
--- a/parse-options.h
+++ b/parse-options.h
@@ -238,5 +238,7 @@ extern int parse_opt_noop_cb(const struct option *, const char *, int);
 	  PARSE_OPT_OPTARG, &parse_opt_abbrev_cb, 0 }
 #define OPT__COLOR(var, h) \
 	OPT_COLOR_FLAG(0, "color", (var), (h))
+#define OPT_COLUMN(s, l, v, h) \
+	{ OPTION_CALLBACK, (s), (l), (v), "style", (h), PARSE_OPT_OPTARG, parseopt_column_callback }
 
 #endif
diff --git a/t/t9002-column.sh b/t/t9002-column.sh
new file mode 100755
index 0000000..b0b6d62
--- /dev/null
+++ b/t/t9002-column.sh
@@ -0,0 +1,27 @@
+#!/bin/sh
+
+test_description='git column'
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+	cat >lista <<\EOF
+one
+two
+three
+four
+five
+six
+seven
+eight
+nine
+ten
+eleven
+EOF
+'
+
+test_expect_success 'never' '
+	git column --mode=never <lista >actual &&
+	test_cmp lista actual
+'
+
+test_done
-- 
1.7.8.36.g69ee2

^ permalink raw reply related	[flat|nested] 66+ messages in thread

* [PATCH v6 03/11] Stop starting pager recursively
  2012-02-25 11:41 [PATCH v6 00/11] Column display Nguyễn Thái Ngọc Duy
  2012-02-25 11:41 ` [PATCH v6 01/11] column: add API to print items in columns Nguyễn Thái Ngọc Duy
  2012-02-25 11:41 ` [PATCH v6 02/11] Add git-column and column mode parsing Nguyễn Thái Ngọc Duy
@ 2012-02-25 11:41 ` Nguyễn Thái Ngọc Duy
  2012-02-25 11:41 ` [PATCH v6 04/11] column: add columnar layout Nguyễn Thái Ngọc Duy
                   ` (9 subsequent siblings)
  12 siblings, 0 replies; 66+ messages in thread
From: Nguyễn Thái Ngọc Duy @ 2012-02-25 11:41 UTC (permalink / raw)
  To: git; +Cc: Junio C Hamano, Nguyễn Thái Ngọc Duy

git-column can be used as a pager for other git commands, something
like this:

    GIT_PAGER="git -p column --mode='dense color'" git -p branch

The problem with this is that "git -p column" also has $GIT_PAGER
set so the pager runs itself again as a pager, then again and again.

Stop this.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 pager.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)

diff --git a/pager.c b/pager.c
index 05584de..4dcb08d 100644
--- a/pager.c
+++ b/pager.c
@@ -73,7 +73,7 @@ void setup_pager(void)
 {
 	const char *pager = git_pager(isatty(1));
 
-	if (!pager)
+	if (!pager || pager_in_use())
 		return;
 
 	/*
-- 
1.7.8.36.g69ee2

^ permalink raw reply related	[flat|nested] 66+ messages in thread

* [PATCH v6 04/11] column: add columnar layout
  2012-02-25 11:41 [PATCH v6 00/11] Column display Nguyễn Thái Ngọc Duy
                   ` (2 preceding siblings ...)
  2012-02-25 11:41 ` [PATCH v6 03/11] Stop starting pager recursively Nguyễn Thái Ngọc Duy
@ 2012-02-25 11:41 ` Nguyễn Thái Ngọc Duy
  2012-02-25 11:41 ` [PATCH v6 05/11] column: support columns with different widths Nguyễn Thái Ngọc Duy
                   ` (8 subsequent siblings)
  12 siblings, 0 replies; 66+ messages in thread
From: Nguyễn Thái Ngọc Duy @ 2012-02-25 11:41 UTC (permalink / raw)
  To: git; +Cc: Junio C Hamano, Nguyễn Thái Ngọc Duy

COL_MODE_COLUMN and COL_MODE_ROW fill column by column (or row by row
respectively), given the terminal width and how many space between
columns.

Strings are supposed to be in UTF-8. If strings contain ANSI escape
strings, COL_ANSI must be specified for correct length calculation.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 column.c          |  131 ++++++++++++++++++++++++++++++++++++++++++++++++++++-
 column.h          |    3 +
 t/t9002-column.sh |   86 +++++++++++++++++++++++++++++++++++
 3 files changed, 219 insertions(+), 1 deletions(-)

diff --git a/column.c b/column.c
index e62edf7..6dca3b8 100644
--- a/column.c
+++ b/column.c
@@ -2,8 +2,66 @@
 #include "column.h"
 #include "string-list.h"
 #include "parse-options.h"
+#include "utf8.h"
 
 #define MODE(mode) ((mode) & COL_MODE)
+#define XY2LINEAR(d, x, y) (MODE((d)->mode) == COL_MODE_COLUMN ? \
+			    (x) * (d)->rows + (y) : \
+			    (y) * (d)->cols + (x))
+
+struct column_data {
+	const struct string_list *list; /* list of all cells */
+	int mode;			/* COL_MODE */
+	int total_width;		/* terminal width */
+	int padding;			/* cell padding */
+	const char *indent;		/* left most column indentation */
+	const char *nl;
+
+	int rows, cols;
+	int *len;			/* cell length */
+};
+
+/* return length of 's' in letters, ANSI escapes stripped */
+static int item_length(int mode, const char *s)
+{
+	int len, i = 0;
+	struct strbuf str = STRBUF_INIT;
+
+	if (!(mode & COL_ANSI))
+		return utf8_strwidth(s);
+
+	strbuf_addstr(&str, s);
+	while ((s = strstr(str.buf + i, "\033[")) != NULL) {
+		int len = strspn(s + 2, "0123456789;");
+		i = s - str.buf;
+		strbuf_remove(&str, i, len + 3); /* \033[<len><func char> */
+	}
+	len = utf8_strwidth(str.buf);
+	strbuf_release(&str);
+	return len;
+}
+
+/*
+ * Calculate cell width, rows and cols for a table of equal cells, given
+ * table width and how many spaces between cells.
+ */
+static void layout(struct column_data *data, int *width)
+{
+	int i;
+
+	*width = 0;
+	for (i = 0; i < data->list->nr; i++)
+		if (*width < data->len[i])
+			*width = data->len[i];
+
+	*width += data->padding;
+
+	data->cols = (data->total_width - strlen(data->indent)) / *width;
+	if (data->cols == 0)
+		data->cols = 1;
+
+	data->rows = DIV_ROUND_UP(data->list->nr, data->cols);
+}
 
 /* Display without layout when COL_ENABLED is not set */
 static void display_plain(const struct string_list *list,
@@ -15,6 +73,65 @@ static void display_plain(const struct string_list *list,
 		printf("%s%s%s", indent, list->items[i].string, nl);
 }
 
+/* Print a cell to stdout with all necessary leading/traling space */
+static int display_cell(struct column_data *data, int initial_width,
+			const char *empty_cell, int x, int y)
+{
+	int i, len, newline;
+
+	i = XY2LINEAR(data, x, y);
+	if (i >= data->list->nr)
+		return -1;
+	len = data->len[i];
+	if (MODE(data->mode) == COL_MODE_COLUMN)
+		newline = i + data->rows >= data->list->nr;
+	else
+		newline = x == data->cols - 1 || i == data->list->nr - 1;
+
+	printf("%s%s%s",
+			x == 0 ? data->indent : "",
+			data->list->items[i].string,
+			newline ? data->nl : empty_cell + len);
+	return 0;
+}
+
+/* Display COL_MODE_COLUMN or COL_MODE_ROW */
+static void display_table(const struct string_list *list,
+			  int mode, int total_width,
+			  int padding, const char *indent,
+			  const char *nl)
+{
+	struct column_data data;
+	int x, y, i, initial_width;
+	char *empty_cell;
+
+	memset(&data, 0, sizeof(data));
+	data.list = list;
+	data.mode = mode;
+	data.total_width = total_width;
+	data.padding = padding;
+	data.indent = indent;
+	data.nl = nl;
+
+	data.len = xmalloc(sizeof(*data.len) * list->nr);
+	for (i = 0; i < list->nr; i++)
+		data.len[i] = item_length(mode, list->items[i].string);
+
+	layout(&data, &initial_width);
+
+	empty_cell = xmalloc(initial_width + 1);
+	memset(empty_cell, ' ', initial_width);
+	empty_cell[initial_width] = '\0';
+	for (y = 0; y < data.rows; y++) {
+		for (x = 0; x < data.cols; x++)
+			if (display_cell(&data, initial_width, empty_cell, x, y))
+				break;
+	}
+
+	free(data.len);
+	free(empty_cell);
+}
+
 void print_columns(const struct string_list *list, unsigned int mode,
 		   struct column_options *opts)
 {
@@ -36,7 +153,16 @@ void print_columns(const struct string_list *list, unsigned int mode,
 		display_plain(list, indent, nl);
 		return;
 	}
-	die("BUG: invalid mode %d", MODE(mode));
+
+	switch (MODE(mode)) {
+	case COL_MODE_ROW:
+	case COL_MODE_COLUMN:
+		display_table(list, mode, width, padding, indent, nl);
+		break;
+
+	default:
+		die("BUG: invalid mode %d", MODE(mode));
+	}
 }
 
 struct colopt {
@@ -98,6 +224,9 @@ static int parse_option(const char *arg, int len,
 		{ ENABLE, "always",  1 },
 		{ ENABLE, "never",   0 },
 		{ ENABLE, "auto",   -1 },
+		{ MODE,   "column", COL_MODE_COLUMN },
+		{ MODE,   "row",    COL_MODE_ROW },
+		{ OPTION, "color",  COL_ANSI },
 	};
 	int i;
 
diff --git a/column.h b/column.h
index 67b1c4f..d9d27c6 100644
--- a/column.h
+++ b/column.h
@@ -2,8 +2,11 @@
 #define COLUMN_H
 
 #define COL_MODE          0x000F
+#define COL_MODE_COLUMN        0   /* Fill columns before rows */
+#define COL_MODE_ROW           1   /* Fill rows before columns */
 #define COL_ENABLED      (1 << 4)
 #define COL_ENABLED_SET  (1 << 5)  /* Has COL_ENABLED been set by config? */
+#define COL_ANSI         (1 << 6)  /* Remove ANSI escapes from string length */
 #define COL_PARSEOPT     (1 << 8)  /* --column is given */
 
 #define explicitly_enable_column(c) \
diff --git a/t/t9002-column.sh b/t/t9002-column.sh
index b0b6d62..cffb029 100755
--- a/t/t9002-column.sh
+++ b/t/t9002-column.sh
@@ -24,4 +24,90 @@ test_expect_success 'never' '
 	test_cmp lista actual
 '
 
+test_expect_success '80 columns' '
+	cat >expected <<\EOF &&
+one    two    three  four   five   six    seven  eight  nine   ten    eleven
+EOF
+	COLUMNS=80 git column --mode=column <lista >actual &&
+	test_cmp expected actual
+'
+
+test_expect_success 'COLUMNS = 1' '
+	cat >expected <<\EOF &&
+one
+two
+three
+four
+five
+six
+seven
+eight
+nine
+ten
+eleven
+EOF
+	COLUMNS=1 git column --mode=column <lista >actual &&
+	test_cmp expected actual
+'
+
+test_expect_success 'width = 1' '
+	git column --mode=column --width=1 <lista >actual &&
+	test_cmp expected actual
+'
+
+COLUMNS=20
+export COLUMNS
+
+test_expect_success '20 columns' '
+	cat >expected <<\EOF &&
+one    seven
+two    eight
+three  nine
+four   ten
+five   eleven
+six
+EOF
+	git column --mode=column <lista >actual &&
+	test_cmp expected actual
+'
+
+test_expect_success '20 columns, padding 2' '
+	cat >expected <<\EOF &&
+one     seven
+two     eight
+three   nine
+four    ten
+five    eleven
+six
+EOF
+	git column --mode=column --padding 2 <lista >actual &&
+	test_cmp expected actual
+'
+
+test_expect_success '20 columns, indented' '
+	cat >expected <<\EOF &&
+  one    seven
+  two    eight
+  three  nine
+  four   ten
+  five   eleven
+  six
+EOF
+	git column --mode=column --indent="  " <lista >actual &&
+	test_cmp expected actual
+'
+
+test_expect_success '20 columns, row first' '
+	cat >expected <<\EOF &&
+one    two
+three  four
+five   six
+seven  eight
+nine   ten
+eleven
+EOF
+	git column --mode=row <lista >actual &&
+	test_cmp expected actual
+'
+
 test_done
-- 
1.7.8.36.g69ee2

^ permalink raw reply related	[flat|nested] 66+ messages in thread

* [PATCH v6 05/11] column: support columns with different widths
  2012-02-25 11:41 [PATCH v6 00/11] Column display Nguyễn Thái Ngọc Duy
                   ` (3 preceding siblings ...)
  2012-02-25 11:41 ` [PATCH v6 04/11] column: add columnar layout Nguyễn Thái Ngọc Duy
@ 2012-02-25 11:41 ` Nguyễn Thái Ngọc Duy
  2012-02-26 23:12   ` Junio C Hamano
  2012-02-25 11:41 ` [PATCH v6 06/11] column: add column.ui for default column output settings Nguyễn Thái Ngọc Duy
                   ` (7 subsequent siblings)
  12 siblings, 1 reply; 66+ messages in thread
From: Nguyễn Thái Ngọc Duy @ 2012-02-25 11:41 UTC (permalink / raw)
  To: git; +Cc: Junio C Hamano, Nguyễn Thái Ngọc Duy

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 column.c          |   80 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 column.h          |    2 +
 t/t9002-column.sh |   48 +++++++++++++++++++++++++++++++
 3 files changed, 130 insertions(+), 0 deletions(-)

diff --git a/column.c b/column.c
index 6dca3b8..3c77997 100644
--- a/column.c
+++ b/column.c
@@ -19,6 +19,7 @@ struct column_data {
 
 	int rows, cols;
 	int *len;			/* cell length */
+	int *width;			/* index to the longest row in column */
 };
 
 /* return length of 's' in letters, ANSI escapes stripped */
@@ -63,6 +64,69 @@ static void layout(struct column_data *data, int *width)
 	data->rows = DIV_ROUND_UP(data->list->nr, data->cols);
 }
 
+static void compute_column_width(struct column_data *data)
+{
+	int i, x, y;
+	for (x = 0; x < data->cols; x++) {
+		data->width[x] = XY2LINEAR(data, x, 0);
+		for (y = 0; y < data->rows; y++) {
+			i = XY2LINEAR(data, x, y);
+			if (i >= data->list->nr)
+				continue;
+			if (data->len[data->width[x]] < data->len[i])
+				data->width[x] = i;
+		}
+	}
+}
+
+/*
+ * Shrink all columns by shortening them one row each time (and adding
+ * more columns along the way). Hopefully the longest cell will be
+ * moved to the next column, column is shrunk so we have more space
+ * for new columns. The process ends when the whole thing no longer
+ * fits in data->total_width.
+ */
+static void shrink_columns(struct column_data *data)
+{
+	int x, y, total_width, cols, rows;
+
+	data->width = xrealloc(data->width,
+			       sizeof(*data->width) * data->cols);
+	for (x = 0; x < data->cols; x++) {
+		data->width[x] = 0;
+		for (y = 0; y < data->rows; y++) {
+			int len1 = data->len[data->width[x]];
+			int len2 = data->len[XY2LINEAR(data, x, y)];
+			if (len1 < len2)
+				data->width[x] = y;
+		}
+	}
+
+	while (data->rows > 1) {
+		rows = data->rows;
+		cols = data->cols;
+
+		data->rows--;
+		data->cols = DIV_ROUND_UP(data->list->nr, data->rows);
+		if (data->cols != cols)
+			data->width = xrealloc(data->width, sizeof(*data->width) * data->cols);
+
+		compute_column_width(data);
+
+		total_width = strlen(data->indent);
+		for (x = 0; x < data->cols; x++) {
+			total_width += data->len[data->width[x]];
+			total_width += data->padding;
+		}
+		if (total_width > data->total_width) {
+			data->rows = rows;
+			data->cols = cols;
+			compute_column_width(data);
+			break;
+		}
+	}
+}
+
 /* Display without layout when COL_ENABLED is not set */
 static void display_plain(const struct string_list *list,
 			  const char *indent, const char *nl)
@@ -82,7 +146,18 @@ static int display_cell(struct column_data *data, int initial_width,
 	i = XY2LINEAR(data, x, y);
 	if (i >= data->list->nr)
 		return -1;
+
 	len = data->len[i];
+	if (data->width && data->len[data->width[x]] < initial_width) {
+		/*
+		 * empty_cell has initial_width chars, if real column
+		 * is narrower, increase len a bit so we fill less
+		 * space.
+		 */
+		len += initial_width - data->len[data->width[x]];
+		len -= data->padding;
+	}
+
 	if (MODE(data->mode) == COL_MODE_COLUMN)
 		newline = i + data->rows >= data->list->nr;
 	else
@@ -119,6 +194,9 @@ static void display_table(const struct string_list *list,
 
 	layout(&data, &initial_width);
 
+	if (mode & COL_DENSE)
+		shrink_columns(&data);
+
 	empty_cell = xmalloc(initial_width + 1);
 	memset(empty_cell, ' ', initial_width);
 	empty_cell[initial_width] = '\0';
@@ -129,6 +207,7 @@ static void display_table(const struct string_list *list,
 	}
 
 	free(data.len);
+	free(data.width);
 	free(empty_cell);
 }
 
@@ -227,6 +306,7 @@ static int parse_option(const char *arg, int len,
 		{ MODE,   "column", COL_MODE_COLUMN },
 		{ MODE,   "row",    COL_MODE_ROW },
 		{ OPTION, "color",  COL_ANSI },
+		{ OPTION, "dense",  COL_DENSE },
 	};
 	int i;
 
diff --git a/column.h b/column.h
index d9d27c6..eb03c6c 100644
--- a/column.h
+++ b/column.h
@@ -7,6 +7,8 @@
 #define COL_ENABLED      (1 << 4)
 #define COL_ENABLED_SET  (1 << 5)  /* Has COL_ENABLED been set by config? */
 #define COL_ANSI         (1 << 6)  /* Remove ANSI escapes from string length */
+#define COL_DENSE        (1 << 7)  /* Shrink columns when possible,
+				      making space for more columns */
 #define COL_PARSEOPT     (1 << 8)  /* --column is given */
 
 #define explicitly_enable_column(c) \
diff --git a/t/t9002-column.sh b/t/t9002-column.sh
index cffb029..fe7a30e 100755
--- a/t/t9002-column.sh
+++ b/t/t9002-column.sh
@@ -71,6 +71,30 @@ EOF
 	test_cmp expected actual
 '
 
+test_expect_success '20 columns, nodense' '
+	cat >expected <<\EOF &&
+one    seven
+two    eight
+three  nine
+four   ten
+five   eleven
+six
+EOF
+	git column --mode=column,nodense < lista > actual &&
+	test_cmp expected actual
+'
+
+test_expect_success '20 columns, dense' '
+	cat >expected <<\EOF &&
+one   five  nine
+two   six   ten
+three seven eleven
+four  eight
+EOF
+	git column --mode=column,dense < lista > actual &&
+	test_cmp expected actual
+'
+
 test_expect_success '20 columns, padding 2' '
 	cat >expected <<\EOF &&
 one     seven
@@ -110,4 +134,28 @@ EOF
 	test_cmp expected actual
 '
 
+test_expect_success '20 columns, row first, nodense' '
+	cat >expected <<\EOF &&
+one    two
+three  four
+five   six
+seven  eight
+nine   ten
+eleven
+EOF
+	git column --mode=row,nodense <lista >actual &&
+	test_cmp expected actual
+'
+
+test_expect_success '20 columns, row first, dense' '
+	cat >expected <<\EOF &&
+one   two    three
+four  five   six
+seven eight  nine
+ten   eleven
+EOF
+	git column --mode=row,dense <lista >actual &&
+	test_cmp expected actual
+'
+
 test_done
-- 
1.7.8.36.g69ee2

^ permalink raw reply related	[flat|nested] 66+ messages in thread

* [PATCH v6 06/11] column: add column.ui for default column output settings
  2012-02-25 11:41 [PATCH v6 00/11] Column display Nguyễn Thái Ngọc Duy
                   ` (4 preceding siblings ...)
  2012-02-25 11:41 ` [PATCH v6 05/11] column: support columns with different widths Nguyễn Thái Ngọc Duy
@ 2012-02-25 11:41 ` Nguyễn Thái Ngọc Duy
  2012-02-27  6:20   ` Junio C Hamano
  2012-02-25 11:41 ` [PATCH v6 07/11] help: reuse print_columns() for help -a Nguyễn Thái Ngọc Duy
                   ` (6 subsequent siblings)
  12 siblings, 1 reply; 66+ messages in thread
From: Nguyễn Thái Ngọc Duy @ 2012-02-25 11:41 UTC (permalink / raw)
  To: git; +Cc: Junio C Hamano, Nguyễn Thái Ngọc Duy

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 Documentation/config.txt     |   26 ++++++++++++++++++++++++++
 Documentation/git-column.txt |    6 +++++-
 builtin/column.c             |   21 +++++++++++++++++++++
 column.c                     |   28 ++++++++++++++++++++++++++++
 column.h                     |    2 ++
 5 files changed, 82 insertions(+), 1 deletions(-)

diff --git a/Documentation/config.txt b/Documentation/config.txt
index abeb82b..5216598 100644
--- a/Documentation/config.txt
+++ b/Documentation/config.txt
@@ -821,6 +821,32 @@ color.ui::
 	`never` if you prefer git commands not to use color unless enabled
 	explicitly with some other configuration or the `--color` option.
 
+column.ui::
+	Specify whether supported commands should output in columns.
+	This variable consists of a list of tokens separated by spaces
+	or commas:
++
+--
+`always`;;
+	always show in columns
+`never`;;
+	never show in columns
+`auto`;;
+	show in columns if the output is to the terminal
+`column`;;
+	fill columns before rows (default)
+`row`;;
+	fill rows before columns
+`dense`;;
+	make unequal size columns to utilize more space
+`nodense`;;
+	make equal size columns
+`color`;;
+	input contains ANSI escape sequence for coloring
+--
++
+	This option defaults to 'never'.
+
 commit.status::
 	A boolean to enable/disable inclusion of status information in the
 	commit message template when using an editor to prepare the commit
diff --git a/Documentation/git-column.txt b/Documentation/git-column.txt
index 508b85f..94fd7ac 100644
--- a/Documentation/git-column.txt
+++ b/Documentation/git-column.txt
@@ -8,7 +8,7 @@ git-column - Display data in columns
 SYNOPSIS
 --------
 [verse]
-'git column' [--mode=<mode> | --rawmode=<n>] [--width=<width>]
+'git column' [--command=<name>] [--[raw]mode=<mode>] [--width=<width>]
 	     [--indent=<string>] [--nl=<string>] [--pading=<n>]
 
 DESCRIPTION
@@ -17,6 +17,10 @@ This command formats its input into multiple columns.
 
 OPTIONS
 -------
+--command=<name>::
+	Look up layout mode using configuration variable column.<name> and
+	column.ui.
+
 --mode=<mode>::
 	Specify layout mode. See configuration variable column.ui for option
 	syntax.
diff --git a/builtin/column.c b/builtin/column.c
index 3b0f74e..102d71b 100644
--- a/builtin/column.c
+++ b/builtin/column.c
@@ -11,12 +11,19 @@ static const char * const builtin_column_usage[] = {
 };
 static unsigned int colopts;
 
+static int column_config(const char *var, const char *value, void *cb)
+{
+	return git_column_config(var, value, cb, &colopts);
+}
+
 int cmd_column(int argc, const char **argv, const char *prefix)
 {
 	struct string_list list = STRING_LIST_INIT_DUP;
 	struct strbuf sb = STRBUF_INIT;
 	struct column_options copts;
+	const char *command = NULL, *real_command = NULL;
 	struct option options[] = {
+		OPT_STRING(0, "command", &real_command, "name", "lookup config vars"),
 		OPT_COLUMN(0, "mode", &colopts, "layout to use"),
 		OPT_INTEGER(0, "rawmode", &colopts, "layout to use"),
 		OPT_INTEGER(0, "width", &copts.width, "Maximum width"),
@@ -26,6 +33,15 @@ int cmd_column(int argc, const char **argv, const char *prefix)
 		OPT_END()
 	};
 
+	/* This one is special and must be the first one */
+	if (argc > 1 && !prefixcmp(argv[1], "--command=")) {
+		int nonitok = 0;
+		setup_git_directory_gently(&nonitok);
+
+		command = argv[1] + 10;
+		git_config(column_config, (void *)command);
+	}
+
 	memset(&copts, 0, sizeof(copts));
 	copts.width = term_columns();
 	copts.padding = 1;
@@ -33,6 +49,11 @@ int cmd_column(int argc, const char **argv, const char *prefix)
 	if (argc)
 		usage_with_options(builtin_column_usage, options);
 
+	if (real_command || command) {
+		if (!real_command || !command || strcmp(real_command, command))
+			die(_("--command must be the first argument"));
+	}
+
 	while (!strbuf_getline(&sb, stdin, '\n'))
 		string_list_append(&list, sb.buf);
 
diff --git a/column.c b/column.c
index 3c77997..94fd1a1 100644
--- a/column.c
+++ b/column.c
@@ -2,6 +2,7 @@
 #include "column.h"
 #include "string-list.h"
 #include "parse-options.h"
+#include "color.h"
 #include "utf8.h"
 
 #define MODE(mode) ((mode) & COL_MODE)
@@ -363,6 +364,33 @@ int git_config_column(unsigned int *mode, const char *value,
 	return 0;
 }
 
+static int column_config(const char *var, const char *value,
+			 const char *key, unsigned int *colopts)
+{
+	if (git_config_column(colopts, value, -1))
+		return error("invalid %s mode %s", key, value);
+	return 0;
+}
+
+int git_column_config(const char *var, const char *value,
+		      const char *command, unsigned int *colopts)
+{
+	if (!strcmp(var, "column.ui"))
+		return column_config(var, value, "column.ui", colopts);
+
+	if (command) {
+		struct strbuf sb = STRBUF_INIT;
+		int ret = 0;
+		strbuf_addf(&sb, "column.%s", command);
+		if (!strcmp(var, sb.buf))
+			ret = column_config(var, value, sb.buf, colopts);
+		strbuf_release(&sb);
+		return ret;
+	}
+
+	return 0;
+}
+
 int parseopt_column_callback(const struct option *opt,
 			     const char *arg, int unset)
 {
diff --git a/column.h b/column.h
index eb03c6c..43528da 100644
--- a/column.h
+++ b/column.h
@@ -27,6 +27,8 @@ extern void print_columns(const struct string_list *list,
 			  struct column_options *opts);
 extern int git_config_column(unsigned int *mode, const char *value,
 			     int stdout_is_tty);
+extern int git_column_config(const char *var, const char *value,
+			     const char *command, unsigned int *colopts);
 
 struct option;
 extern int parseopt_column_callback(const struct option *opt,
-- 
1.7.8.36.g69ee2

^ permalink raw reply related	[flat|nested] 66+ messages in thread

* [PATCH v6 07/11] help: reuse print_columns() for help -a
  2012-02-25 11:41 [PATCH v6 00/11] Column display Nguyễn Thái Ngọc Duy
                   ` (5 preceding siblings ...)
  2012-02-25 11:41 ` [PATCH v6 06/11] column: add column.ui for default column output settings Nguyễn Thái Ngọc Duy
@ 2012-02-25 11:41 ` Nguyễn Thái Ngọc Duy
  2012-02-25 11:41 ` [PATCH v6 08/11] branch: add --column Nguyễn Thái Ngọc Duy
                   ` (5 subsequent siblings)
  12 siblings, 0 replies; 66+ messages in thread
From: Nguyễn Thái Ngọc Duy @ 2012-02-25 11:41 UTC (permalink / raw)
  To: git; +Cc: Junio C Hamano, Nguyễn Thái Ngọc Duy

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 help.c |   48 ++++++++++++++----------------------------------
 1 files changed, 14 insertions(+), 34 deletions(-)

diff --git a/help.c b/help.c
index 14eefc9..d6d2e19 100644
--- a/help.c
+++ b/help.c
@@ -4,6 +4,8 @@
 #include "levenshtein.h"
 #include "help.h"
 #include "common-cmds.h"
+#include "string-list.h"
+#include "column.h"
 
 void add_cmdname(struct cmdnames *cmds, const char *name, int len)
 {
@@ -70,31 +72,18 @@ void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes)
 	cmds->cnt = cj;
 }
 
-static void pretty_print_string_list(struct cmdnames *cmds, int longest)
+static void pretty_print_string_list(struct cmdnames *cmds)
 {
-	int cols = 1, rows;
-	int space = longest + 1; /* min 1 SP between words */
-	int max_cols = term_columns() - 1; /* don't print *on* the edge */
-	int i, j;
-
-	if (space < max_cols)
-		cols = max_cols / space;
-	rows = DIV_ROUND_UP(cmds->cnt, cols);
-
-	for (i = 0; i < rows; i++) {
-		printf("  ");
+	struct string_list list = STRING_LIST_INIT_NODUP;
+	struct column_options copts;
+	int i;
 
-		for (j = 0; j < cols; j++) {
-			int n = j * rows + i;
-			int size = space;
-			if (n >= cmds->cnt)
-				break;
-			if (j == cols-1 || n + rows >= cmds->cnt)
-				size = 1;
-			printf("%-*s", size, cmds->names[n]->name);
-		}
-		putchar('\n');
-	}
+	for (i = 0; i < cmds->cnt; i++)
+		string_list_append(&list, cmds->names[i]->name);
+	memset(&copts, 0, sizeof(copts));
+	copts.indent = "  ";
+	print_columns(&list, COL_MODE_COLUMN | COL_ENABLED, &copts);
+	string_list_clear(&list, 0);
 }
 
 static int is_executable(const char *name)
@@ -206,22 +195,13 @@ void load_command_list(const char *prefix,
 void list_commands(const char *title, struct cmdnames *main_cmds,
 		   struct cmdnames *other_cmds)
 {
-	int i, longest = 0;
-
-	for (i = 0; i < main_cmds->cnt; i++)
-		if (longest < main_cmds->names[i]->len)
-			longest = main_cmds->names[i]->len;
-	for (i = 0; i < other_cmds->cnt; i++)
-		if (longest < other_cmds->names[i]->len)
-			longest = other_cmds->names[i]->len;
-
 	if (main_cmds->cnt) {
 		const char *exec_path = git_exec_path();
 		printf("available %s in '%s'\n", title, exec_path);
 		printf("----------------");
 		mput_char('-', strlen(title) + strlen(exec_path));
 		putchar('\n');
-		pretty_print_string_list(main_cmds, longest);
+		pretty_print_string_list(main_cmds);
 		putchar('\n');
 	}
 
@@ -230,7 +210,7 @@ void list_commands(const char *title, struct cmdnames *main_cmds,
 		printf("---------------------------------------");
 		mput_char('-', strlen(title));
 		putchar('\n');
-		pretty_print_string_list(other_cmds, longest);
+		pretty_print_string_list(other_cmds);
 		putchar('\n');
 	}
 }
-- 
1.7.8.36.g69ee2

^ permalink raw reply related	[flat|nested] 66+ messages in thread

* [PATCH v6 08/11] branch: add --column
  2012-02-25 11:41 [PATCH v6 00/11] Column display Nguyễn Thái Ngọc Duy
                   ` (6 preceding siblings ...)
  2012-02-25 11:41 ` [PATCH v6 07/11] help: reuse print_columns() for help -a Nguyễn Thái Ngọc Duy
@ 2012-02-25 11:41 ` Nguyễn Thái Ngọc Duy
  2012-02-25 11:41 ` [PATCH v6 09/11] status: " Nguyễn Thái Ngọc Duy
                   ` (4 subsequent siblings)
  12 siblings, 0 replies; 66+ messages in thread
From: Nguyễn Thái Ngọc Duy @ 2012-02-25 11:41 UTC (permalink / raw)
  To: git; +Cc: Junio C Hamano, Nguyễn Thái Ngọc Duy

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 Documentation/config.txt     |    4 ++
 Documentation/git-branch.txt |    9 +++++
 Makefile                     |    2 +-
 builtin/branch.c             |   32 +++++++++++++++--
 t/t3200-branch.sh            |   77 ++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 119 insertions(+), 5 deletions(-)

diff --git a/Documentation/config.txt b/Documentation/config.txt
index 5216598..c14db27 100644
--- a/Documentation/config.txt
+++ b/Documentation/config.txt
@@ -847,6 +847,10 @@ column.ui::
 +
 	This option defaults to 'never'.
 
+column.branch::
+	Specify whether to output branch listing in `git branch` in columns.
+	See `column.ui` for details.
+
 commit.status::
 	A boolean to enable/disable inclusion of status information in the
 	commit message template when using an editor to prepare the commit
diff --git a/Documentation/git-branch.txt b/Documentation/git-branch.txt
index 0427e80..ba5cccb 100644
--- a/Documentation/git-branch.txt
+++ b/Documentation/git-branch.txt
@@ -10,6 +10,7 @@ SYNOPSIS
 [verse]
 'git branch' [--color[=<when>] | --no-color] [-r | -a]
 	[--list] [-v [--abbrev=<length> | --no-abbrev]]
+	[--column[=<options>] | --no-column]
 	[(--merged | --no-merged | --contains) [<commit>]] [<pattern>...]
 'git branch' [--set-upstream | --track | --no-track] [-l] [-f] <branchname> [<start-point>]
 'git branch' (-m | -M) [<oldbranch>] <newbranch>
@@ -107,6 +108,14 @@ OPTIONS
 	default to color output.
 	Same as `--color=never`.
 
+--column[=<options>]::
+--no-column::
+	Display branch listing in columns. See configuration variable
+	column.branch for option syntax.`--column` and `--no-column`
+	without options are equivalent to 'always' and 'never' respectively.
++
+This option is only applicable in non-verbose mode.
+
 -r::
 --remotes::
 	List or delete (if used with -d) the remote-tracking branches.
diff --git a/Makefile b/Makefile
index 0998f0d..320d3f8 100644
--- a/Makefile
+++ b/Makefile
@@ -2168,7 +2168,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
+builtin/branch.o 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/builtin/branch.c b/builtin/branch.c
index cb17bc3..611cc0e 100644
--- a/builtin/branch.c
+++ b/builtin/branch.c
@@ -15,6 +15,8 @@
 #include "branch.h"
 #include "diff.h"
 #include "revision.h"
+#include "string-list.h"
+#include "column.h"
 
 static const char * const builtin_branch_usage[] = {
 	"git branch [options] [-r | -a] [--merged | --no-merged]",
@@ -53,6 +55,9 @@ static enum merge_filter {
 } merge_filter;
 static unsigned char merge_filter_ref[20];
 
+static struct string_list output = STRING_LIST_INIT_DUP;
+static unsigned int colopts;
+
 static int parse_branch_color_slot(const char *var, int ofs)
 {
 	if (!strcasecmp(var+ofs, "plain"))
@@ -70,6 +75,8 @@ static int parse_branch_color_slot(const char *var, int ofs)
 
 static int git_branch_config(const char *var, const char *value, void *cb)
 {
+	if (!prefixcmp(var, "column."))
+		return git_column_config(var, value, "branch", &colopts);
 	if (!strcmp(var, "color.branch")) {
 		branch_use_color = git_config_colorbool(var, value);
 		return 0;
@@ -474,7 +481,12 @@ static void print_ref_item(struct ref_item *item, int maxwidth, int verbose,
 	else if (verbose)
 		/* " f7c0c00 [ahead 58, behind 197] vcs-svn: drop obj_pool.h" */
 		add_verbose_info(&out, item, verbose, abbrev);
-	printf("%s\n", out.buf);
+	if (colopts & COL_ENABLED) {
+		assert(!verbose && "--column and --verbose are incompatible");
+		string_list_append(&output, out.buf);
+	} else {
+		printf("%s\n", out.buf);
+	}
 	strbuf_release(&name);
 	strbuf_release(&out);
 }
@@ -727,6 +739,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
 			PARSE_OPT_LASTARG_DEFAULT | PARSE_OPT_NONEG,
 			opt_parse_merge_filter, (intptr_t) "HEAD",
 		},
+		OPT_COLUMN(0, "column", &colopts, "list branches in columns"),
 		OPT_END(),
 	};
 
@@ -749,6 +762,8 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
 	}
 	hashcpy(merge_filter_ref, head_sha1);
 
+
+	colopts |= COL_ANSI;
 	argc = parse_options(argc, argv, prefix, options, builtin_branch_usage,
 			     0);
 
@@ -760,12 +775,21 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
 
 	if (abbrev == -1)
 		abbrev = DEFAULT_ABBREV;
+	if (verbose) {
+		if (explicitly_enable_column(colopts))
+			die(_("--column and --verbose are incompatible"));
+		colopts = 0;
+	}
 
 	if (delete)
 		return delete_branches(argc, argv, delete > 1, kinds);
-	else if (list)
-		return print_ref_list(kinds, detached, verbose, abbrev,
-				      with_commit, argv);
+	else if (list) {
+		int ret = print_ref_list(kinds, detached, verbose, abbrev,
+					 with_commit, argv);
+		print_columns(&output, colopts, NULL);
+		string_list_clear(&output, 0);
+		return ret;
+	}
 	else if (edit_description) {
 		const char *branch_name;
 		struct strbuf branch_ref = STRBUF_INIT;
diff --git a/t/t3200-branch.sh b/t/t3200-branch.sh
index dd1aceb..c38592a 100755
--- a/t/t3200-branch.sh
+++ b/t/t3200-branch.sh
@@ -160,6 +160,83 @@ test_expect_success 'git branch --list -d t should fail' '
 	test_path_is_missing .git/refs/heads/t
 '
 
+test_expect_success 'git branch --column' '
+	COLUMNS=80 git branch --column=column >actual &&
+	cat >expected <<\EOF &&
+  a/b/c     bam       foo       l       * master    n         o/p       r
+  abc       bar       j/k       m/m       master2   o/o       q
+EOF
+	test_cmp expected actual
+'
+
+test_expect_success 'git branch --column with an extremely long branch name' '
+	long=this/is/a/part/of/long/branch/name &&
+	long=z$long/$long/$long/$long &&
+	test_when_finished "git branch -d $long" &&
+	git branch $long &&
+	COLUMNS=80 git branch --column=column >actual &&
+	cat >expected <<EOF &&
+  a/b/c
+  abc
+  bam
+  bar
+  foo
+  j/k
+  l
+  m/m
+* master
+  master2
+  n
+  o/o
+  o/p
+  q
+  r
+  $long
+EOF
+	test_cmp expected actual
+'
+
+test_expect_success 'git branch with column.*' '
+	git config column.ui column &&
+	git config column.branch "dense" &&
+	COLUMNS=80 git branch >actual &&
+	git config --unset column.branch &&
+	git config --unset column.ui &&
+	cat >expected <<\EOF &&
+  a/b/c   bam   foo   l   * master    n     o/p   r
+  abc     bar   j/k   m/m   master2   o/o   q
+EOF
+	test_cmp expected actual
+'
+
+test_expect_success 'git branch --column -v should fail' '
+	test_must_fail git branch --column -v
+'
+
+test_expect_success 'git branch -v with column.ui ignored' '
+	git config column.ui column &&
+	COLUMNS=80 git branch -v | cut -c -10 | sed "s/ *$//" >actual &&
+	git config --unset column.ui &&
+	cat >expected <<\EOF &&
+  a/b/c
+  abc
+  bam
+  bar
+  foo
+  j/k
+  l
+  m/m
+* master
+  master2
+  n
+  o/o
+  o/p
+  q
+  r
+EOF
+	test_cmp expected actual
+'
+
 mv .git/config .git/config-saved
 
 test_expect_success 'git branch -m q q2 without config should succeed' '
-- 
1.7.8.36.g69ee2

^ permalink raw reply related	[flat|nested] 66+ messages in thread

* [PATCH v6 09/11] status: add --column
  2012-02-25 11:41 [PATCH v6 00/11] Column display Nguyễn Thái Ngọc Duy
                   ` (7 preceding siblings ...)
  2012-02-25 11:41 ` [PATCH v6 08/11] branch: add --column Nguyễn Thái Ngọc Duy
@ 2012-02-25 11:41 ` Nguyễn Thái Ngọc Duy
  2012-02-25 11:41 ` [PATCH v6 10/11] column: support piping stdout to external git-column process Nguyễn Thái Ngọc Duy
                   ` (3 subsequent siblings)
  12 siblings, 0 replies; 66+ messages in thread
From: Nguyễn Thái Ngọc Duy @ 2012-02-25 11:41 UTC (permalink / raw)
  To: git; +Cc: Junio C Hamano, Nguyễn Thái Ngọc Duy

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 Documentation/config.txt     |    4 ++++
 Documentation/git-status.txt |    7 +++++++
 Makefile                     |    2 +-
 builtin/commit.c             |    6 ++++++
 t/t7508-status.sh            |   24 ++++++++++++++++++++++++
 wt-status.c                  |   28 ++++++++++++++++++++++++++--
 wt-status.h                  |    1 +
 7 files changed, 69 insertions(+), 3 deletions(-)

diff --git a/Documentation/config.txt b/Documentation/config.txt
index c14db27..ebb210c 100644
--- a/Documentation/config.txt
+++ b/Documentation/config.txt
@@ -851,6 +851,10 @@ column.branch::
 	Specify whether to output branch listing in `git branch` in columns.
 	See `column.ui` for details.
 
+column.status::
+	Specify whether to output untracked files in `git status` in columns.
+	See `column.ui` for details.
+
 commit.status::
 	A boolean to enable/disable inclusion of status information in the
 	commit message template when using an editor to prepare the commit
diff --git a/Documentation/git-status.txt b/Documentation/git-status.txt
index 3d51717..2f87207 100644
--- a/Documentation/git-status.txt
+++ b/Documentation/git-status.txt
@@ -77,6 +77,13 @@ configuration variable documented in linkgit:git-config[1].
 	Terminate entries with NUL, instead of LF.  This implies
 	the `--porcelain` output format if no other format is given.
 
+--column[=<options>]::
+--no-column::
+	Display untracked files in columns. See configuration variable
+	column.status for option syntax.`--column` and `--no-column`
+	without options are equivalent to 'always' and 'never'
+	respectively.
+
 
 OUTPUT
 ------
diff --git a/Makefile b/Makefile
index 320d3f8..d9c5f00 100644
--- a/Makefile
+++ b/Makefile
@@ -2168,7 +2168,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
-builtin/branch.o column.o help.o pager.o: column.h
+builtin/branch.o builtin/commit.o 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/builtin/commit.c b/builtin/commit.c
index 3714582..b42b83f 100644
--- a/builtin/commit.c
+++ b/builtin/commit.c
@@ -27,6 +27,7 @@
 #include "quote.h"
 #include "submodule.h"
 #include "gpg-interface.h"
+#include "column.h"
 
 static const char * const builtin_commit_usage[] = {
 	"git commit [options] [--] <filepattern>...",
@@ -88,6 +89,7 @@ static int quiet, verbose, no_verify, allow_empty, dry_run, renew_authorship;
 static int no_post_rewrite, allow_empty_message;
 static char *untracked_files_arg, *force_date, *ignore_submodule_arg;
 static char *sign_commit;
+static unsigned int colopts;
 
 /*
  * The default commit message cleanup mode will remove the lines
@@ -1145,6 +1147,8 @@ static int git_status_config(const char *k, const char *v, void *cb)
 {
 	struct wt_status *s = cb;
 
+	if (!prefixcmp(k, "column."))
+		return git_column_config(k, v, "status", &colopts);
 	if (!strcmp(k, "status.submodulesummary")) {
 		int is_bool;
 		s->submodule_summary = git_config_bool_or_int(k, v, &is_bool);
@@ -1210,6 +1214,7 @@ int cmd_status(int argc, const char **argv, const char *prefix)
 		{ OPTION_STRING, 0, "ignore-submodules", &ignore_submodule_arg, "when",
 		  "ignore changes to submodules, optional when: all, dirty, untracked. (Default: all)",
 		  PARSE_OPT_OPTARG, NULL, (intptr_t)"all" },
+		OPT_COLUMN(0, "column", &colopts, "list untracked files in columns"),
 		OPT_END(),
 	};
 
@@ -1223,6 +1228,7 @@ int cmd_status(int argc, const char **argv, const char *prefix)
 	argc = parse_options(argc, argv, prefix,
 			     builtin_status_options,
 			     builtin_status_usage, 0);
+	s.colopts = colopts;
 
 	if (null_termination && status_format == STATUS_FORMAT_LONG)
 		status_format = STATUS_FORMAT_PORCELAIN;
diff --git a/t/t7508-status.sh b/t/t7508-status.sh
index fc57b13..8f5cfac 100755
--- a/t/t7508-status.sh
+++ b/t/t7508-status.sh
@@ -59,6 +59,30 @@ test_expect_success 'status (1)' '
 	test_i18ngrep "use \"git rm --cached <file>\.\.\.\" to unstage" output
 '
 
+test_expect_success 'status --column' '
+	COLUMNS=50 git status --column="column dense" >output &&
+	cat >expect <<\EOF &&
+# On branch master
+# Changes to be committed:
+#   (use "git reset HEAD <file>..." to unstage)
+#
+#	new file:   dir2/added
+#
+# Changes not staged for commit:
+#   (use "git add <file>..." to update what will be committed)
+#   (use "git checkout -- <file>..." to discard changes in working directory)
+#
+#	modified:   dir1/modified
+#
+# Untracked files:
+#   (use "git add <file>..." to include in what will be committed)
+#
+#	dir1/untracked dir2/untracked untracked
+#	dir2/modified  output
+EOF
+	test_cmp expect output
+'
+
 cat >expect <<\EOF
 # On branch master
 # Changes to be committed:
diff --git a/wt-status.c b/wt-status.c
index 9ffc535..eee059e 100644
--- a/wt-status.c
+++ b/wt-status.c
@@ -11,6 +11,7 @@
 #include "remote.h"
 #include "refs.h"
 #include "submodule.h"
+#include "column.h"
 
 static char default_wt_status_colors[][COLOR_MAXLEN] = {
 	GIT_COLOR_NORMAL, /* WT_STATUS_HEADER */
@@ -641,6 +642,8 @@ static void wt_status_print_other(struct wt_status *s,
 {
 	int i;
 	struct strbuf buf = STRBUF_INIT;
+	static struct string_list output = STRING_LIST_INIT_DUP;
+	struct column_options copts;
 
 	if (!l->nr)
 		return;
@@ -649,12 +652,33 @@ static void wt_status_print_other(struct wt_status *s,
 
 	for (i = 0; i < l->nr; i++) {
 		struct string_list_item *it;
+		const char *path;
 		it = &(l->items[i]);
+		path = quote_path(it->string, strlen(it->string),
+				  &buf, s->prefix);
+		if (s->colopts & COL_ENABLED) {
+			string_list_append(&output, path);
+			continue;
+		}
 		status_printf(s, color(WT_STATUS_HEADER, s), "\t");
 		status_printf_more(s, color(WT_STATUS_UNTRACKED, s),
-			"%s\n", quote_path(it->string, strlen(it->string),
-					    &buf, s->prefix));
+				   "%s\n", path);
 	}
+
+	strbuf_release(&buf);
+	if ((s->colopts & COL_ENABLED) == 0)
+		return;
+
+	strbuf_addf(&buf, "%s#\t%s",
+		    color(WT_STATUS_HEADER, s),
+		    color(WT_STATUS_UNTRACKED, s));
+	memset(&copts, 0, sizeof(copts));
+	copts.padding = 1;
+	copts.indent = buf.buf;
+	if (want_color(s->use_color))
+		copts.nl = GIT_COLOR_RESET "\n";
+	print_columns(&output, s->colopts, &copts);
+	string_list_clear(&output, 0);
 	strbuf_release(&buf);
 }
 
diff --git a/wt-status.h b/wt-status.h
index 682b4c8..6dd7207 100644
--- a/wt-status.h
+++ b/wt-status.h
@@ -56,6 +56,7 @@ struct wt_status {
 	enum untracked_status_type show_untracked_files;
 	const char *ignore_submodule_arg;
 	char color_palette[WT_STATUS_MAXSLOT][COLOR_MAXLEN];
+	int colopts;
 
 	/* These are computed during processing of the individual sections */
 	int commitable;
-- 
1.7.8.36.g69ee2

^ permalink raw reply related	[flat|nested] 66+ messages in thread

* [PATCH v6 10/11] column: support piping stdout to external git-column process
  2012-02-25 11:41 [PATCH v6 00/11] Column display Nguyễn Thái Ngọc Duy
                   ` (8 preceding siblings ...)
  2012-02-25 11:41 ` [PATCH v6 09/11] status: " Nguyễn Thái Ngọc Duy
@ 2012-02-25 11:41 ` Nguyễn Thái Ngọc Duy
  2012-02-25 11:41 ` [PATCH v6 11/11] tag: add --column Nguyễn Thái Ngọc Duy
                   ` (2 subsequent siblings)
  12 siblings, 0 replies; 66+ messages in thread
From: Nguyễn Thái Ngọc Duy @ 2012-02-25 11:41 UTC (permalink / raw)
  To: git; +Cc: Junio C Hamano, Nguyễn Thái Ngọc Duy

For too complicated output handling, it'd be easier to just spawn
git-column and redirect stdout to it. This patch provides helpers
to do that.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 column.c |   69 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 column.h |    3 ++
 2 files changed, 72 insertions(+), 0 deletions(-)

diff --git a/column.c b/column.c
index 94fd1a1..ecf2d14 100644
--- a/column.c
+++ b/column.c
@@ -2,6 +2,7 @@
 #include "column.h"
 #include "string-list.h"
 #include "parse-options.h"
+#include "run-command.h"
 #include "color.h"
 #include "utf8.h"
 
@@ -407,3 +408,71 @@ int parseopt_column_callback(const struct option *opt,
 	*mode |= COL_ENABLED | COL_ENABLED_SET;
 	return 0;
 }
+
+static int fd_out = -1;
+static struct child_process column_process;
+
+int run_column_filter(int colopts, const struct column_options *opts)
+{
+	const char *av[10];
+	int ret, ac = 0;
+	struct strbuf sb_colopt  = STRBUF_INIT;
+	struct strbuf sb_width   = STRBUF_INIT;
+	struct strbuf sb_padding = STRBUF_INIT;
+
+	if (fd_out != -1)
+		return -1;
+
+	av[ac++] = "column";
+	strbuf_addf(&sb_colopt, "--rawmode=%d", colopts);
+	av[ac++] = sb_colopt.buf;
+	if (opts->width) {
+		strbuf_addf(&sb_width, "--width=%d", opts->width);
+		av[ac++] = sb_width.buf;
+	}
+	if (opts->indent) {
+		av[ac++] = "--indent";
+		av[ac++] = opts->indent;
+	}
+	if (opts->padding) {
+		strbuf_addf(&sb_padding, "--padding=%d", opts->padding);
+		av[ac++] = sb_padding.buf;
+	}
+	av[ac] = NULL;
+
+	fflush(stdout);
+	memset(&column_process, 0, sizeof(column_process));
+	column_process.in = -1;
+	column_process.out = dup(1);
+	column_process.git_cmd = 1;
+	column_process.argv = av;
+
+	ret = start_command(&column_process);
+
+	strbuf_release(&sb_colopt);
+	strbuf_release(&sb_width);
+	strbuf_release(&sb_padding);
+
+	if (ret)
+		return -2;
+
+	fd_out = dup(1);
+	close(1);
+	dup2(column_process.in, 1);
+	close(column_process.in);
+	return 0;
+}
+
+int stop_column_filter(void)
+{
+	if (fd_out == -1)
+		return -1;
+
+	fflush(stdout);
+	close(1);
+	finish_command(&column_process);
+	dup2(fd_out, 1);
+	close(fd_out);
+	fd_out = -1;
+	return 0;
+}
diff --git a/column.h b/column.h
index 43528da..cb81c8a 100644
--- a/column.h
+++ b/column.h
@@ -34,4 +34,7 @@ struct option;
 extern int parseopt_column_callback(const struct option *opt,
 				    const char *arg, int unset);
 
+extern int run_column_filter(int colopts, const struct column_options *);
+extern int stop_column_filter();
+
 #endif
-- 
1.7.8.36.g69ee2

^ permalink raw reply related	[flat|nested] 66+ messages in thread

* [PATCH v6 11/11] tag: add --column
  2012-02-25 11:41 [PATCH v6 00/11] Column display Nguyễn Thái Ngọc Duy
                   ` (9 preceding siblings ...)
  2012-02-25 11:41 ` [PATCH v6 10/11] column: support piping stdout to external git-column process Nguyễn Thái Ngọc Duy
@ 2012-02-25 11:41 ` Nguyễn Thái Ngọc Duy
  2012-02-26 23:02 ` [PATCH v6 00/11] Column display Junio C Hamano
  2012-02-28 11:58 ` [PATCH v7 00/10] " Nguyễn Thái Ngọc Duy
  12 siblings, 0 replies; 66+ messages in thread
From: Nguyễn Thái Ngọc Duy @ 2012-02-25 11:41 UTC (permalink / raw)
  To: git; +Cc: Junio C Hamano, Nguyễn Thái Ngọc Duy

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 Documentation/config.txt  |    4 ++++
 Documentation/git-tag.txt |    9 +++++++++
 Makefile                  |    2 +-
 builtin/tag.c             |   26 +++++++++++++++++++++++---
 t/t7004-tag.sh            |   44 ++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 81 insertions(+), 4 deletions(-)

diff --git a/Documentation/config.txt b/Documentation/config.txt
index ebb210c..145336a 100644
--- a/Documentation/config.txt
+++ b/Documentation/config.txt
@@ -855,6 +855,10 @@ column.status::
 	Specify whether to output untracked files in `git status` in columns.
 	See `column.ui` for details.
 
+column.tag::
+	Specify whether to output tag listing in `git tag` in columns.
+	See `column.ui` for details.
+
 commit.status::
 	A boolean to enable/disable inclusion of status information in the
 	commit message template when using an editor to prepare the commit
diff --git a/Documentation/git-tag.txt b/Documentation/git-tag.txt
index 8d32b9a..e36a7c3 100644
--- a/Documentation/git-tag.txt
+++ b/Documentation/git-tag.txt
@@ -13,6 +13,7 @@ SYNOPSIS
 	<tagname> [<commit> | <object>]
 'git tag' -d <tagname>...
 'git tag' [-n[<num>]] -l [--contains <commit>] [--points-at <object>]
+	[--column[=<options>] | --no-column] [<pattern>...]
 	[<pattern>...]
 'git tag' -v <tagname>...
 
@@ -84,6 +85,14 @@ OPTIONS
 	using fnmatch(3)).  Multiple patterns may be given; if any of
 	them matches, the tag is shown.
 
+--column[=<options>]::
+--no-column::
+	Display tag listing in columns. See configuration variable
+	column.tag for option syntax.`--column` and `--no-column`
+	without options are equivalent to 'always' and 'never' respectively.
++
+This option is only applicable when listing tags without annotation lines.
+
 --contains <commit>::
 	Only list tags which contain the specified commit.
 
diff --git a/Makefile b/Makefile
index d9c5f00..65fc6b9 100644
--- a/Makefile
+++ b/Makefile
@@ -2168,7 +2168,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
-builtin/branch.o builtin/commit.o column.o help.o pager.o: column.h
+builtin/branch.o builtin/commit.o builtin/tag.o 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/builtin/tag.c b/builtin/tag.c
index fe7e5e5..e99cbff 100644
--- a/builtin/tag.c
+++ b/builtin/tag.c
@@ -16,6 +16,7 @@
 #include "revision.h"
 #include "gpg-interface.h"
 #include "sha1-array.h"
+#include "column.h"
 
 static const char * const git_tag_usage[] = {
 	"git tag [-a|-s|-u <key-id>] [-f] [-m <msg>|-F <file>] <tagname> [<head>]",
@@ -33,6 +34,7 @@ struct tag_filter {
 };
 
 static struct sha1_array points_at;
+static unsigned int colopts;
 
 static int match_pattern(const char **patterns, const char *ref)
 {
@@ -263,6 +265,8 @@ static int git_tag_config(const char *var, const char *value, void *cb)
 	int status = git_gpg_config(var, value, cb);
 	if (status)
 		return status;
+	if (!prefixcmp(var, "column."))
+		return git_column_config(var, value, "tag", &colopts);
 	return git_default_config(var, value, cb);
 }
 
@@ -459,6 +463,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
 		OPT_STRING('u', "local-user", &keyid, "key-id",
 					"use another key to sign the tag"),
 		OPT__FORCE(&force, "replace the tag if exists"),
+		OPT_COLUMN(0, "column", &colopts, "show tag list in columns"),
 
 		OPT_GROUP("Tag listing options"),
 		{
@@ -495,9 +500,24 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
 
 	if (list + delete + verify > 1)
 		usage_with_options(git_tag_usage, options);
-	if (list)
-		return list_tags(argv, lines == -1 ? 0 : lines,
-				 with_commit);
+	if (list && lines != -1) {
+		if (explicitly_enable_column(colopts))
+			die(_("--column and -n are incompatible"));
+		colopts = 0;
+	}
+	if (list) {
+		int ret;
+		if (colopts & COL_ENABLED) {
+			struct column_options copts;
+			memset(&copts, 0, sizeof(copts));
+			copts.padding = 2;
+			run_column_filter(colopts, &copts);
+		}
+		ret = list_tags(argv, lines == -1 ? 0 : lines, with_commit);
+		if (colopts & COL_ENABLED)
+			stop_column_filter();
+		return ret;
+	}
 	if (lines != -1)
 		die(_("-n option is only allowed with -l."));
 	if (with_commit)
diff --git a/t/t7004-tag.sh b/t/t7004-tag.sh
index f8c247a..5189446 100755
--- a/t/t7004-tag.sh
+++ b/t/t7004-tag.sh
@@ -263,6 +263,50 @@ test_expect_success 'tag -l can accept multiple patterns' '
 	test_cmp expect actual
 '
 
+test_expect_success 'listing tags in column' '
+	COLUMNS=40 git tag -l --column=row >actual &&
+	cat >expected <<\EOF &&
+a1      aa1     cba     t210    t211
+v0.2.1  v1.0    v1.0.1  v1.1.3
+EOF
+	test_cmp expected actual
+'
+
+test_expect_success 'listing tags in column with column.*' '
+	git config column.tag row &&
+	git config column.ui dense &&
+	COLUMNS=40 git tag -l >actual &&
+	git config --unset column.ui &&
+	git config --unset column.tag &&
+	cat >expected <<\EOF &&
+a1      aa1   cba     t210    t211
+v0.2.1  v1.0  v1.0.1  v1.1.3
+EOF
+	test_cmp expected actual
+'
+
+test_expect_success 'listing tag with -n --column should fail' '
+	test_must_fail git tag --column -n
+'
+
+test_expect_success 'listing tags -n in column with column.ui ignored' '
+	git config column.ui "row dense" &&
+	COLUMNS=40 git tag -l -n >actual &&
+	git config --unset column.ui &&
+	cat >expected <<\EOF &&
+a1              Foo
+aa1             Foo
+cba             Foo
+t210            Foo
+t211            Foo
+v0.2.1          Foo
+v1.0            Foo
+v1.0.1          Foo
+v1.1.3          Foo
+EOF
+	test_cmp expected actual
+'
+
 # creating and verifying lightweight tags:
 
 test_expect_success \
-- 
1.7.8.36.g69ee2

^ permalink raw reply related	[flat|nested] 66+ messages in thread

* Re: [PATCH v6 00/11] Column display
  2012-02-25 11:41 [PATCH v6 00/11] Column display Nguyễn Thái Ngọc Duy
                   ` (10 preceding siblings ...)
  2012-02-25 11:41 ` [PATCH v6 11/11] tag: add --column Nguyễn Thái Ngọc Duy
@ 2012-02-26 23:02 ` Junio C Hamano
  2012-02-27  0:40   ` Nguyen Thai Ngoc Duy
  2012-02-28 11:58 ` [PATCH v7 00/10] " Nguyễn Thái Ngọc Duy
  12 siblings, 1 reply; 66+ messages in thread
From: Junio C Hamano @ 2012-02-26 23:02 UTC (permalink / raw)
  To: Nguyễn Thái Ngọc Duy; +Cc: git

Nguyễn Thái Ngọc Duy  <pclouds@gmail.com> writes:

> Nguyễn Thái Ngọc Duy (11):
>   column: add API to print items in columns
>   Add git-column and column mode parsing
>   Stop starting pager recursively
>   column: add columnar layout
>   column: support columns with different widths
>   column: add column.ui for default column output settings
>   help: reuse print_columns() for help -a
>   branch: add --column
>   status: add --column
>   column: support piping stdout to external git-column process
>   tag: add --column

The first one adds something that nobody uses, hence it cannot be blamed
by bisecting, but the "API" gets updated over time by other changes.
Perhaps the first two may want to become a single commit.

Other than that, the patch progression looks very logical.  The column
output subsystem gets enhanced using git-column as the scaffolding to help
testing and developing it, and then various commands start to make use of
the result when the subsystem has become usable in their context.

But I am very much reluctant to see us adding a "git column" subcommand; I
cannot justify it myself because what it does is even less connected to
git than the "--no-index" mode of grep/diff commands, i.e. it does not
have much to do with "Git, the version control system".

Shouldn't it be "test-column" that is useful during the hacking, much like
"test-date", "test-chmtime", and "test_credential"?

^ permalink raw reply	[flat|nested] 66+ messages in thread

* Re: [PATCH v6 05/11] column: support columns with different widths
  2012-02-25 11:41 ` [PATCH v6 05/11] column: support columns with different widths Nguyễn Thái Ngọc Duy
@ 2012-02-26 23:12   ` Junio C Hamano
  0 siblings, 0 replies; 66+ messages in thread
From: Junio C Hamano @ 2012-02-26 23:12 UTC (permalink / raw)
  To: Nguyễn Thái Ngọc Duy; +Cc: git

Nguyễn Thái Ngọc Duy  <pclouds@gmail.com> writes:

> Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>

The contrast between this and the previous one is striking.

The addition of MODE_COLUMN and MODE_ROW are very well explained, and it
will help people to later learn how to use the columns subsystem in their
programs.  But this does not even mention what "different widths" is, let
alone explaining how to use it or if it is always on and the programs do
not have to worry about (and have no control over) its behaviour.

I am guessing from the patch that you can tell the column subsystem to use
a different mode, just like you can use MODE_COLUMN or MODE_ROW to instruct
column/row major ordering, by using MODE_DENSE or something.

I am also reading from the patch that the "dense mode" allocates minimum
necessary width dynamically to columns, which end up having different
width.  So "different" is not the more important part; "dense" is.

But the reader should not have to guess these things.

^ permalink raw reply	[flat|nested] 66+ messages in thread

* Re: [PATCH v6 00/11] Column display
  2012-02-26 23:02 ` [PATCH v6 00/11] Column display Junio C Hamano
@ 2012-02-27  0:40   ` Nguyen Thai Ngoc Duy
  2012-02-27  1:37     ` Junio C Hamano
  0 siblings, 1 reply; 66+ messages in thread
From: Nguyen Thai Ngoc Duy @ 2012-02-27  0:40 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git

2012/2/27 Junio C Hamano <gitster@pobox.com>:
> But I am very much reluctant to see us adding a "git column" subcommand; I
> cannot justify it myself because what it does is even less connected to
> git than the "--no-index" mode of grep/diff commands, i.e. it does not
> have much to do with "Git, the version control system".
>
> Shouldn't it be "test-column" that is useful during the hacking, much like
> "test-date", "test-chmtime", and "test_credential"?

It used to be test-column. But we would need an external program to
pipe through to minimize changing display code, especially when
display code can get complicated (e.g. git-tag for example). The other
reason is to get column display even when the command does not support
it (e.g. GIT_PAGER="git column" git diff --name-status HEAD~10)
-- 
Duy

^ permalink raw reply	[flat|nested] 66+ messages in thread

* Re: [PATCH v6 00/11] Column display
  2012-02-27  0:40   ` Nguyen Thai Ngoc Duy
@ 2012-02-27  1:37     ` Junio C Hamano
  2012-02-27  7:46       ` Junio C Hamano
  0 siblings, 1 reply; 66+ messages in thread
From: Junio C Hamano @ 2012-02-27  1:37 UTC (permalink / raw)
  To: Nguyen Thai Ngoc Duy; +Cc: git

Nguyen Thai Ngoc Duy <pclouds@gmail.com> writes:

> It used to be test-column. But we would need an external program to
> pipe through to minimize changing display code, especially when
> display code can get complicated (e.g. git-tag for example).

Hrm, I was hoping that we don't have to resort to a hacky implementation
like that; upload-pack/receive-pack that are not run directly by the end
users but having to be somewhere in path were bad enough, but they were
very much about git.

But at least with your series, "git column" is as invisible as git-sh-setup
and git-stripspace by being marked as purehelpers, so your patch may be
the best we could do.  I dunno.

^ permalink raw reply	[flat|nested] 66+ messages in thread

* Re: [PATCH v6 06/11] column: add column.ui for default column output settings
  2012-02-25 11:41 ` [PATCH v6 06/11] column: add column.ui for default column output settings Nguyễn Thái Ngọc Duy
@ 2012-02-27  6:20   ` Junio C Hamano
  2012-02-27  7:04     ` Nguyen Thai Ngoc Duy
  0 siblings, 1 reply; 66+ messages in thread
From: Junio C Hamano @ 2012-02-27  6:20 UTC (permalink / raw)
  To: Nguyễn Thái Ngọc Duy; +Cc: git

Nguyễn Thái Ngọc Duy  <pclouds@gmail.com> writes:

> Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
> ---
>  Documentation/config.txt     |   26 ++++++++++++++++++++++++++
>  Documentation/git-column.txt |    6 +++++-
>  builtin/column.c             |   21 +++++++++++++++++++++
>  column.c                     |   28 ++++++++++++++++++++++++++++
>  column.h                     |    2 ++
>  5 files changed, 82 insertions(+), 1 deletions(-)
>
> diff --git a/Documentation/config.txt b/Documentation/config.txt
> index abeb82b..5216598 100644
> --- a/Documentation/config.txt
> +++ b/Documentation/config.txt
> @@ -821,6 +821,32 @@ color.ui::
>  	`never` if you prefer git commands not to use color unless enabled
>  	explicitly with some other configuration or the `--color` option.
>  
> +column.ui::
> +	Specify whether supported commands should output in columns.
> +	This variable consists of a list of tokens separated by spaces
> +	or commas:
> ++
> +--
> +`always`;;
> +	always show in columns
> +`never`;;
> +	never show in columns
> +`auto`;;
> +	show in columns if the output is to the terminal
> +`column`;;
> +	fill columns before rows (default)
> +`row`;;
> +	fill rows before columns
> +`dense`;;
> +	make unequal size columns to utilize more space
> +`nodense`;;
> +	make equal size columns
> +`color`;;
> +	input contains ANSI escape sequence for coloring
> +--
> ++
> +	This option defaults to 'never'.

I tried comparing output from 

    $ git help -a
    $ git -c column.ui=row help -a

but did not spot differences.  What am I doing wrong?

^ permalink raw reply	[flat|nested] 66+ messages in thread

* Re: [PATCH v6 06/11] column: add column.ui for default column output settings
  2012-02-27  6:20   ` Junio C Hamano
@ 2012-02-27  7:04     ` Nguyen Thai Ngoc Duy
  0 siblings, 0 replies; 66+ messages in thread
From: Nguyen Thai Ngoc Duy @ 2012-02-27  7:04 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git

2012/2/27 Junio C Hamano <gitster@pobox.com>:
> I tried comparing output from
>
>    $ git help -a
>    $ git -c column.ui=row help -a
>
> but did not spot differences.  What am I doing wrong?

Config does not affect "help -a". It has always been sorted column
first. But yeah if you prefer row-first, then it should probably
follow that too.
-- 
Duy

^ permalink raw reply	[flat|nested] 66+ messages in thread

* Re: [PATCH v6 00/11] Column display
  2012-02-27  1:37     ` Junio C Hamano
@ 2012-02-27  7:46       ` Junio C Hamano
  2012-02-27  8:14         ` Nguyen Thai Ngoc Duy
  0 siblings, 1 reply; 66+ messages in thread
From: Junio C Hamano @ 2012-02-27  7:46 UTC (permalink / raw)
  To: Nguyen Thai Ngoc Duy; +Cc: git

Junio C Hamano <gitster@pobox.com> writes:

> But at least with your series, "git column" is as invisible as git-sh-setup
> and git-stripspace by being marked as purehelpers, so your patch may be
> the best we could do.  I dunno.

I finally realized the true reason why I was so reluctant to take this
series.  If the series were presented like the following, I wouldn't have
had any problem:

    There is a widely used program, "column", that takes a data stream
    one-item-per-line as its input and produces columnar output.  Some
    lists we produce as our output, e.g. "git tag --list", are often too
    tall and sufficiently narrow, that "git tag --list | column" is a
    useful thing to do.  The first patch in this series teaches "git tag
    --list" to do just that but internally, so the user does not have to
    pipe manually from the command line.

    Luckily, "column" is a free software, and more importantly, it is
    written modularly enough so that we can reuse its primary logic to
    produce lines for columnar output without having to feed its input
    from the standard input or drain its output directly to the standard
    output.  Build an API around its core, and plug that logic to "branch
    --list", "help -a" and other commands.

But that is not the case.

Yes, there is "column" that comes with BSD, and I am not suggesting to gut
it and reuse its code, but by adding our own "git column" and implementing
it as a built-in internal, we are making it harder for the outside world
to benefit from our implementation, even when our "git column" some day
becomes polished enough to the point that people who are used to the BSD
"column" start to wish their "column" had extra features offered by "git
column", without swallowing "git" as a whole.

Come to think of it, that is really the same distaste I had against both
"diff --no-index" and "grep --no-index".  We know adding our enhancements
over non-git versions of these programs is a better gift to the outside
world, but we instead added the --no-index mode to Git to only keep the
benefit to ourselves, because it is far easier for us to do so.

But I do not deeply care either way.

^ permalink raw reply	[flat|nested] 66+ messages in thread

* Re: [PATCH v6 00/11] Column display
  2012-02-27  7:46       ` Junio C Hamano
@ 2012-02-27  8:14         ` Nguyen Thai Ngoc Duy
  0 siblings, 0 replies; 66+ messages in thread
From: Nguyen Thai Ngoc Duy @ 2012-02-27  8:14 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git

On Mon, Feb 27, 2012 at 2:46 PM, Junio C Hamano <gitster@pobox.com> wrote:
> Come to think of it, that is really the same distaste I had against both
> "diff --no-index" and "grep --no-index".  We know adding our enhancements
> over non-git versions of these programs is a better gift to the outside
> world, but we instead added the --no-index mode to Git to only keep the
> benefit to ourselves, because it is far easier for us to do so.

Exactly. Some people like me also benefit from the popularity of Git.
If it's (likely) installed, I get many extra features for free. There
are machines that I'm not allowed to install new stuff on. If it comes
with BSD "column", not too bad. If not arghhh.
-- 
Duy

^ permalink raw reply	[flat|nested] 66+ messages in thread

* Re: [PATCH v6 02/11] Add git-column and column mode parsing
  2012-02-25 11:41 ` [PATCH v6 02/11] Add git-column and column mode parsing Nguyễn Thái Ngọc Duy
@ 2012-02-27 20:09   ` Ramsay Jones
  2012-02-28 11:00     ` Nguyen Thai Ngoc Duy
  0 siblings, 1 reply; 66+ messages in thread
From: Ramsay Jones @ 2012-02-27 20:09 UTC (permalink / raw)
  To: Nguyễn Thái Ngọc Duy; +Cc: git, Junio C Hamano

Nguyễn Thái Ngọc Duy wrote:
[...]
> +static int parse_option(const char *arg, int len,
> +			unsigned int *mode, int stdout_is_tty)
> +{
> +	struct colopt opts[] = {
> +		{ ENABLE, "always",  1 },
> +		{ ENABLE, "never",   0 },
> +		{ ENABLE, "auto",   -1 },
> +	};

Hmm, I don't recognise this table from last time ...

> +	int i;
> +
> +	for (i = 0; i < ARRAY_SIZE(opts); i++) {
> +		int set = 1, arg_len = len, name_len;

set is initialised here (mainly to silence the compiler) in each
loop, but then ...

> +		const char *arg_str = arg;
> +
> +		if (opts[i].type == OPTION) {
> +			if (arg_len > 2 && !strncmp(arg_str, "no", 2)) {
> +				arg_str += 2;
> +				arg_len -= 2;
> +				set = 0;
> +			} else {
> +				set = 1;

... this else clause is no longer required.

> +			}
> +		}
> +
> +		name_len = strlen(opts[i].name);
> +		if (arg_len != name_len ||
> +		    strncmp(arg_str, opts[i].name, name_len))
> +			continue;
> +
> +		switch (opts[i].type) {
> +		case ENABLE:
> +			return set_enable_bit(mode, opts[i].value,
> +					      stdout_is_tty);

given the above table, can the following case limbs ever be reached?
(the "no" prefix is only applied to the OPTION type, so most of the
above code seems to be useless now ...)

> +		case MODE:
> +			return set_mode(mode, opts[i].value);
> +		case OPTION:
> +			return set_option(mode, opts[i].value, set);
> +		default:
> +			die("BUG: Unknown option type %d", opts[i].type);
> +		}
> +	}
> +
> +	return error("unsupported style '%s'", arg);
> +}
> +
[...]

Note, I only skimmed the patch text, I haven't applied it or tested it ...

ATB,
Ramsay Jones

^ permalink raw reply	[flat|nested] 66+ messages in thread

* Re: [PATCH v6 02/11] Add git-column and column mode parsing
  2012-02-27 20:09   ` Ramsay Jones
@ 2012-02-28 11:00     ` Nguyen Thai Ngoc Duy
  0 siblings, 0 replies; 66+ messages in thread
From: Nguyen Thai Ngoc Duy @ 2012-02-28 11:00 UTC (permalink / raw)
  To: Ramsay Jones; +Cc: git, Junio C Hamano

2012/2/28 Ramsay Jones <ramsay@ramsay1.demon.co.uk>:
> Nguyễn Thái Ngọc Duy wrote:
> [...]
>> +static int parse_option(const char *arg, int len,
>> +                     unsigned int *mode, int stdout_is_tty)
>> +{
>> +     struct colopt opts[] = {
>> +             { ENABLE, "always",  1 },
>> +             { ENABLE, "never",   0 },
>> +             { ENABLE, "auto",   -1 },
>> +     };
>
> Hmm, I don't recognise this table from last time ...
>
>> +                     }
>> +             }
>> +
>> +             name_len = strlen(opts[i].name);
>> +             if (arg_len != name_len ||
>> +                 strncmp(arg_str, opts[i].name, name_len))
>> +                     continue;
>> +
>> +             switch (opts[i].type) {
>> +             case ENABLE:
>> +                     return set_enable_bit(mode, opts[i].value,
>> +                                           stdout_is_tty);
>
> given the above table, can the following case limbs ever be reached?
> (the "no" prefix is only applied to the OPTION type, so most of the
> above code seems to be useless now ...)

Not in this patch, no. The table is extended later on with more modes
and options.

>
>> +             case MODE:
>> +                     return set_mode(mode, opts[i].value);
>> +             case OPTION:
>> +                     return set_option(mode, opts[i].value, set);
>> +             default:
>> +                     die("BUG: Unknown option type %d", opts[i].type);
>> +             }
>> +     }
>> +
>> +     return error("unsupported style '%s'", arg);
>> +}
>> +

-- 
Duy

^ permalink raw reply	[flat|nested] 66+ messages in thread

* [PATCH v7 00/10] Column display
  2012-02-25 11:41 [PATCH v6 00/11] Column display Nguyễn Thái Ngọc Duy
                   ` (11 preceding siblings ...)
  2012-02-26 23:02 ` [PATCH v6 00/11] Column display Junio C Hamano
@ 2012-02-28 11:58 ` Nguyễn Thái Ngọc Duy
  2012-02-28 11:58   ` [PATCH v7 01/10] Add git-column for columnar display Nguyễn Thái Ngọc Duy
                     ` (11 more replies)
  12 siblings, 12 replies; 66+ messages in thread
From: Nguyễn Thái Ngọc Duy @ 2012-02-28 11:58 UTC (permalink / raw)
  To: git; +Cc: Junio C Hamano, Ramsay Jones, Nguyễn Thái Ngọc Duy

Changes from v6 [1]:

 - Merge the first two patches into one
 - Make "help -a" support column.ui
 - Remove redundant code Ramsay pointed out in 01/10
 - Reword COL_DENSE commit message, 04/10

[1] http://article.gmane.org/gmane.comp.version-control.git/191522

Nguyễn Thái Ngọc Duy (10):
  Add git-column for columnar display
  Stop starting pager recursively
  column: add columnar layout
  column: add dense layout support
  column: add column.ui for default column output settings
  help: reuse print_columns() for help -a
  branch: add --column
  status: add --column
  column: support piping stdout to external git-column process
  tag: add --column

 .gitignore                   |    1 +
 Documentation/config.txt     |   38 ++++
 Documentation/git-branch.txt |    9 +
 Documentation/git-column.txt |   53 +++++
 Documentation/git-status.txt |    7 +
 Documentation/git-tag.txt    |    9 +
 Makefile                     |    3 +
 builtin.h                    |    1 +
 builtin/branch.c             |   32 +++-
 builtin/column.c             |   62 ++++++
 builtin/commit.c             |    6 +
 builtin/help.c               |    7 +-
 builtin/tag.c                |   26 ++-
 column.c                     |  476 ++++++++++++++++++++++++++++++++++++++++++
 column.h                     |   40 ++++
 command-list.txt             |    1 +
 git.c                        |    1 +
 help.c                       |   54 ++----
 help.h                       |    2 +-
 pager.c                      |    2 +-
 parse-options.h              |    2 +
 t/t3200-branch.sh            |   77 +++++++
 t/t7004-tag.sh               |   44 ++++
 t/t7508-status.sh            |   24 ++
 t/t9002-column.sh            |  161 ++++++++++++++
 wt-status.c                  |   28 +++-
 wt-status.h                  |    1 +
 27 files changed, 1119 insertions(+), 48 deletions(-)
 create mode 100644 Documentation/git-column.txt
 create mode 100644 builtin/column.c
 create mode 100644 column.c
 create mode 100644 column.h
 create mode 100755 t/t9002-column.sh

-- 
1.7.8.36.g69ee2

^ permalink raw reply	[flat|nested] 66+ messages in thread

* [PATCH v7 01/10] Add git-column for columnar display
  2012-02-28 11:58 ` [PATCH v7 00/10] " Nguyễn Thái Ngọc Duy
@ 2012-02-28 11:58   ` Nguyễn Thái Ngọc Duy
  2012-02-28 18:10     ` Junio C Hamano
  2012-02-28 11:58   ` [PATCH v7 02/10] Stop starting pager recursively Nguyễn Thái Ngọc Duy
                     ` (10 subsequent siblings)
  11 siblings, 1 reply; 66+ messages in thread
From: Nguyễn Thái Ngọc Duy @ 2012-02-28 11:58 UTC (permalink / raw)
  To: git; +Cc: Junio C Hamano, Ramsay Jones, Nguyễn Thái Ngọc Duy

This command is not particularly tied to anything specific to git. The
command is added for supporting column display in various commands,
either by reusing code or piping output to this command for layour.
The command at this stage is just a skeleton, no layout code yet.

A column option string consists of many token separated by either
space of commas. A token belongs to one of three groups:

 - enabling: always, never and auto
 - layout mode: to be implemented
 - other tuning, which could be negated be prefix 'no'

A command line option without argument (e.g. --column) will enable
column output and reuse existing settings (layout mode and options..).
--no-column disables columnar output.

Thanks-to: Ramsay Jones <ramsay@ramsay1.demon.co.uk>
Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 .gitignore                   |    1 +
 Documentation/git-column.txt |   49 ++++++++++++
 Makefile                     |    3 +
 builtin.h                    |    1 +
 builtin/column.c             |   41 ++++++++++
 column.c                     |  170 ++++++++++++++++++++++++++++++++++++++++++
 column.h                     |   30 ++++++++
 command-list.txt             |    1 +
 git.c                        |    1 +
 parse-options.h              |    2 +
 t/t9002-column.sh            |   27 +++++++
 11 files changed, 326 insertions(+), 0 deletions(-)
 create mode 100644 Documentation/git-column.txt
 create mode 100644 builtin/column.c
 create mode 100644 column.c
 create mode 100644 column.h
 create mode 100755 t/t9002-column.sh

diff --git a/.gitignore b/.gitignore
index 87fcc5f..2540264 100644
--- a/.gitignore
+++ b/.gitignore
@@ -26,6 +26,7 @@
 /git-cherry-pick
 /git-clean
 /git-clone
+/git-column
 /git-commit
 /git-commit-tree
 /git-config
diff --git a/Documentation/git-column.txt b/Documentation/git-column.txt
new file mode 100644
index 0000000..508b85f
--- /dev/null
+++ b/Documentation/git-column.txt
@@ -0,0 +1,49 @@
+git-column(1)
+=============
+
+NAME
+----
+git-column - Display data in columns
+
+SYNOPSIS
+--------
+[verse]
+'git column' [--mode=<mode> | --rawmode=<n>] [--width=<width>]
+	     [--indent=<string>] [--nl=<string>] [--pading=<n>]
+
+DESCRIPTION
+-----------
+This command formats its input into multiple columns.
+
+OPTIONS
+-------
+--mode=<mode>::
+	Specify layout mode. See configuration variable column.ui for option
+	syntax.
+
+--rawmode=<n>::
+	Same as --mode but take mode encoded as a number. This is mainly used
+	by other commands that have already parsed layout mode.
+
+--width=<width>::
+	Specify the terminal width. By default 'git column' will detect the
+	terminal width, or fall back to 80 if it is unable to do so.
+
+--indent=<string>::
+	String to be printed at the beginning of each line.
+
+--nl=<N>::
+	String to be printed at the end of each line,
+	including newline character.
+
+--padding=<N>::
+	The number of spaces between columns. One space by default.
+
+
+Author
+------
+Written by Nguyen Thai Ngoc Duy <pclouds@gmail.com>
+
+GIT
+---
+Part of the linkgit:git[1] suite
diff --git a/Makefile b/Makefile
index a0de4e9..0998f0d 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
@@ -774,6 +775,7 @@ BUILTIN_OBJS += builtin/checkout-index.o
 BUILTIN_OBJS += builtin/checkout.o
 BUILTIN_OBJS += builtin/clean.o
 BUILTIN_OBJS += builtin/clone.o
+BUILTIN_OBJS += builtin/column.o
 BUILTIN_OBJS += builtin/commit-tree.o
 BUILTIN_OBJS += builtin/commit.o
 BUILTIN_OBJS += builtin/config.o
@@ -2166,6 +2168,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/builtin.h b/builtin.h
index 857b9c8..338f540 100644
--- a/builtin.h
+++ b/builtin.h
@@ -61,6 +61,7 @@ extern int cmd_cherry(int argc, const char **argv, const char *prefix);
 extern int cmd_cherry_pick(int argc, const char **argv, const char *prefix);
 extern int cmd_clone(int argc, const char **argv, const char *prefix);
 extern int cmd_clean(int argc, const char **argv, const char *prefix);
+extern int cmd_column(int argc, const char **argv, const char *prefix);
 extern int cmd_commit(int argc, const char **argv, const char *prefix);
 extern int cmd_commit_tree(int argc, const char **argv, const char *prefix);
 extern int cmd_config(int argc, const char **argv, const char *prefix);
diff --git a/builtin/column.c b/builtin/column.c
new file mode 100644
index 0000000..3b0f74e
--- /dev/null
+++ b/builtin/column.c
@@ -0,0 +1,41 @@
+#include "builtin.h"
+#include "cache.h"
+#include "strbuf.h"
+#include "parse-options.h"
+#include "string-list.h"
+#include "column.h"
+
+static const char * const builtin_column_usage[] = {
+	"git column [options]",
+	NULL
+};
+static unsigned int colopts;
+
+int cmd_column(int argc, const char **argv, const char *prefix)
+{
+	struct string_list list = STRING_LIST_INIT_DUP;
+	struct strbuf sb = STRBUF_INIT;
+	struct column_options copts;
+	struct option options[] = {
+		OPT_COLUMN(0, "mode", &colopts, "layout to use"),
+		OPT_INTEGER(0, "rawmode", &colopts, "layout to use"),
+		OPT_INTEGER(0, "width", &copts.width, "Maximum width"),
+		OPT_STRING(0, "indent", &copts.indent, "string", "Padding space on left border"),
+		OPT_INTEGER(0, "nl", &copts.nl, "Padding space on right border"),
+		OPT_INTEGER(0, "padding", &copts.padding, "Padding space between columns"),
+		OPT_END()
+	};
+
+	memset(&copts, 0, sizeof(copts));
+	copts.width = term_columns();
+	copts.padding = 1;
+	argc = parse_options(argc, argv, "", options, builtin_column_usage, 0);
+	if (argc)
+		usage_with_options(builtin_column_usage, options);
+
+	while (!strbuf_getline(&sb, stdin, '\n'))
+		string_list_append(&list, sb.buf);
+
+	print_columns(&list, colopts, &copts);
+	return 0;
+}
diff --git a/column.c b/column.c
new file mode 100644
index 0000000..d61da81
--- /dev/null
+++ b/column.c
@@ -0,0 +1,170 @@
+#include "cache.h"
+#include "column.h"
+#include "string-list.h"
+#include "parse-options.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));
+}
+
+struct colopt {
+	enum {
+		ENABLE,
+		MODE,
+		OPTION
+	} type;
+	const char *name;
+	int value;
+};
+
+/*
+ * Set COL_ENABLED and COL_ENABLED_SET. If 'set' is -1, check if
+ * stdout is tty.
+ */
+static int set_enable_bit(unsigned int *mode, int set, int stdout_is_tty)
+{
+	if (set < 0) {	/* auto */
+		if (stdout_is_tty < 0)
+			stdout_is_tty = isatty(1);
+		set = stdout_is_tty || (pager_in_use() && pager_use_color);
+	}
+	if (set)
+		*mode = *mode | COL_ENABLED | COL_ENABLED_SET;
+	else
+		*mode = (*mode & ~COL_ENABLED) | COL_ENABLED_SET;
+	return 0;
+}
+
+/*
+ * Set COL_MODE_*. mode is intially copied from column.ui. If
+ * COL_ENABLED_SET is not set, then neither 'always', 'never' nor
+ * 'auto' has been used. Default to 'always'.
+ */
+static int set_mode(unsigned int *mode, unsigned int value)
+{
+	*mode = (*mode & ~COL_MODE) | value;
+	if (!(*mode & COL_ENABLED_SET))
+		*mode |= COL_ENABLED | COL_ENABLED_SET;
+
+	return 0;
+}
+
+/* Set or unset other COL_* */
+static int set_option(unsigned int *mode, unsigned int opt, int set)
+{
+	if (set)
+		*mode |= opt;
+	else
+		*mode &= ~opt;
+	return 0;
+}
+
+static int parse_option(const char *arg, int len,
+			unsigned int *mode, int stdout_is_tty)
+{
+	struct colopt opts[] = {
+		{ ENABLE, "always",  1 },
+		{ ENABLE, "never",   0 },
+		{ ENABLE, "auto",   -1 },
+	};
+	int i;
+
+	for (i = 0; i < ARRAY_SIZE(opts); i++) {
+		int set = 1, arg_len = len, name_len;
+		const char *arg_str = arg;
+
+		if (opts[i].type == OPTION) {
+			if (arg_len > 2 && !strncmp(arg_str, "no", 2)) {
+				arg_str += 2;
+				arg_len -= 2;
+				set = 0;
+			}
+		}
+
+		name_len = strlen(opts[i].name);
+		if (arg_len != name_len ||
+		    strncmp(arg_str, opts[i].name, name_len))
+			continue;
+
+		switch (opts[i].type) {
+		case ENABLE:
+			return set_enable_bit(mode, opts[i].value,
+					      stdout_is_tty);
+		case MODE:
+			return set_mode(mode, opts[i].value);
+		case OPTION:
+			return set_option(mode, opts[i].value, set);
+		default:
+			die("BUG: Unknown option type %d", opts[i].type);
+		}
+	}
+
+	return error("unsupported style '%s'", arg);
+}
+
+int git_config_column(unsigned int *mode, const char *value,
+		      int stdout_is_tty)
+{
+	const char *sep = " ,";
+
+	while (*value) {
+		int len = strcspn(value, sep);
+		if (len) {
+			if (parse_option(value, len, mode, stdout_is_tty))
+				return -1;
+
+			value += len;
+		}
+		value += strspn(value, sep);
+	}
+	return 0;
+}
+
+int parseopt_column_callback(const struct option *opt,
+			     const char *arg, int unset)
+{
+	unsigned int *mode = opt->value;
+	*mode |= COL_PARSEOPT;
+	if (unset) {
+		*mode = (*mode & ~COL_ENABLED) | COL_ENABLED_SET;
+		return 0;
+	}
+	if (arg)
+		return git_config_column(mode, arg, -1);
+
+	/* no arg, turn it on */
+	*mode |= COL_ENABLED | COL_ENABLED_SET;
+	return 0;
+}
diff --git a/column.h b/column.h
new file mode 100644
index 0000000..67b1c4f
--- /dev/null
+++ b/column.h
@@ -0,0 +1,30 @@
+#ifndef COLUMN_H
+#define COLUMN_H
+
+#define COL_MODE          0x000F
+#define COL_ENABLED      (1 << 4)
+#define COL_ENABLED_SET  (1 << 5)  /* Has COL_ENABLED been set by config? */
+#define COL_PARSEOPT     (1 << 8)  /* --column is given */
+
+#define explicitly_enable_column(c) \
+	(((c) & (COL_PARSEOPT | COL_ENABLED)) == (COL_PARSEOPT | COL_ENABLED))
+
+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);
+extern int git_config_column(unsigned int *mode, const char *value,
+			     int stdout_is_tty);
+
+struct option;
+extern int parseopt_column_callback(const struct option *opt,
+				    const char *arg, int unset);
+
+#endif
diff --git a/command-list.txt b/command-list.txt
index a36ee9b..fe06f15 100644
--- a/command-list.txt
+++ b/command-list.txt
@@ -20,6 +20,7 @@ git-cherry-pick                         mainporcelain
 git-citool                              mainporcelain
 git-clean                               mainporcelain
 git-clone                               mainporcelain common
+git-column                              purehelpers
 git-commit                              mainporcelain common
 git-commit-tree                         plumbingmanipulators
 git-config                              ancillarymanipulators
diff --git a/git.c b/git.c
index 3805616..419e3cc 100644
--- a/git.c
+++ b/git.c
@@ -348,6 +348,7 @@ static void handle_internal_command(int argc, const char **argv)
 		{ "cherry-pick", cmd_cherry_pick, RUN_SETUP | NEED_WORK_TREE },
 		{ "clean", cmd_clean, RUN_SETUP | NEED_WORK_TREE },
 		{ "clone", cmd_clone },
+		{ "column", cmd_column },
 		{ "commit", cmd_commit, RUN_SETUP | NEED_WORK_TREE },
 		{ "commit-tree", cmd_commit_tree, RUN_SETUP },
 		{ "config", cmd_config, RUN_SETUP_GENTLY },
diff --git a/parse-options.h b/parse-options.h
index 2e811dc..56fcafd 100644
--- a/parse-options.h
+++ b/parse-options.h
@@ -238,5 +238,7 @@ extern int parse_opt_noop_cb(const struct option *, const char *, int);
 	  PARSE_OPT_OPTARG, &parse_opt_abbrev_cb, 0 }
 #define OPT__COLOR(var, h) \
 	OPT_COLOR_FLAG(0, "color", (var), (h))
+#define OPT_COLUMN(s, l, v, h) \
+	{ OPTION_CALLBACK, (s), (l), (v), "style", (h), PARSE_OPT_OPTARG, parseopt_column_callback }
 
 #endif
diff --git a/t/t9002-column.sh b/t/t9002-column.sh
new file mode 100755
index 0000000..b0b6d62
--- /dev/null
+++ b/t/t9002-column.sh
@@ -0,0 +1,27 @@
+#!/bin/sh
+
+test_description='git column'
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+	cat >lista <<\EOF
+one
+two
+three
+four
+five
+six
+seven
+eight
+nine
+ten
+eleven
+EOF
+'
+
+test_expect_success 'never' '
+	git column --mode=never <lista >actual &&
+	test_cmp lista actual
+'
+
+test_done
-- 
1.7.8.36.g69ee2

^ permalink raw reply related	[flat|nested] 66+ messages in thread

* [PATCH v7 02/10] Stop starting pager recursively
  2012-02-28 11:58 ` [PATCH v7 00/10] " Nguyễn Thái Ngọc Duy
  2012-02-28 11:58   ` [PATCH v7 01/10] Add git-column for columnar display Nguyễn Thái Ngọc Duy
@ 2012-02-28 11:58   ` Nguyễn Thái Ngọc Duy
  2012-02-28 18:13     ` Junio C Hamano
  2012-02-28 11:58   ` [PATCH v7 03/10] column: add columnar layout Nguyễn Thái Ngọc Duy
                     ` (9 subsequent siblings)
  11 siblings, 1 reply; 66+ messages in thread
From: Nguyễn Thái Ngọc Duy @ 2012-02-28 11:58 UTC (permalink / raw)
  To: git; +Cc: Junio C Hamano, Ramsay Jones, Nguyễn Thái Ngọc Duy

git-column can be used as a pager for other git commands, something
like this:

    GIT_PAGER="git -p column --mode='dense color'" git -p branch

The problem with this is that "git -p column" also has $GIT_PAGER
set so the pager runs itself again as a pager, then again and again.

Stop this.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 pager.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)

diff --git a/pager.c b/pager.c
index 05584de..4dcb08d 100644
--- a/pager.c
+++ b/pager.c
@@ -73,7 +73,7 @@ void setup_pager(void)
 {
 	const char *pager = git_pager(isatty(1));
 
-	if (!pager)
+	if (!pager || pager_in_use())
 		return;
 
 	/*
-- 
1.7.8.36.g69ee2

^ permalink raw reply related	[flat|nested] 66+ messages in thread

* [PATCH v7 03/10] column: add columnar layout
  2012-02-28 11:58 ` [PATCH v7 00/10] " Nguyễn Thái Ngọc Duy
  2012-02-28 11:58   ` [PATCH v7 01/10] Add git-column for columnar display Nguyễn Thái Ngọc Duy
  2012-02-28 11:58   ` [PATCH v7 02/10] Stop starting pager recursively Nguyễn Thái Ngọc Duy
@ 2012-02-28 11:58   ` Nguyễn Thái Ngọc Duy
  2012-02-28 18:22     ` Junio C Hamano
  2012-02-28 11:58   ` [PATCH v7 04/10] column: add dense layout support Nguyễn Thái Ngọc Duy
                     ` (8 subsequent siblings)
  11 siblings, 1 reply; 66+ messages in thread
From: Nguyễn Thái Ngọc Duy @ 2012-02-28 11:58 UTC (permalink / raw)
  To: git; +Cc: Junio C Hamano, Ramsay Jones, Nguyễn Thái Ngọc Duy

COL_MODE_COLUMN and COL_MODE_ROW fill column by column (or row by row
respectively), given the terminal width and how many space between
columns.

Strings are supposed to be in UTF-8. If strings contain ANSI escape
strings, COL_ANSI must be specified for correct length calculation.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 column.c          |  131 ++++++++++++++++++++++++++++++++++++++++++++++++++++-
 column.h          |    3 +
 t/t9002-column.sh |   86 +++++++++++++++++++++++++++++++++++
 3 files changed, 219 insertions(+), 1 deletions(-)

diff --git a/column.c b/column.c
index d61da81..6aaa829 100644
--- a/column.c
+++ b/column.c
@@ -2,8 +2,66 @@
 #include "column.h"
 #include "string-list.h"
 #include "parse-options.h"
+#include "utf8.h"
 
 #define MODE(mode) ((mode) & COL_MODE)
+#define XY2LINEAR(d, x, y) (MODE((d)->mode) == COL_MODE_COLUMN ? \
+			    (x) * (d)->rows + (y) : \
+			    (y) * (d)->cols + (x))
+
+struct column_data {
+	const struct string_list *list; /* list of all cells */
+	int mode;			/* COL_MODE */
+	int total_width;		/* terminal width */
+	int padding;			/* cell padding */
+	const char *indent;		/* left most column indentation */
+	const char *nl;
+
+	int rows, cols;
+	int *len;			/* cell length */
+};
+
+/* return length of 's' in letters, ANSI escapes stripped */
+static int item_length(int mode, const char *s)
+{
+	int len, i = 0;
+	struct strbuf str = STRBUF_INIT;
+
+	if (!(mode & COL_ANSI))
+		return utf8_strwidth(s);
+
+	strbuf_addstr(&str, s);
+	while ((s = strstr(str.buf + i, "\033[")) != NULL) {
+		int len = strspn(s + 2, "0123456789;");
+		i = s - str.buf;
+		strbuf_remove(&str, i, len + 3); /* \033[<len><func char> */
+	}
+	len = utf8_strwidth(str.buf);
+	strbuf_release(&str);
+	return len;
+}
+
+/*
+ * Calculate cell width, rows and cols for a table of equal cells, given
+ * table width and how many spaces between cells.
+ */
+static void layout(struct column_data *data, int *width)
+{
+	int i;
+
+	*width = 0;
+	for (i = 0; i < data->list->nr; i++)
+		if (*width < data->len[i])
+			*width = data->len[i];
+
+	*width += data->padding;
+
+	data->cols = (data->total_width - strlen(data->indent)) / *width;
+	if (data->cols == 0)
+		data->cols = 1;
+
+	data->rows = DIV_ROUND_UP(data->list->nr, data->cols);
+}
 
 /* Display without layout when COL_ENABLED is not set */
 static void display_plain(const struct string_list *list,
@@ -15,6 +73,65 @@ static void display_plain(const struct string_list *list,
 		printf("%s%s%s", indent, list->items[i].string, nl);
 }
 
+/* Print a cell to stdout with all necessary leading/traling space */
+static int display_cell(struct column_data *data, int initial_width,
+			const char *empty_cell, int x, int y)
+{
+	int i, len, newline;
+
+	i = XY2LINEAR(data, x, y);
+	if (i >= data->list->nr)
+		return -1;
+	len = data->len[i];
+	if (MODE(data->mode) == COL_MODE_COLUMN)
+		newline = i + data->rows >= data->list->nr;
+	else
+		newline = x == data->cols - 1 || i == data->list->nr - 1;
+
+	printf("%s%s%s",
+			x == 0 ? data->indent : "",
+			data->list->items[i].string,
+			newline ? data->nl : empty_cell + len);
+	return 0;
+}
+
+/* Display COL_MODE_COLUMN or COL_MODE_ROW */
+static void display_table(const struct string_list *list,
+			  int mode, int total_width,
+			  int padding, const char *indent,
+			  const char *nl)
+{
+	struct column_data data;
+	int x, y, i, initial_width;
+	char *empty_cell;
+
+	memset(&data, 0, sizeof(data));
+	data.list = list;
+	data.mode = mode;
+	data.total_width = total_width;
+	data.padding = padding;
+	data.indent = indent;
+	data.nl = nl;
+
+	data.len = xmalloc(sizeof(*data.len) * list->nr);
+	for (i = 0; i < list->nr; i++)
+		data.len[i] = item_length(mode, list->items[i].string);
+
+	layout(&data, &initial_width);
+
+	empty_cell = xmalloc(initial_width + 1);
+	memset(empty_cell, ' ', initial_width);
+	empty_cell[initial_width] = '\0';
+	for (y = 0; y < data.rows; y++) {
+		for (x = 0; x < data.cols; x++)
+			if (display_cell(&data, initial_width, empty_cell, x, y))
+				break;
+	}
+
+	free(data.len);
+	free(empty_cell);
+}
+
 void print_columns(const struct string_list *list, unsigned int mode,
 		   struct column_options *opts)
 {
@@ -36,7 +153,16 @@ void print_columns(const struct string_list *list, unsigned int mode,
 		display_plain(list, indent, nl);
 		return;
 	}
-	die("BUG: invalid mode %d", MODE(mode));
+
+	switch (MODE(mode)) {
+	case COL_MODE_ROW:
+	case COL_MODE_COLUMN:
+		display_table(list, mode, width, padding, indent, nl);
+		break;
+
+	default:
+		die("BUG: invalid mode %d", MODE(mode));
+	}
 }
 
 struct colopt {
@@ -98,6 +224,9 @@ static int parse_option(const char *arg, int len,
 		{ ENABLE, "always",  1 },
 		{ ENABLE, "never",   0 },
 		{ ENABLE, "auto",   -1 },
+		{ MODE,   "column", COL_MODE_COLUMN },
+		{ MODE,   "row",    COL_MODE_ROW },
+		{ OPTION, "color",  COL_ANSI },
 	};
 	int i;
 
diff --git a/column.h b/column.h
index 67b1c4f..d9d27c6 100644
--- a/column.h
+++ b/column.h
@@ -2,8 +2,11 @@
 #define COLUMN_H
 
 #define COL_MODE          0x000F
+#define COL_MODE_COLUMN        0   /* Fill columns before rows */
+#define COL_MODE_ROW           1   /* Fill rows before columns */
 #define COL_ENABLED      (1 << 4)
 #define COL_ENABLED_SET  (1 << 5)  /* Has COL_ENABLED been set by config? */
+#define COL_ANSI         (1 << 6)  /* Remove ANSI escapes from string length */
 #define COL_PARSEOPT     (1 << 8)  /* --column is given */
 
 #define explicitly_enable_column(c) \
diff --git a/t/t9002-column.sh b/t/t9002-column.sh
index b0b6d62..cffb029 100755
--- a/t/t9002-column.sh
+++ b/t/t9002-column.sh
@@ -24,4 +24,90 @@ test_expect_success 'never' '
 	test_cmp lista actual
 '
 
+test_expect_success '80 columns' '
+	cat >expected <<\EOF &&
+one    two    three  four   five   six    seven  eight  nine   ten    eleven
+EOF
+	COLUMNS=80 git column --mode=column <lista >actual &&
+	test_cmp expected actual
+'
+
+test_expect_success 'COLUMNS = 1' '
+	cat >expected <<\EOF &&
+one
+two
+three
+four
+five
+six
+seven
+eight
+nine
+ten
+eleven
+EOF
+	COLUMNS=1 git column --mode=column <lista >actual &&
+	test_cmp expected actual
+'
+
+test_expect_success 'width = 1' '
+	git column --mode=column --width=1 <lista >actual &&
+	test_cmp expected actual
+'
+
+COLUMNS=20
+export COLUMNS
+
+test_expect_success '20 columns' '
+	cat >expected <<\EOF &&
+one    seven
+two    eight
+three  nine
+four   ten
+five   eleven
+six
+EOF
+	git column --mode=column <lista >actual &&
+	test_cmp expected actual
+'
+
+test_expect_success '20 columns, padding 2' '
+	cat >expected <<\EOF &&
+one     seven
+two     eight
+three   nine
+four    ten
+five    eleven
+six
+EOF
+	git column --mode=column --padding 2 <lista >actual &&
+	test_cmp expected actual
+'
+
+test_expect_success '20 columns, indented' '
+	cat >expected <<\EOF &&
+  one    seven
+  two    eight
+  three  nine
+  four   ten
+  five   eleven
+  six
+EOF
+	git column --mode=column --indent="  " <lista >actual &&
+	test_cmp expected actual
+'
+
+test_expect_success '20 columns, row first' '
+	cat >expected <<\EOF &&
+one    two
+three  four
+five   six
+seven  eight
+nine   ten
+eleven
+EOF
+	git column --mode=row <lista >actual &&
+	test_cmp expected actual
+'
+
 test_done
-- 
1.7.8.36.g69ee2

^ permalink raw reply related	[flat|nested] 66+ messages in thread

* [PATCH v7 04/10] column: add dense layout support
  2012-02-28 11:58 ` [PATCH v7 00/10] " Nguyễn Thái Ngọc Duy
                     ` (2 preceding siblings ...)
  2012-02-28 11:58   ` [PATCH v7 03/10] column: add columnar layout Nguyễn Thái Ngọc Duy
@ 2012-02-28 11:58   ` Nguyễn Thái Ngọc Duy
  2012-02-28 18:27     ` Junio C Hamano
  2012-02-28 11:58   ` [PATCH v7 05/10] column: add column.ui for default column output settings Nguyễn Thái Ngọc Duy
                     ` (7 subsequent siblings)
  11 siblings, 1 reply; 66+ messages in thread
From: Nguyễn Thái Ngọc Duy @ 2012-02-28 11:58 UTC (permalink / raw)
  To: git; +Cc: Junio C Hamano, Ramsay Jones, Nguyễn Thái Ngọc Duy

Normally, all items will be gone through once. The largest cell will set
column width for all columns. With this strategy, one long item will
stretch out all columns, wasting space in between them.

With COL_DENSE enabled, it shrinks all columns to minimum, then attempts
to push the last row's cells over to the next column with hope that
everything still fits even there's one row less. The process is repeated
until the new layout cannot fit in given width anymore, or there's only
one row left (perfect!).

Apparently, this mode consumes more cpu than the former, but makes
better use of terminal space. For layouting one or two screens, cpu
usage should not be a problem.

Thanks-to: Ramsay Jones <ramsay@ramsay1.demon.co.uk>
Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 column.c          |   80 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 column.h          |    2 +
 t/t9002-column.sh |   48 +++++++++++++++++++++++++++++++
 3 files changed, 130 insertions(+), 0 deletions(-)

diff --git a/column.c b/column.c
index 6aaa829..95faec5 100644
--- a/column.c
+++ b/column.c
@@ -19,6 +19,7 @@ struct column_data {
 
 	int rows, cols;
 	int *len;			/* cell length */
+	int *width;			/* index to the longest row in column */
 };
 
 /* return length of 's' in letters, ANSI escapes stripped */
@@ -63,6 +64,69 @@ static void layout(struct column_data *data, int *width)
 	data->rows = DIV_ROUND_UP(data->list->nr, data->cols);
 }
 
+static void compute_column_width(struct column_data *data)
+{
+	int i, x, y;
+	for (x = 0; x < data->cols; x++) {
+		data->width[x] = XY2LINEAR(data, x, 0);
+		for (y = 0; y < data->rows; y++) {
+			i = XY2LINEAR(data, x, y);
+			if (i >= data->list->nr)
+				continue;
+			if (data->len[data->width[x]] < data->len[i])
+				data->width[x] = i;
+		}
+	}
+}
+
+/*
+ * Shrink all columns by shortening them one row each time (and adding
+ * more columns along the way). Hopefully the longest cell will be
+ * moved to the next column, column is shrunk so we have more space
+ * for new columns. The process ends when the whole thing no longer
+ * fits in data->total_width.
+ */
+static void shrink_columns(struct column_data *data)
+{
+	int x, y, total_width, cols, rows;
+
+	data->width = xrealloc(data->width,
+			       sizeof(*data->width) * data->cols);
+	for (x = 0; x < data->cols; x++) {
+		data->width[x] = 0;
+		for (y = 0; y < data->rows; y++) {
+			int len1 = data->len[data->width[x]];
+			int len2 = data->len[XY2LINEAR(data, x, y)];
+			if (len1 < len2)
+				data->width[x] = y;
+		}
+	}
+
+	while (data->rows > 1) {
+		rows = data->rows;
+		cols = data->cols;
+
+		data->rows--;
+		data->cols = DIV_ROUND_UP(data->list->nr, data->rows);
+		if (data->cols != cols)
+			data->width = xrealloc(data->width, sizeof(*data->width) * data->cols);
+
+		compute_column_width(data);
+
+		total_width = strlen(data->indent);
+		for (x = 0; x < data->cols; x++) {
+			total_width += data->len[data->width[x]];
+			total_width += data->padding;
+		}
+		if (total_width > data->total_width) {
+			data->rows = rows;
+			data->cols = cols;
+			compute_column_width(data);
+			break;
+		}
+	}
+}
+
 /* Display without layout when COL_ENABLED is not set */
 static void display_plain(const struct string_list *list,
 			  const char *indent, const char *nl)
@@ -82,7 +146,18 @@ static int display_cell(struct column_data *data, int initial_width,
 	i = XY2LINEAR(data, x, y);
 	if (i >= data->list->nr)
 		return -1;
+
 	len = data->len[i];
+	if (data->width && data->len[data->width[x]] < initial_width) {
+		/*
+		 * empty_cell has initial_width chars, if real column
+		 * is narrower, increase len a bit so we fill less
+		 * space.
+		 */
+		len += initial_width - data->len[data->width[x]];
+		len -= data->padding;
+	}
+
 	if (MODE(data->mode) == COL_MODE_COLUMN)
 		newline = i + data->rows >= data->list->nr;
 	else
@@ -119,6 +194,9 @@ static void display_table(const struct string_list *list,
 
 	layout(&data, &initial_width);
 
+	if (mode & COL_DENSE)
+		shrink_columns(&data);
+
 	empty_cell = xmalloc(initial_width + 1);
 	memset(empty_cell, ' ', initial_width);
 	empty_cell[initial_width] = '\0';
@@ -129,6 +207,7 @@ static void display_table(const struct string_list *list,
 	}
 
 	free(data.len);
+	free(data.width);
 	free(empty_cell);
 }
 
@@ -227,6 +306,7 @@ static int parse_option(const char *arg, int len,
 		{ MODE,   "column", COL_MODE_COLUMN },
 		{ MODE,   "row",    COL_MODE_ROW },
 		{ OPTION, "color",  COL_ANSI },
+		{ OPTION, "dense",  COL_DENSE },
 	};
 	int i;
 
diff --git a/column.h b/column.h
index d9d27c6..eb03c6c 100644
--- a/column.h
+++ b/column.h
@@ -7,6 +7,8 @@
 #define COL_ENABLED      (1 << 4)
 #define COL_ENABLED_SET  (1 << 5)  /* Has COL_ENABLED been set by config? */
 #define COL_ANSI         (1 << 6)  /* Remove ANSI escapes from string length */
+#define COL_DENSE        (1 << 7)  /* Shrink columns when possible,
+				      making space for more columns */
 #define COL_PARSEOPT     (1 << 8)  /* --column is given */
 
 #define explicitly_enable_column(c) \
diff --git a/t/t9002-column.sh b/t/t9002-column.sh
index cffb029..fe7a30e 100755
--- a/t/t9002-column.sh
+++ b/t/t9002-column.sh
@@ -71,6 +71,30 @@ EOF
 	test_cmp expected actual
 '
 
+test_expect_success '20 columns, nodense' '
+	cat >expected <<\EOF &&
+one    seven
+two    eight
+three  nine
+four   ten
+five   eleven
+six
+EOF
+	git column --mode=column,nodense < lista > actual &&
+	test_cmp expected actual
+'
+
+test_expect_success '20 columns, dense' '
+	cat >expected <<\EOF &&
+one   five  nine
+two   six   ten
+three seven eleven
+four  eight
+EOF
+	git column --mode=column,dense < lista > actual &&
+	test_cmp expected actual
+'
+
 test_expect_success '20 columns, padding 2' '
 	cat >expected <<\EOF &&
 one     seven
@@ -110,4 +134,28 @@ EOF
 	test_cmp expected actual
 '
 
+test_expect_success '20 columns, row first, nodense' '
+	cat >expected <<\EOF &&
+one    two
+three  four
+five   six
+seven  eight
+nine   ten
+eleven
+EOF
+	git column --mode=row,nodense <lista >actual &&
+	test_cmp expected actual
+'
+
+test_expect_success '20 columns, row first, dense' '
+	cat >expected <<\EOF &&
+one   two    three
+four  five   six
+seven eight  nine
+ten   eleven
+EOF
+	git column --mode=row,dense <lista >actual &&
+	test_cmp expected actual
+'
+
 test_done
-- 
1.7.8.36.g69ee2

^ permalink raw reply related	[flat|nested] 66+ messages in thread

* [PATCH v7 05/10] column: add column.ui for default column output settings
  2012-02-28 11:58 ` [PATCH v7 00/10] " Nguyễn Thái Ngọc Duy
                     ` (3 preceding siblings ...)
  2012-02-28 11:58   ` [PATCH v7 04/10] column: add dense layout support Nguyễn Thái Ngọc Duy
@ 2012-02-28 11:58   ` Nguyễn Thái Ngọc Duy
  2012-02-28 18:44     ` Junio C Hamano
  2012-02-28 11:58   ` [PATCH v7 06/10] help: reuse print_columns() for help -a Nguyễn Thái Ngọc Duy
                     ` (6 subsequent siblings)
  11 siblings, 1 reply; 66+ messages in thread
From: Nguyễn Thái Ngọc Duy @ 2012-02-28 11:58 UTC (permalink / raw)
  To: git; +Cc: Junio C Hamano, Ramsay Jones, Nguyễn Thái Ngọc Duy

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 Documentation/config.txt     |   26 ++++++++++++++++++++++++++
 Documentation/git-column.txt |    6 +++++-
 builtin/column.c             |   21 +++++++++++++++++++++
 column.c                     |   28 ++++++++++++++++++++++++++++
 column.h                     |    2 ++
 5 files changed, 82 insertions(+), 1 deletions(-)

diff --git a/Documentation/config.txt b/Documentation/config.txt
index abeb82b..5216598 100644
--- a/Documentation/config.txt
+++ b/Documentation/config.txt
@@ -821,6 +821,32 @@ color.ui::
 	`never` if you prefer git commands not to use color unless enabled
 	explicitly with some other configuration or the `--color` option.
 
+column.ui::
+	Specify whether supported commands should output in columns.
+	This variable consists of a list of tokens separated by spaces
+	or commas:
++
+--
+`always`;;
+	always show in columns
+`never`;;
+	never show in columns
+`auto`;;
+	show in columns if the output is to the terminal
+`column`;;
+	fill columns before rows (default)
+`row`;;
+	fill rows before columns
+`dense`;;
+	make unequal size columns to utilize more space
+`nodense`;;
+	make equal size columns
+`color`;;
+	input contains ANSI escape sequence for coloring
+--
++
+	This option defaults to 'never'.
+
 commit.status::
 	A boolean to enable/disable inclusion of status information in the
 	commit message template when using an editor to prepare the commit
diff --git a/Documentation/git-column.txt b/Documentation/git-column.txt
index 508b85f..94fd7ac 100644
--- a/Documentation/git-column.txt
+++ b/Documentation/git-column.txt
@@ -8,7 +8,7 @@ git-column - Display data in columns
 SYNOPSIS
 --------
 [verse]
-'git column' [--mode=<mode> | --rawmode=<n>] [--width=<width>]
+'git column' [--command=<name>] [--[raw]mode=<mode>] [--width=<width>]
 	     [--indent=<string>] [--nl=<string>] [--pading=<n>]
 
 DESCRIPTION
@@ -17,6 +17,10 @@ This command formats its input into multiple columns.
 
 OPTIONS
 -------
+--command=<name>::
+	Look up layout mode using configuration variable column.<name> and
+	column.ui.
+
 --mode=<mode>::
 	Specify layout mode. See configuration variable column.ui for option
 	syntax.
diff --git a/builtin/column.c b/builtin/column.c
index 3b0f74e..102d71b 100644
--- a/builtin/column.c
+++ b/builtin/column.c
@@ -11,12 +11,19 @@ static const char * const builtin_column_usage[] = {
 };
 static unsigned int colopts;
 
+static int column_config(const char *var, const char *value, void *cb)
+{
+	return git_column_config(var, value, cb, &colopts);
+}
+
 int cmd_column(int argc, const char **argv, const char *prefix)
 {
 	struct string_list list = STRING_LIST_INIT_DUP;
 	struct strbuf sb = STRBUF_INIT;
 	struct column_options copts;
+	const char *command = NULL, *real_command = NULL;
 	struct option options[] = {
+		OPT_STRING(0, "command", &real_command, "name", "lookup config vars"),
 		OPT_COLUMN(0, "mode", &colopts, "layout to use"),
 		OPT_INTEGER(0, "rawmode", &colopts, "layout to use"),
 		OPT_INTEGER(0, "width", &copts.width, "Maximum width"),
@@ -26,6 +33,15 @@ int cmd_column(int argc, const char **argv, const char *prefix)
 		OPT_END()
 	};
 
+	/* This one is special and must be the first one */
+	if (argc > 1 && !prefixcmp(argv[1], "--command=")) {
+		int nonitok = 0;
+		setup_git_directory_gently(&nonitok);
+
+		command = argv[1] + 10;
+		git_config(column_config, (void *)command);
+	}
+
 	memset(&copts, 0, sizeof(copts));
 	copts.width = term_columns();
 	copts.padding = 1;
@@ -33,6 +49,11 @@ int cmd_column(int argc, const char **argv, const char *prefix)
 	if (argc)
 		usage_with_options(builtin_column_usage, options);
 
+	if (real_command || command) {
+		if (!real_command || !command || strcmp(real_command, command))
+			die(_("--command must be the first argument"));
+	}
+
 	while (!strbuf_getline(&sb, stdin, '\n'))
 		string_list_append(&list, sb.buf);
 
diff --git a/column.c b/column.c
index 95faec5..67bfd06 100644
--- a/column.c
+++ b/column.c
@@ -2,6 +2,7 @@
 #include "column.h"
 #include "string-list.h"
 #include "parse-options.h"
+#include "color.h"
 #include "utf8.h"
 
 #define MODE(mode) ((mode) & COL_MODE)
@@ -361,6 +362,33 @@ int git_config_column(unsigned int *mode, const char *value,
 	return 0;
 }
 
+static int column_config(const char *var, const char *value,
+			 const char *key, unsigned int *colopts)
+{
+	if (git_config_column(colopts, value, -1))
+		return error("invalid %s mode %s", key, value);
+	return 0;
+}
+
+int git_column_config(const char *var, const char *value,
+		      const char *command, unsigned int *colopts)
+{
+	if (!strcmp(var, "column.ui"))
+		return column_config(var, value, "column.ui", colopts);
+
+	if (command) {
+		struct strbuf sb = STRBUF_INIT;
+		int ret = 0;
+		strbuf_addf(&sb, "column.%s", command);
+		if (!strcmp(var, sb.buf))
+			ret = column_config(var, value, sb.buf, colopts);
+		strbuf_release(&sb);
+		return ret;
+	}
+
+	return 0;
+}
+
 int parseopt_column_callback(const struct option *opt,
 			     const char *arg, int unset)
 {
diff --git a/column.h b/column.h
index eb03c6c..43528da 100644
--- a/column.h
+++ b/column.h
@@ -27,6 +27,8 @@ extern void print_columns(const struct string_list *list,
 			  struct column_options *opts);
 extern int git_config_column(unsigned int *mode, const char *value,
 			     int stdout_is_tty);
+extern int git_column_config(const char *var, const char *value,
+			     const char *command, unsigned int *colopts);
 
 struct option;
 extern int parseopt_column_callback(const struct option *opt,
-- 
1.7.8.36.g69ee2

^ permalink raw reply related	[flat|nested] 66+ messages in thread

* [PATCH v7 06/10] help: reuse print_columns() for help -a
  2012-02-28 11:58 ` [PATCH v7 00/10] " Nguyễn Thái Ngọc Duy
                     ` (4 preceding siblings ...)
  2012-02-28 11:58   ` [PATCH v7 05/10] column: add column.ui for default column output settings Nguyễn Thái Ngọc Duy
@ 2012-02-28 11:58   ` Nguyễn Thái Ngọc Duy
  2012-02-28 11:58   ` [PATCH v7 07/10] branch: add --column Nguyễn Thái Ngọc Duy
                     ` (5 subsequent siblings)
  11 siblings, 0 replies; 66+ messages in thread
From: Nguyễn Thái Ngọc Duy @ 2012-02-28 11:58 UTC (permalink / raw)
  To: git; +Cc: Junio C Hamano, Ramsay Jones, Nguyễn Thái Ngọc Duy

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 builtin/help.c |    7 ++++++-
 help.c         |   54 ++++++++++++++++++------------------------------------
 help.h         |    2 +-
 3 files changed, 25 insertions(+), 38 deletions(-)

diff --git a/builtin/help.c b/builtin/help.c
index 61ff798..c64f152 100644
--- a/builtin/help.c
+++ b/builtin/help.c
@@ -9,6 +9,7 @@
 #include "common-cmds.h"
 #include "parse-options.h"
 #include "run-command.h"
+#include "column.h"
 #include "help.h"
 
 static struct man_viewer_list {
@@ -30,6 +31,7 @@ enum help_format {
 };
 
 static int show_all = 0;
+static unsigned int colopts;
 static enum help_format help_format = HELP_FORMAT_NONE;
 static struct option builtin_help_options[] = {
 	OPT_BOOLEAN('a', "all", &show_all, "print all available commands"),
@@ -251,6 +253,8 @@ static int add_man_viewer_info(const char *var, const char *value)
 
 static int git_help_config(const char *var, const char *value, void *cb)
 {
+	if (!prefixcmp(var, "column."))
+		return git_column_config(var, value, "help", &colopts);
 	if (!strcmp(var, "help.format")) {
 		if (!value)
 			return config_error_nonbool(var);
@@ -424,8 +428,9 @@ int cmd_help(int argc, const char **argv, const char *prefix)
 	parsed_help_format = help_format;
 
 	if (show_all) {
+		git_config(git_help_config, NULL);
 		printf("usage: %s\n\n", git_usage_string);
-		list_commands("git commands", &main_cmds, &other_cmds);
+		list_commands("git commands", colopts, &main_cmds, &other_cmds);
 		printf("%s\n", git_more_info_string);
 		return 0;
 	}
diff --git a/help.c b/help.c
index 14eefc9..3155c68 100644
--- a/help.c
+++ b/help.c
@@ -4,6 +4,8 @@
 #include "levenshtein.h"
 #include "help.h"
 #include "common-cmds.h"
+#include "string-list.h"
+#include "column.h"
 
 void add_cmdname(struct cmdnames *cmds, const char *name, int len)
 {
@@ -70,31 +72,20 @@ void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes)
 	cmds->cnt = cj;
 }
 
-static void pretty_print_string_list(struct cmdnames *cmds, int longest)
+static void pretty_print_string_list(struct cmdnames *cmds, unsigned int colopts)
 {
-	int cols = 1, rows;
-	int space = longest + 1; /* min 1 SP between words */
-	int max_cols = term_columns() - 1; /* don't print *on* the edge */
-	int i, j;
-
-	if (space < max_cols)
-		cols = max_cols / space;
-	rows = DIV_ROUND_UP(cmds->cnt, cols);
-
-	for (i = 0; i < rows; i++) {
-		printf("  ");
+	struct string_list list = STRING_LIST_INIT_NODUP;
+	struct column_options copts;
+	int i;
 
-		for (j = 0; j < cols; j++) {
-			int n = j * rows + i;
-			int size = space;
-			if (n >= cmds->cnt)
-				break;
-			if (j == cols-1 || n + rows >= cmds->cnt)
-				size = 1;
-			printf("%-*s", size, cmds->names[n]->name);
-		}
-		putchar('\n');
-	}
+	for (i = 0; i < cmds->cnt; i++)
+		string_list_append(&list, cmds->names[i]->name);
+	memset(&copts, 0, sizeof(copts));
+	copts.indent = "  ";
+	copts.padding = 2;
+	/* always enable column display here */
+	print_columns(&list, colopts | COL_ENABLED, &copts);
+	string_list_clear(&list, 0);
 }
 
 static int is_executable(const char *name)
@@ -203,25 +194,16 @@ void load_command_list(const char *prefix,
 	exclude_cmds(other_cmds, main_cmds);
 }
 
-void list_commands(const char *title, struct cmdnames *main_cmds,
-		   struct cmdnames *other_cmds)
+void list_commands(const char *title, unsigned int colopts,
+		   struct cmdnames *main_cmds, struct cmdnames *other_cmds)
 {
-	int i, longest = 0;
-
-	for (i = 0; i < main_cmds->cnt; i++)
-		if (longest < main_cmds->names[i]->len)
-			longest = main_cmds->names[i]->len;
-	for (i = 0; i < other_cmds->cnt; i++)
-		if (longest < other_cmds->names[i]->len)
-			longest = other_cmds->names[i]->len;
-
 	if (main_cmds->cnt) {
 		const char *exec_path = git_exec_path();
 		printf("available %s in '%s'\n", title, exec_path);
 		printf("----------------");
 		mput_char('-', strlen(title) + strlen(exec_path));
 		putchar('\n');
-		pretty_print_string_list(main_cmds, longest);
+		pretty_print_string_list(main_cmds, colopts);
 		putchar('\n');
 	}
 
@@ -230,7 +212,7 @@ void list_commands(const char *title, struct cmdnames *main_cmds,
 		printf("---------------------------------------");
 		mput_char('-', strlen(title));
 		putchar('\n');
-		pretty_print_string_list(other_cmds, longest);
+		pretty_print_string_list(other_cmds, colopts);
 		putchar('\n');
 	}
 }
diff --git a/help.h b/help.h
index b6b12d5..854d2d4 100644
--- a/help.h
+++ b/help.h
@@ -25,7 +25,7 @@ extern void add_cmdname(struct cmdnames *cmds, const char *name, int len);
 /* Here we require that excludes is a sorted list. */
 extern void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes);
 extern int is_in_cmdlist(struct cmdnames *cmds, const char *name);
-extern void list_commands(const char *title,
+extern void list_commands(const char *title, unsigned int colopts,
 			  struct cmdnames *main_cmds,
 			  struct cmdnames *other_cmds);
 
-- 
1.7.8.36.g69ee2

^ permalink raw reply related	[flat|nested] 66+ messages in thread

* [PATCH v7 07/10] branch: add --column
  2012-02-28 11:58 ` [PATCH v7 00/10] " Nguyễn Thái Ngọc Duy
                     ` (5 preceding siblings ...)
  2012-02-28 11:58   ` [PATCH v7 06/10] help: reuse print_columns() for help -a Nguyễn Thái Ngọc Duy
@ 2012-02-28 11:58   ` Nguyễn Thái Ngọc Duy
  2012-02-28 11:58   ` [PATCH v7 08/10] status: " Nguyễn Thái Ngọc Duy
                     ` (4 subsequent siblings)
  11 siblings, 0 replies; 66+ messages in thread
From: Nguyễn Thái Ngọc Duy @ 2012-02-28 11:58 UTC (permalink / raw)
  To: git; +Cc: Junio C Hamano, Ramsay Jones, Nguyễn Thái Ngọc Duy

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 Documentation/config.txt     |    4 ++
 Documentation/git-branch.txt |    9 +++++
 Makefile                     |    2 +-
 builtin/branch.c             |   32 +++++++++++++++--
 t/t3200-branch.sh            |   77 ++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 119 insertions(+), 5 deletions(-)

diff --git a/Documentation/config.txt b/Documentation/config.txt
index 5216598..c14db27 100644
--- a/Documentation/config.txt
+++ b/Documentation/config.txt
@@ -847,6 +847,10 @@ column.ui::
 +
 	This option defaults to 'never'.
 
+column.branch::
+	Specify whether to output branch listing in `git branch` in columns.
+	See `column.ui` for details.
+
 commit.status::
 	A boolean to enable/disable inclusion of status information in the
 	commit message template when using an editor to prepare the commit
diff --git a/Documentation/git-branch.txt b/Documentation/git-branch.txt
index 0427e80..ba5cccb 100644
--- a/Documentation/git-branch.txt
+++ b/Documentation/git-branch.txt
@@ -10,6 +10,7 @@ SYNOPSIS
 [verse]
 'git branch' [--color[=<when>] | --no-color] [-r | -a]
 	[--list] [-v [--abbrev=<length> | --no-abbrev]]
+	[--column[=<options>] | --no-column]
 	[(--merged | --no-merged | --contains) [<commit>]] [<pattern>...]
 'git branch' [--set-upstream | --track | --no-track] [-l] [-f] <branchname> [<start-point>]
 'git branch' (-m | -M) [<oldbranch>] <newbranch>
@@ -107,6 +108,14 @@ OPTIONS
 	default to color output.
 	Same as `--color=never`.
 
+--column[=<options>]::
+--no-column::
+	Display branch listing in columns. See configuration variable
+	column.branch for option syntax.`--column` and `--no-column`
+	without options are equivalent to 'always' and 'never' respectively.
++
+This option is only applicable in non-verbose mode.
+
 -r::
 --remotes::
 	List or delete (if used with -d) the remote-tracking branches.
diff --git a/Makefile b/Makefile
index 0998f0d..320d3f8 100644
--- a/Makefile
+++ b/Makefile
@@ -2168,7 +2168,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
+builtin/branch.o 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/builtin/branch.c b/builtin/branch.c
index cb17bc3..611cc0e 100644
--- a/builtin/branch.c
+++ b/builtin/branch.c
@@ -15,6 +15,8 @@
 #include "branch.h"
 #include "diff.h"
 #include "revision.h"
+#include "string-list.h"
+#include "column.h"
 
 static const char * const builtin_branch_usage[] = {
 	"git branch [options] [-r | -a] [--merged | --no-merged]",
@@ -53,6 +55,9 @@ static enum merge_filter {
 } merge_filter;
 static unsigned char merge_filter_ref[20];
 
+static struct string_list output = STRING_LIST_INIT_DUP;
+static unsigned int colopts;
+
 static int parse_branch_color_slot(const char *var, int ofs)
 {
 	if (!strcasecmp(var+ofs, "plain"))
@@ -70,6 +75,8 @@ static int parse_branch_color_slot(const char *var, int ofs)
 
 static int git_branch_config(const char *var, const char *value, void *cb)
 {
+	if (!prefixcmp(var, "column."))
+		return git_column_config(var, value, "branch", &colopts);
 	if (!strcmp(var, "color.branch")) {
 		branch_use_color = git_config_colorbool(var, value);
 		return 0;
@@ -474,7 +481,12 @@ static void print_ref_item(struct ref_item *item, int maxwidth, int verbose,
 	else if (verbose)
 		/* " f7c0c00 [ahead 58, behind 197] vcs-svn: drop obj_pool.h" */
 		add_verbose_info(&out, item, verbose, abbrev);
-	printf("%s\n", out.buf);
+	if (colopts & COL_ENABLED) {
+		assert(!verbose && "--column and --verbose are incompatible");
+		string_list_append(&output, out.buf);
+	} else {
+		printf("%s\n", out.buf);
+	}
 	strbuf_release(&name);
 	strbuf_release(&out);
 }
@@ -727,6 +739,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
 			PARSE_OPT_LASTARG_DEFAULT | PARSE_OPT_NONEG,
 			opt_parse_merge_filter, (intptr_t) "HEAD",
 		},
+		OPT_COLUMN(0, "column", &colopts, "list branches in columns"),
 		OPT_END(),
 	};
 
@@ -749,6 +762,8 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
 	}
 	hashcpy(merge_filter_ref, head_sha1);
 
+
+	colopts |= COL_ANSI;
 	argc = parse_options(argc, argv, prefix, options, builtin_branch_usage,
 			     0);
 
@@ -760,12 +775,21 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
 
 	if (abbrev == -1)
 		abbrev = DEFAULT_ABBREV;
+	if (verbose) {
+		if (explicitly_enable_column(colopts))
+			die(_("--column and --verbose are incompatible"));
+		colopts = 0;
+	}
 
 	if (delete)
 		return delete_branches(argc, argv, delete > 1, kinds);
-	else if (list)
-		return print_ref_list(kinds, detached, verbose, abbrev,
-				      with_commit, argv);
+	else if (list) {
+		int ret = print_ref_list(kinds, detached, verbose, abbrev,
+					 with_commit, argv);
+		print_columns(&output, colopts, NULL);
+		string_list_clear(&output, 0);
+		return ret;
+	}
 	else if (edit_description) {
 		const char *branch_name;
 		struct strbuf branch_ref = STRBUF_INIT;
diff --git a/t/t3200-branch.sh b/t/t3200-branch.sh
index dd1aceb..c38592a 100755
--- a/t/t3200-branch.sh
+++ b/t/t3200-branch.sh
@@ -160,6 +160,83 @@ test_expect_success 'git branch --list -d t should fail' '
 	test_path_is_missing .git/refs/heads/t
 '
 
+test_expect_success 'git branch --column' '
+	COLUMNS=80 git branch --column=column >actual &&
+	cat >expected <<\EOF &&
+  a/b/c     bam       foo       l       * master    n         o/p       r
+  abc       bar       j/k       m/m       master2   o/o       q
+EOF
+	test_cmp expected actual
+'
+
+test_expect_success 'git branch --column with an extremely long branch name' '
+	long=this/is/a/part/of/long/branch/name &&
+	long=z$long/$long/$long/$long &&
+	test_when_finished "git branch -d $long" &&
+	git branch $long &&
+	COLUMNS=80 git branch --column=column >actual &&
+	cat >expected <<EOF &&
+  a/b/c
+  abc
+  bam
+  bar
+  foo
+  j/k
+  l
+  m/m
+* master
+  master2
+  n
+  o/o
+  o/p
+  q
+  r
+  $long
+EOF
+	test_cmp expected actual
+'
+
+test_expect_success 'git branch with column.*' '
+	git config column.ui column &&
+	git config column.branch "dense" &&
+	COLUMNS=80 git branch >actual &&
+	git config --unset column.branch &&
+	git config --unset column.ui &&
+	cat >expected <<\EOF &&
+  a/b/c   bam   foo   l   * master    n     o/p   r
+  abc     bar   j/k   m/m   master2   o/o   q
+EOF
+	test_cmp expected actual
+'
+
+test_expect_success 'git branch --column -v should fail' '
+	test_must_fail git branch --column -v
+'
+
+test_expect_success 'git branch -v with column.ui ignored' '
+	git config column.ui column &&
+	COLUMNS=80 git branch -v | cut -c -10 | sed "s/ *$//" >actual &&
+	git config --unset column.ui &&
+	cat >expected <<\EOF &&
+  a/b/c
+  abc
+  bam
+  bar
+  foo
+  j/k
+  l
+  m/m
+* master
+  master2
+  n
+  o/o
+  o/p
+  q
+  r
+EOF
+	test_cmp expected actual
+'
+
 mv .git/config .git/config-saved
 
 test_expect_success 'git branch -m q q2 without config should succeed' '
-- 
1.7.8.36.g69ee2

^ permalink raw reply related	[flat|nested] 66+ messages in thread

* [PATCH v7 08/10] status: add --column
  2012-02-28 11:58 ` [PATCH v7 00/10] " Nguyễn Thái Ngọc Duy
                     ` (6 preceding siblings ...)
  2012-02-28 11:58   ` [PATCH v7 07/10] branch: add --column Nguyễn Thái Ngọc Duy
@ 2012-02-28 11:58   ` Nguyễn Thái Ngọc Duy
  2012-02-28 11:58   ` [PATCH v7 09/10] column: support piping stdout to external git-column process Nguyễn Thái Ngọc Duy
                     ` (3 subsequent siblings)
  11 siblings, 0 replies; 66+ messages in thread
From: Nguyễn Thái Ngọc Duy @ 2012-02-28 11:58 UTC (permalink / raw)
  To: git; +Cc: Junio C Hamano, Ramsay Jones, Nguyễn Thái Ngọc Duy

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 Documentation/config.txt     |    4 ++++
 Documentation/git-status.txt |    7 +++++++
 Makefile                     |    2 +-
 builtin/commit.c             |    6 ++++++
 t/t7508-status.sh            |   24 ++++++++++++++++++++++++
 wt-status.c                  |   28 ++++++++++++++++++++++++++--
 wt-status.h                  |    1 +
 7 files changed, 69 insertions(+), 3 deletions(-)

diff --git a/Documentation/config.txt b/Documentation/config.txt
index c14db27..ebb210c 100644
--- a/Documentation/config.txt
+++ b/Documentation/config.txt
@@ -851,6 +851,10 @@ column.branch::
 	Specify whether to output branch listing in `git branch` in columns.
 	See `column.ui` for details.
 
+column.status::
+	Specify whether to output untracked files in `git status` in columns.
+	See `column.ui` for details.
+
 commit.status::
 	A boolean to enable/disable inclusion of status information in the
 	commit message template when using an editor to prepare the commit
diff --git a/Documentation/git-status.txt b/Documentation/git-status.txt
index 3d51717..2f87207 100644
--- a/Documentation/git-status.txt
+++ b/Documentation/git-status.txt
@@ -77,6 +77,13 @@ configuration variable documented in linkgit:git-config[1].
 	Terminate entries with NUL, instead of LF.  This implies
 	the `--porcelain` output format if no other format is given.
 
+--column[=<options>]::
+--no-column::
+	Display untracked files in columns. See configuration variable
+	column.status for option syntax.`--column` and `--no-column`
+	without options are equivalent to 'always' and 'never'
+	respectively.
+
 
 OUTPUT
 ------
diff --git a/Makefile b/Makefile
index 320d3f8..d9c5f00 100644
--- a/Makefile
+++ b/Makefile
@@ -2168,7 +2168,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
-builtin/branch.o column.o help.o pager.o: column.h
+builtin/branch.o builtin/commit.o 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/builtin/commit.c b/builtin/commit.c
index 3714582..b42b83f 100644
--- a/builtin/commit.c
+++ b/builtin/commit.c
@@ -27,6 +27,7 @@
 #include "quote.h"
 #include "submodule.h"
 #include "gpg-interface.h"
+#include "column.h"
 
 static const char * const builtin_commit_usage[] = {
 	"git commit [options] [--] <filepattern>...",
@@ -88,6 +89,7 @@ static int quiet, verbose, no_verify, allow_empty, dry_run, renew_authorship;
 static int no_post_rewrite, allow_empty_message;
 static char *untracked_files_arg, *force_date, *ignore_submodule_arg;
 static char *sign_commit;
+static unsigned int colopts;
 
 /*
  * The default commit message cleanup mode will remove the lines
@@ -1145,6 +1147,8 @@ static int git_status_config(const char *k, const char *v, void *cb)
 {
 	struct wt_status *s = cb;
 
+	if (!prefixcmp(k, "column."))
+		return git_column_config(k, v, "status", &colopts);
 	if (!strcmp(k, "status.submodulesummary")) {
 		int is_bool;
 		s->submodule_summary = git_config_bool_or_int(k, v, &is_bool);
@@ -1210,6 +1214,7 @@ int cmd_status(int argc, const char **argv, const char *prefix)
 		{ OPTION_STRING, 0, "ignore-submodules", &ignore_submodule_arg, "when",
 		  "ignore changes to submodules, optional when: all, dirty, untracked. (Default: all)",
 		  PARSE_OPT_OPTARG, NULL, (intptr_t)"all" },
+		OPT_COLUMN(0, "column", &colopts, "list untracked files in columns"),
 		OPT_END(),
 	};
 
@@ -1223,6 +1228,7 @@ int cmd_status(int argc, const char **argv, const char *prefix)
 	argc = parse_options(argc, argv, prefix,
 			     builtin_status_options,
 			     builtin_status_usage, 0);
+	s.colopts = colopts;
 
 	if (null_termination && status_format == STATUS_FORMAT_LONG)
 		status_format = STATUS_FORMAT_PORCELAIN;
diff --git a/t/t7508-status.sh b/t/t7508-status.sh
index fc57b13..8f5cfac 100755
--- a/t/t7508-status.sh
+++ b/t/t7508-status.sh
@@ -59,6 +59,30 @@ test_expect_success 'status (1)' '
 	test_i18ngrep "use \"git rm --cached <file>\.\.\.\" to unstage" output
 '
 
+test_expect_success 'status --column' '
+	COLUMNS=50 git status --column="column dense" >output &&
+	cat >expect <<\EOF &&
+# On branch master
+# Changes to be committed:
+#   (use "git reset HEAD <file>..." to unstage)
+#
+#	new file:   dir2/added
+#
+# Changes not staged for commit:
+#   (use "git add <file>..." to update what will be committed)
+#   (use "git checkout -- <file>..." to discard changes in working directory)
+#
+#	modified:   dir1/modified
+#
+# Untracked files:
+#   (use "git add <file>..." to include in what will be committed)
+#
+#	dir1/untracked dir2/untracked untracked
+#	dir2/modified  output
+EOF
+	test_cmp expect output
+'
+
 cat >expect <<\EOF
 # On branch master
 # Changes to be committed:
diff --git a/wt-status.c b/wt-status.c
index 9ffc535..eee059e 100644
--- a/wt-status.c
+++ b/wt-status.c
@@ -11,6 +11,7 @@
 #include "remote.h"
 #include "refs.h"
 #include "submodule.h"
+#include "column.h"
 
 static char default_wt_status_colors[][COLOR_MAXLEN] = {
 	GIT_COLOR_NORMAL, /* WT_STATUS_HEADER */
@@ -641,6 +642,8 @@ static void wt_status_print_other(struct wt_status *s,
 {
 	int i;
 	struct strbuf buf = STRBUF_INIT;
+	static struct string_list output = STRING_LIST_INIT_DUP;
+	struct column_options copts;
 
 	if (!l->nr)
 		return;
@@ -649,12 +652,33 @@ static void wt_status_print_other(struct wt_status *s,
 
 	for (i = 0; i < l->nr; i++) {
 		struct string_list_item *it;
+		const char *path;
 		it = &(l->items[i]);
+		path = quote_path(it->string, strlen(it->string),
+				  &buf, s->prefix);
+		if (s->colopts & COL_ENABLED) {
+			string_list_append(&output, path);
+			continue;
+		}
 		status_printf(s, color(WT_STATUS_HEADER, s), "\t");
 		status_printf_more(s, color(WT_STATUS_UNTRACKED, s),
-			"%s\n", quote_path(it->string, strlen(it->string),
-					    &buf, s->prefix));
+				   "%s\n", path);
 	}
+
+	strbuf_release(&buf);
+	if ((s->colopts & COL_ENABLED) == 0)
+		return;
+
+	strbuf_addf(&buf, "%s#\t%s",
+		    color(WT_STATUS_HEADER, s),
+		    color(WT_STATUS_UNTRACKED, s));
+	memset(&copts, 0, sizeof(copts));
+	copts.padding = 1;
+	copts.indent = buf.buf;
+	if (want_color(s->use_color))
+		copts.nl = GIT_COLOR_RESET "\n";
+	print_columns(&output, s->colopts, &copts);
+	string_list_clear(&output, 0);
 	strbuf_release(&buf);
 }
 
diff --git a/wt-status.h b/wt-status.h
index 682b4c8..6dd7207 100644
--- a/wt-status.h
+++ b/wt-status.h
@@ -56,6 +56,7 @@ struct wt_status {
 	enum untracked_status_type show_untracked_files;
 	const char *ignore_submodule_arg;
 	char color_palette[WT_STATUS_MAXSLOT][COLOR_MAXLEN];
+	int colopts;
 
 	/* These are computed during processing of the individual sections */
 	int commitable;
-- 
1.7.8.36.g69ee2

^ permalink raw reply related	[flat|nested] 66+ messages in thread

* [PATCH v7 09/10] column: support piping stdout to external git-column process
  2012-02-28 11:58 ` [PATCH v7 00/10] " Nguyễn Thái Ngọc Duy
                     ` (7 preceding siblings ...)
  2012-02-28 11:58   ` [PATCH v7 08/10] status: " Nguyễn Thái Ngọc Duy
@ 2012-02-28 11:58   ` Nguyễn Thái Ngọc Duy
  2012-02-28 11:58   ` [PATCH v7 10/10] tag: add --column Nguyễn Thái Ngọc Duy
                     ` (2 subsequent siblings)
  11 siblings, 0 replies; 66+ messages in thread
From: Nguyễn Thái Ngọc Duy @ 2012-02-28 11:58 UTC (permalink / raw)
  To: git; +Cc: Junio C Hamano, Ramsay Jones, Nguyễn Thái Ngọc Duy

For too complicated output handling, it'd be easier to just spawn
git-column and redirect stdout to it. This patch provides helpers
to do that.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 column.c |   69 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 column.h |    3 ++
 2 files changed, 72 insertions(+), 0 deletions(-)

diff --git a/column.c b/column.c
index 67bfd06..103d98c 100644
--- a/column.c
+++ b/column.c
@@ -2,6 +2,7 @@
 #include "column.h"
 #include "string-list.h"
 #include "parse-options.h"
+#include "run-command.h"
 #include "color.h"
 #include "utf8.h"
 
@@ -405,3 +406,71 @@ int parseopt_column_callback(const struct option *opt,
 	*mode |= COL_ENABLED | COL_ENABLED_SET;
 	return 0;
 }
+
+static int fd_out = -1;
+static struct child_process column_process;
+
+int run_column_filter(int colopts, const struct column_options *opts)
+{
+	const char *av[10];
+	int ret, ac = 0;
+	struct strbuf sb_colopt  = STRBUF_INIT;
+	struct strbuf sb_width   = STRBUF_INIT;
+	struct strbuf sb_padding = STRBUF_INIT;
+
+	if (fd_out != -1)
+		return -1;
+
+	av[ac++] = "column";
+	strbuf_addf(&sb_colopt, "--rawmode=%d", colopts);
+	av[ac++] = sb_colopt.buf;
+	if (opts->width) {
+		strbuf_addf(&sb_width, "--width=%d", opts->width);
+		av[ac++] = sb_width.buf;
+	}
+	if (opts->indent) {
+		av[ac++] = "--indent";
+		av[ac++] = opts->indent;
+	}
+	if (opts->padding) {
+		strbuf_addf(&sb_padding, "--padding=%d", opts->padding);
+		av[ac++] = sb_padding.buf;
+	}
+	av[ac] = NULL;
+
+	fflush(stdout);
+	memset(&column_process, 0, sizeof(column_process));
+	column_process.in = -1;
+	column_process.out = dup(1);
+	column_process.git_cmd = 1;
+	column_process.argv = av;
+
+	ret = start_command(&column_process);
+
+	strbuf_release(&sb_colopt);
+	strbuf_release(&sb_width);
+	strbuf_release(&sb_padding);
+
+	if (ret)
+		return -2;
+
+	fd_out = dup(1);
+	close(1);
+	dup2(column_process.in, 1);
+	close(column_process.in);
+	return 0;
+}
+
+int stop_column_filter(void)
+{
+	if (fd_out == -1)
+		return -1;
+
+	fflush(stdout);
+	close(1);
+	finish_command(&column_process);
+	dup2(fd_out, 1);
+	close(fd_out);
+	fd_out = -1;
+	return 0;
+}
diff --git a/column.h b/column.h
index 43528da..cb81c8a 100644
--- a/column.h
+++ b/column.h
@@ -34,4 +34,7 @@ struct option;
 extern int parseopt_column_callback(const struct option *opt,
 				    const char *arg, int unset);
 
+extern int run_column_filter(int colopts, const struct column_options *);
+extern int stop_column_filter();
+
 #endif
-- 
1.7.8.36.g69ee2

^ permalink raw reply related	[flat|nested] 66+ messages in thread

* [PATCH v7 10/10] tag: add --column
  2012-02-28 11:58 ` [PATCH v7 00/10] " Nguyễn Thái Ngọc Duy
                     ` (8 preceding siblings ...)
  2012-02-28 11:58   ` [PATCH v7 09/10] column: support piping stdout to external git-column process Nguyễn Thái Ngọc Duy
@ 2012-02-28 11:58   ` Nguyễn Thái Ngọc Duy
  2012-03-02 11:25   ` [PATCH v7 00/10] Column display Thomas Rast
  2012-03-13 12:09   ` [PATCH v7 00/9] " Nguyễn Thái Ngọc Duy
  11 siblings, 0 replies; 66+ messages in thread
From: Nguyễn Thái Ngọc Duy @ 2012-02-28 11:58 UTC (permalink / raw)
  To: git; +Cc: Junio C Hamano, Ramsay Jones, Nguyễn Thái Ngọc Duy

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 Documentation/config.txt  |    4 ++++
 Documentation/git-tag.txt |    9 +++++++++
 Makefile                  |    2 +-
 builtin/tag.c             |   26 +++++++++++++++++++++++---
 t/t7004-tag.sh            |   44 ++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 81 insertions(+), 4 deletions(-)

diff --git a/Documentation/config.txt b/Documentation/config.txt
index ebb210c..145336a 100644
--- a/Documentation/config.txt
+++ b/Documentation/config.txt
@@ -855,6 +855,10 @@ column.status::
 	Specify whether to output untracked files in `git status` in columns.
 	See `column.ui` for details.
 
+column.tag::
+	Specify whether to output tag listing in `git tag` in columns.
+	See `column.ui` for details.
+
 commit.status::
 	A boolean to enable/disable inclusion of status information in the
 	commit message template when using an editor to prepare the commit
diff --git a/Documentation/git-tag.txt b/Documentation/git-tag.txt
index 8d32b9a..e36a7c3 100644
--- a/Documentation/git-tag.txt
+++ b/Documentation/git-tag.txt
@@ -13,6 +13,7 @@ SYNOPSIS
 	<tagname> [<commit> | <object>]
 'git tag' -d <tagname>...
 'git tag' [-n[<num>]] -l [--contains <commit>] [--points-at <object>]
+	[--column[=<options>] | --no-column] [<pattern>...]
 	[<pattern>...]
 'git tag' -v <tagname>...
 
@@ -84,6 +85,14 @@ OPTIONS
 	using fnmatch(3)).  Multiple patterns may be given; if any of
 	them matches, the tag is shown.
 
+--column[=<options>]::
+--no-column::
+	Display tag listing in columns. See configuration variable
+	column.tag for option syntax.`--column` and `--no-column`
+	without options are equivalent to 'always' and 'never' respectively.
++
+This option is only applicable when listing tags without annotation lines.
+
 --contains <commit>::
 	Only list tags which contain the specified commit.
 
diff --git a/Makefile b/Makefile
index d9c5f00..65fc6b9 100644
--- a/Makefile
+++ b/Makefile
@@ -2168,7 +2168,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
-builtin/branch.o builtin/commit.o column.o help.o pager.o: column.h
+builtin/branch.o builtin/commit.o builtin/tag.o 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/builtin/tag.c b/builtin/tag.c
index fe7e5e5..e99cbff 100644
--- a/builtin/tag.c
+++ b/builtin/tag.c
@@ -16,6 +16,7 @@
 #include "revision.h"
 #include "gpg-interface.h"
 #include "sha1-array.h"
+#include "column.h"
 
 static const char * const git_tag_usage[] = {
 	"git tag [-a|-s|-u <key-id>] [-f] [-m <msg>|-F <file>] <tagname> [<head>]",
@@ -33,6 +34,7 @@ struct tag_filter {
 };
 
 static struct sha1_array points_at;
+static unsigned int colopts;
 
 static int match_pattern(const char **patterns, const char *ref)
 {
@@ -263,6 +265,8 @@ static int git_tag_config(const char *var, const char *value, void *cb)
 	int status = git_gpg_config(var, value, cb);
 	if (status)
 		return status;
+	if (!prefixcmp(var, "column."))
+		return git_column_config(var, value, "tag", &colopts);
 	return git_default_config(var, value, cb);
 }
 
@@ -459,6 +463,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
 		OPT_STRING('u', "local-user", &keyid, "key-id",
 					"use another key to sign the tag"),
 		OPT__FORCE(&force, "replace the tag if exists"),
+		OPT_COLUMN(0, "column", &colopts, "show tag list in columns"),
 
 		OPT_GROUP("Tag listing options"),
 		{
@@ -495,9 +500,24 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
 
 	if (list + delete + verify > 1)
 		usage_with_options(git_tag_usage, options);
-	if (list)
-		return list_tags(argv, lines == -1 ? 0 : lines,
-				 with_commit);
+	if (list && lines != -1) {
+		if (explicitly_enable_column(colopts))
+			die(_("--column and -n are incompatible"));
+		colopts = 0;
+	}
+	if (list) {
+		int ret;
+		if (colopts & COL_ENABLED) {
+			struct column_options copts;
+			memset(&copts, 0, sizeof(copts));
+			copts.padding = 2;
+			run_column_filter(colopts, &copts);
+		}
+		ret = list_tags(argv, lines == -1 ? 0 : lines, with_commit);
+		if (colopts & COL_ENABLED)
+			stop_column_filter();
+		return ret;
+	}
 	if (lines != -1)
 		die(_("-n option is only allowed with -l."));
 	if (with_commit)
diff --git a/t/t7004-tag.sh b/t/t7004-tag.sh
index f8c247a..5189446 100755
--- a/t/t7004-tag.sh
+++ b/t/t7004-tag.sh
@@ -263,6 +263,50 @@ test_expect_success 'tag -l can accept multiple patterns' '
 	test_cmp expect actual
 '
 
+test_expect_success 'listing tags in column' '
+	COLUMNS=40 git tag -l --column=row >actual &&
+	cat >expected <<\EOF &&
+a1      aa1     cba     t210    t211
+v0.2.1  v1.0    v1.0.1  v1.1.3
+EOF
+	test_cmp expected actual
+'
+
+test_expect_success 'listing tags in column with column.*' '
+	git config column.tag row &&
+	git config column.ui dense &&
+	COLUMNS=40 git tag -l >actual &&
+	git config --unset column.ui &&
+	git config --unset column.tag &&
+	cat >expected <<\EOF &&
+a1      aa1   cba     t210    t211
+v0.2.1  v1.0  v1.0.1  v1.1.3
+EOF
+	test_cmp expected actual
+'
+
+test_expect_success 'listing tag with -n --column should fail' '
+	test_must_fail git tag --column -n
+'
+
+test_expect_success 'listing tags -n in column with column.ui ignored' '
+	git config column.ui "row dense" &&
+	COLUMNS=40 git tag -l -n >actual &&
+	git config --unset column.ui &&
+	cat >expected <<\EOF &&
+a1              Foo
+aa1             Foo
+cba             Foo
+t210            Foo
+t211            Foo
+v0.2.1          Foo
+v1.0            Foo
+v1.0.1          Foo
+v1.1.3          Foo
+EOF
+	test_cmp expected actual
+'
+
 # creating and verifying lightweight tags:
 
 test_expect_success \
-- 
1.7.8.36.g69ee2

^ permalink raw reply related	[flat|nested] 66+ messages in thread

* Re: [PATCH v7 01/10] Add git-column for columnar display
  2012-02-28 11:58   ` [PATCH v7 01/10] Add git-column for columnar display Nguyễn Thái Ngọc Duy
@ 2012-02-28 18:10     ` Junio C Hamano
  2012-03-02 12:36       ` Nguyen Thai Ngoc Duy
  0 siblings, 1 reply; 66+ messages in thread
From: Junio C Hamano @ 2012-02-28 18:10 UTC (permalink / raw)
  To: Nguyễn Thái Ngọc Duy; +Cc: git, Ramsay Jones

Nguyễn Thái Ngọc Duy <pclouds@gmail.com> writes:

> diff --git a/Documentation/git-column.txt b/Documentation/git-column.txt
> new file mode 100644
> index 0000000..508b85f
> --- /dev/null
> +++ b/Documentation/git-column.txt
> @@ -0,0 +1,49 @@
> +git-column(1)
> +=============
> +
> +NAME
> +----
> +git-column - Display data in columns
> +
> +SYNOPSIS
> +--------
> +[verse]
> +'git column' [--mode=<mode> | --rawmode=<n>] [--width=<width>]

Please spell this "--raw-mode".

> diff --git a/builtin/column.c b/builtin/column.c
> new file mode 100644
> index 0000000..3b0f74e
> --- /dev/null
> +++ b/builtin/column.c
> @@ -0,0 +1,41 @@
> +#include "builtin.h"
> +#include "cache.h"
> +#include "strbuf.h"
> +#include "parse-options.h"
> +#include "string-list.h"
> +#include "column.h"
> +
> +static const char * const builtin_column_usage[] = {
> +	"git column [options]",
> +	NULL
> +};
> +static unsigned int colopts;
> +
> +int cmd_column(int argc, const char **argv, const char *prefix)
> +{
> +	struct string_list list = STRING_LIST_INIT_DUP;
> +	struct strbuf sb = STRBUF_INIT;
> +	struct column_options copts;
> +	struct option options[] = {
> +		OPT_COLUMN(0, "mode", &colopts, "layout to use"),
> +		OPT_INTEGER(0, "rawmode", &colopts, "layout to use"),
> +		OPT_INTEGER(0, "width", &copts.width, "Maximum width"),
> +		OPT_STRING(0, "indent", &copts.indent, "string", "Padding space on left border"),
> +		OPT_INTEGER(0, "nl", &copts.nl, "Padding space on right border"),
> +		OPT_INTEGER(0, "padding", &copts.padding, "Padding space between columns"),
> +		OPT_END()
> +	};
> +
> +	memset(&copts, 0, sizeof(copts));
> +	copts.width = term_columns();
> +	copts.padding = 1;
> +	argc = parse_options(argc, argv, "", options, builtin_column_usage, 0);

Curious. The usual pattern is to set up the built-in default, call
git_config() to override it with the configured values, and then call
parse_options() to override at the runtime.  I see configuration callback
defined in column.c but no call to git_config() here?

> +	if (argc)
> +		usage_with_options(builtin_column_usage, options);
> +
> +	while (!strbuf_getline(&sb, stdin, '\n'))
> +		string_list_append(&list, sb.buf);
> +
> +	print_columns(&list, colopts, &copts);
> +	return 0;
> +}
> diff --git a/column.c b/column.c
> new file mode 100644
> index 0000000..d61da81
> --- /dev/null
> +++ b/column.c
> @@ -0,0 +1,170 @@
> +#include "cache.h"
> +#include "column.h"
> +#include "string-list.h"
> +#include "parse-options.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)) {

Curious why this is "1".  If your terminal is only 2 columns wide, you
wouldn't be able to show your list items in two columns as you would want
to have an inter-column gap, no?

> +		display_plain(list, indent, nl);
> +		return;
> +	}
> +	die("BUG: invalid mode %d", MODE(mode));
> +}
> +
> +struct colopt {
> +	enum {
> +		ENABLE,
> +		MODE,
> +		OPTION
> +	} type;
> +	const char *name;
> +	int value;
> +};
> +
> +/*
> + * Set COL_ENABLED and COL_ENABLED_SET. If 'set' is -1, check if
> + * stdout is tty.
> + */
> +static int set_enable_bit(unsigned int *mode, int set, int stdout_is_tty)
> +{

Somehow it looks to me that this is setting the ENABLED bit, not enable
bit.

> +	if (set < 0) {	/* auto */
> +		if (stdout_is_tty < 0)
> +			stdout_is_tty = isatty(1);
> +		set = stdout_is_tty || (pager_in_use() && pager_use_color);

Why does this have anything to do with the use of color?

> +	}
> +	if (set)
> +		*mode = *mode | COL_ENABLED | COL_ENABLED_SET;
> +	else
> +		*mode = (*mode & ~COL_ENABLED) | COL_ENABLED_SET;
> +	return 0;
> +}

OK, so we record the desired value (either COL_ENABLED or not) and the
fact that a call to set_enable_bit() function set it.  Which implies that
this function must be called from only one codepath (either setting from
the configuration mechanism, or by parsing the command line option) but
not both.  I guess this is only called from configuration codepath?

> +/*
> + * Set COL_MODE_*. mode is intially copied from column.ui. If
> + * COL_ENABLED_SET is not set, then neither 'always', 'never' nor
> + * 'auto' has been used. Default to 'always'.
> + */
> +static int set_mode(unsigned int *mode, unsigned int value)
> +{
> +	*mode = (*mode & ~COL_MODE) | value;
> +	if (!(*mode & COL_ENABLED_SET))
> +		*mode |= COL_ENABLED | COL_ENABLED_SET;
> +
> +	return 0;
> +}

I am *imagining* that "*mode" begins with some compiled-in default, then
git_config() will read from column.* variables and set "*mode" but not in
this codepath, and by the time command line option triggers this codepath,
we should have seen everything the config wants to tell us, so if the
config did not say anything (i.e. COL_ENABLED_SET is not set yet), we
force enable the feature (the user wanting it to operate in some mode is
an indication enough that the user wants to enable the machinery as a
whole). So this function is designed to be called from command line option
parsing, but never from the configuration parsing.

But the parse_option() function below calls this, which would mean it is
also for command line option parsing and not configuration parsing.  The
same function however calls set_enable_bit() we saw earlier that can only
be called from configuration codepath.  What is going on?

I am afraid that we do not have enough to judge if this is sane in this
patch, as there is no support for column.ui at this stage.  Perhaps the
series is not structured well and has things that are not yet relevant in
early part of it.  Sigh.

The remainder of the patch unreviewed.

^ permalink raw reply	[flat|nested] 66+ messages in thread

* Re: [PATCH v7 02/10] Stop starting pager recursively
  2012-02-28 11:58   ` [PATCH v7 02/10] Stop starting pager recursively Nguyễn Thái Ngọc Duy
@ 2012-02-28 18:13     ` Junio C Hamano
  2012-02-28 19:10       ` Junio C Hamano
  0 siblings, 1 reply; 66+ messages in thread
From: Junio C Hamano @ 2012-02-28 18:13 UTC (permalink / raw)
  To: Nguyễn Thái Ngọc Duy; +Cc: git, Ramsay Jones

Nguyễn Thái Ngọc Duy <pclouds@gmail.com> writes:

> git-column can be used as a pager for other git commands, something
> like this:
>
>     GIT_PAGER="git -p column --mode='dense color'" git -p branch
>
> The problem with this is that "git -p column" also has $GIT_PAGER
> set so the pager runs itself again as a pager, then again and again.
>
> Stop this.

A natural question that may come after reading only the above is if "git
column" is the only one that has this problem.  In other words, is the
undesirable behaviour you observed caused by a bug in setup_pager() that
needs to be fixed, or should it be fixed in "git column"?

> Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
> Signed-off-by: Junio C Hamano <gitster@pobox.com>
> ---
>  pager.c |    2 +-
>  1 files changed, 1 insertions(+), 1 deletions(-)
>
> diff --git a/pager.c b/pager.c
> index 05584de..4dcb08d 100644
> --- a/pager.c
> +++ b/pager.c
> @@ -73,7 +73,7 @@ void setup_pager(void)
>  {
>  	const char *pager = git_pager(isatty(1));
>  
> -	if (!pager)
> +	if (!pager || pager_in_use())
>  		return;
>  
>  	/*

^ permalink raw reply	[flat|nested] 66+ messages in thread

* Re: [PATCH v7 03/10] column: add columnar layout
  2012-02-28 11:58   ` [PATCH v7 03/10] column: add columnar layout Nguyễn Thái Ngọc Duy
@ 2012-02-28 18:22     ` Junio C Hamano
  0 siblings, 0 replies; 66+ messages in thread
From: Junio C Hamano @ 2012-02-28 18:22 UTC (permalink / raw)
  To: Nguyễn Thái Ngọc Duy; +Cc: git, Ramsay Jones

Nguyễn Thái Ngọc Duy <pclouds@gmail.com> writes:

> COL_MODE_COLUMN and COL_MODE_ROW fill column by column (or row by row
> respectively), given the terminal width and how many space between
> columns.
>
> Strings are supposed to be in UTF-8. If strings contain ANSI escape
> strings, COL_ANSI must be specified for correct length calculation.

Hrm, is it too heavyweight to autodetect and relieve the caller from the
burden of passing COL_ANSI?

Perhaps if you update utf8_strwidth() so that it takes <ptr, len> input
not NUL terminated string, you can do item_length like this, no?

	cp = s;
	width = 0;
	while (1) {
		ep = strstr(cp, "\033[");
		if (!ep) {
			width += utf8_strwidth(cp);
                        break;
		}
                utf8_strwidth_counted(cp, ep - cp);
		... scan ep to skip the "\033[...;X"
		cp = ep;
	}

^ permalink raw reply	[flat|nested] 66+ messages in thread

* Re: [PATCH v7 04/10] column: add dense layout support
  2012-02-28 11:58   ` [PATCH v7 04/10] column: add dense layout support Nguyễn Thái Ngọc Duy
@ 2012-02-28 18:27     ` Junio C Hamano
  2012-03-02 12:47       ` Nguyen Thai Ngoc Duy
  0 siblings, 1 reply; 66+ messages in thread
From: Junio C Hamano @ 2012-02-28 18:27 UTC (permalink / raw)
  To: Nguyễn Thái Ngọc Duy; +Cc: git, Ramsay Jones

Nguyễn Thái Ngọc Duy <pclouds@gmail.com> writes:

> Normally, all items will be gone through once. The largest cell will set
> column width for all columns. With this strategy, one long item will
> stretch out all columns, wasting space in between them.
>
> With COL_DENSE enabled, it shrinks all columns to minimum, then attempts
> to push the last row's cells over to the next column with hope that
> everything still fits even there's one row less. The process is repeated
> until the new layout cannot fit in given width anymore, or there's only
> one row left (perfect!).

As you have given 4 bits for COL_MODE in the previous patch, I expected
that this will be one of the mode that you can use, e.g. column-first dense,
or row-first no-dense, with two more bits for operating modes.  Not calling
it COL_MODE_DENSE and assigning a "are we dense of not?" bit out of COL_MODE
bits feels wrong.

^ permalink raw reply	[flat|nested] 66+ messages in thread

* Re: [PATCH v7 05/10] column: add column.ui for default column output settings
  2012-02-28 11:58   ` [PATCH v7 05/10] column: add column.ui for default column output settings Nguyễn Thái Ngọc Duy
@ 2012-02-28 18:44     ` Junio C Hamano
  0 siblings, 0 replies; 66+ messages in thread
From: Junio C Hamano @ 2012-02-28 18:44 UTC (permalink / raw)
  To: Nguyễn Thái Ngọc Duy; +Cc: git, Ramsay Jones

Nguyễn Thái Ngọc Duy <pclouds@gmail.com> writes:

> +static int column_config(const char *var, const char *value,
> +			 const char *key, unsigned int *colopts)
> +{
> +	if (git_config_column(colopts, value, -1))

Are we sure nobody has put us on pager at this point, so that you can tell
git_config_column() that it is OK to use isatty(1) to figure it out, or we
could already be on pager (i.e. pager_in_use() is true) in which case we
know we are interactive and should behave the same way as writing to a
terminal?

> +		return error("invalid %s mode %s", key, value);
> +	return 0;
> +}
> +
> +int git_column_config(const char *var, const char *value,
> +		      const char *command, unsigned int *colopts)
> +{
> +	if (!strcmp(var, "column.ui"))
> +		return column_config(var, value, "column.ui", colopts);
> +
> +	if (command) {
> +		struct strbuf sb = STRBUF_INIT;
> +		int ret = 0;
> +		strbuf_addf(&sb, "column.%s", command);
> +		if (!strcmp(var, sb.buf))
> +			ret = column_config(var, value, sb.buf, colopts);
> +		strbuf_release(&sb);
> +		return ret;
> +	}

This feels wrong. Depending on the order column.ui and column.frotz appear
in the configuration file, asking for "git column --command=frotz" would
yield random results, no?

Shouldn't the flow of logic be more like:

	git_config(git_column_config);
        -> git_column_config() is called for column.ui and column.frotz
           in no specified order; keep two *char variables to store the
           string value given from configuration
	if (kept value from column.frotz is missing)
		git_config_column(..., kept value from column.ui, ...);
	else
		git_config_column(..., kept value from column.frotz, ...); 

> diff --git a/column.h b/column.h
> index eb03c6c..43528da 100644
> --- a/column.h
> +++ b/column.h
> @@ -27,6 +27,8 @@ extern void print_columns(const struct string_list *list,
>  			  struct column_options *opts);
>  extern int git_config_column(unsigned int *mode, const char *value,
>  			     int stdout_is_tty);
> +extern int git_column_config(const char *var, const char *value,
> +			     const char *command, unsigned int *colopts);

Also please rename git_config_column() in the earlier patch, perhaps like
"parse_column_config_string()" or something more sensible, to avoid
confusion.

^ permalink raw reply	[flat|nested] 66+ messages in thread

* Re: [PATCH v7 02/10] Stop starting pager recursively
  2012-02-28 18:13     ` Junio C Hamano
@ 2012-02-28 19:10       ` Junio C Hamano
  2012-02-29  1:54         ` Nguyen Thai Ngoc Duy
  0 siblings, 1 reply; 66+ messages in thread
From: Junio C Hamano @ 2012-02-28 19:10 UTC (permalink / raw)
  To: Nguyễn Thái Ngọc Duy; +Cc: git, Ramsay Jones

Junio C Hamano <gitster@pobox.com> writes:

> Nguyễn Thái Ngọc Duy <pclouds@gmail.com> writes:
>
>> git-column can be used as a pager for other git commands, something
>> like this:
>>
>>     GIT_PAGER="git -p column --mode='dense color'" git -p branch
>>
>> The problem with this is that "git -p column" also has $GIT_PAGER
>> set so the pager runs itself again as a pager, then again and again.
>>
>> Stop this.
>
> A natural question that may come after reading only the above is if "git
> column" is the only one that has this problem.  In other words, is the
> undesirable behaviour you observed caused by a bug in setup_pager() that
> needs to be fixed, or should it be fixed in "git column"?

Put another way, if there is another git command X that can be used as a
filter to the output of a git command Y, do you suffer from the same issue
to when you abuse the GIT_PAGER mechanism to pipe the output from Y to X?
That is a sure sign that the pager mechanism needs improvement (obviously,
an alternative answer could be "don't do that then", though).

For example, shortlog is designed to be X for Y=log, i.e.

	$ git log v1.0.0.. | git shortlog

is a perfectly valid way to use the command.  I could imagine that this
patch may improve the situation if you abuse GIT_PAGER mechanism to
implement the above pipeline, i.e.

	$ GIT_PAGER="git -p shortlog" git log v1.0.0..

Although I never tried it.

^ permalink raw reply	[flat|nested] 66+ messages in thread

* Re: [PATCH v7 02/10] Stop starting pager recursively
  2012-02-28 19:10       ` Junio C Hamano
@ 2012-02-29  1:54         ` Nguyen Thai Ngoc Duy
  2012-02-29  3:37           ` Junio C Hamano
  0 siblings, 1 reply; 66+ messages in thread
From: Nguyen Thai Ngoc Duy @ 2012-02-29  1:54 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git, Ramsay Jones

2012/2/29 Junio C Hamano <gitster@pobox.com>:
>> A natural question that may come after reading only the above is if "git
>> column" is the only one that has this problem.  In other words, is the
>> undesirable behaviour you observed caused by a bug in setup_pager() that
>> needs to be fixed, or should it be fixed in "git column"?
>
> Put another way, if there is another git command X that can be used as a
> filter to the output of a git command Y, do you suffer from the same issue
> to when you abuse the GIT_PAGER mechanism to pipe the output from Y to X?
> That is a sure sign that the pager mechanism needs improvement (obviously,
> an alternative answer could be "don't do that then", though).
>
> For example, shortlog is designed to be X for Y=log, i.e.
>
>        $ git log v1.0.0.. | git shortlog
>
> is a perfectly valid way to use the command.  I could imagine that this
> patch may improve the situation if you abuse GIT_PAGER mechanism to
> implement the above pipeline, i.e.
>
>        $ GIT_PAGER="git -p shortlog" git log v1.0.0..
>
> Although I never tried it.

It does improve that situation, or at least it stops git from forking
frantically.
-- 
Duy

^ permalink raw reply	[flat|nested] 66+ messages in thread

* Re: [PATCH v7 02/10] Stop starting pager recursively
  2012-02-29  1:54         ` Nguyen Thai Ngoc Duy
@ 2012-02-29  3:37           ` Junio C Hamano
  2012-02-29  3:40             ` Nguyen Thai Ngoc Duy
  0 siblings, 1 reply; 66+ messages in thread
From: Junio C Hamano @ 2012-02-29  3:37 UTC (permalink / raw)
  To: Nguyen Thai Ngoc Duy; +Cc: git, Ramsay Jones

Nguyen Thai Ngoc Duy <pclouds@gmail.com> writes:

> It does improve that situation, or at least it stops git from forking
> frantically.

That "at least" sounds suspicious ;-) Do you mean that there is other
breakages in that mode of operation?

^ permalink raw reply	[flat|nested] 66+ messages in thread

* Re: [PATCH v7 02/10] Stop starting pager recursively
  2012-02-29  3:37           ` Junio C Hamano
@ 2012-02-29  3:40             ` Nguyen Thai Ngoc Duy
  2012-02-29  4:51               ` Junio C Hamano
  0 siblings, 1 reply; 66+ messages in thread
From: Nguyen Thai Ngoc Duy @ 2012-02-29  3:40 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git, Ramsay Jones

On Wed, Feb 29, 2012 at 10:37 AM, Junio C Hamano <gitster@pobox.com> wrote:
> Nguyen Thai Ngoc Duy <pclouds@gmail.com> writes:
>
>> It does improve that situation, or at least it stops git from forking
>> frantically.
>
> That "at least" sounds suspicious ;-) Do you mean that there is other
> breakages in that mode of operation?

Well, "shortlog -p" did not run $PAGER (i.e. less). I haven't had time
to dig further.
-- 
Duy

^ permalink raw reply	[flat|nested] 66+ messages in thread

* Re: [PATCH v7 02/10] Stop starting pager recursively
  2012-02-29  3:40             ` Nguyen Thai Ngoc Duy
@ 2012-02-29  4:51               ` Junio C Hamano
  0 siblings, 0 replies; 66+ messages in thread
From: Junio C Hamano @ 2012-02-29  4:51 UTC (permalink / raw)
  To: Nguyen Thai Ngoc Duy; +Cc: git, Ramsay Jones

Nguyen Thai Ngoc Duy <pclouds@gmail.com> writes:

> On Wed, Feb 29, 2012 at 10:37 AM, Junio C Hamano <gitster@pobox.com> wrote:
>> Nguyen Thai Ngoc Duy <pclouds@gmail.com> writes:
>>
>>> It does improve that situation, or at least it stops git from forking
>>> frantically.
>>
>> That "at least" sounds suspicious ;-) Do you mean that there is other
>> breakages in that mode of operation?
>
> Well, "shortlog -p" did not run $PAGER (i.e. less). I haven't had time
> to dig further.

Ok. for that matter,

    $ GIT_PAGER="git -p column" git tag --list

does not seem to use pager, either, so that may not be limited to
shortlog, but in any case, I can see how the current code without your
patch can fall into a fork loop, and your patch is an improvement, even if
it may not yet be the whole solution.

^ permalink raw reply	[flat|nested] 66+ messages in thread

* Re: [PATCH v7 00/10] Column display
  2012-02-28 11:58 ` [PATCH v7 00/10] " Nguyễn Thái Ngọc Duy
                     ` (9 preceding siblings ...)
  2012-02-28 11:58   ` [PATCH v7 10/10] tag: add --column Nguyễn Thái Ngọc Duy
@ 2012-03-02 11:25   ` Thomas Rast
  2012-03-11  7:02     ` Nguyen Thai Ngoc Duy
  2012-03-13 12:09   ` [PATCH v7 00/9] " Nguyễn Thái Ngọc Duy
  11 siblings, 1 reply; 66+ messages in thread
From: Thomas Rast @ 2012-03-02 11:25 UTC (permalink / raw)
  To: Nguyễn Thái Ngọc Duy; +Cc: git, Junio C Hamano, Ramsay Jones

Nguyễn Thái Ngọc Duy <pclouds@gmail.com> writes:

> Nguyễn Thái Ngọc Duy (10):
>   Add git-column for columnar display
>   Stop starting pager recursively
>   column: add columnar layout
>   column: add dense layout support
>   column: add column.ui for default column output settings
>   help: reuse print_columns() for help -a
>   branch: add --column
>   status: add --column
>   column: support piping stdout to external git-column process
>   tag: add --column

I ran valgrind over the tests in pu, and I'm seeing these errors
presumably coming from this series:

t3200-branch.out:==340== Invalid read of size 4
t3200-branch.out:==340==    at 0x48E676: shrink_columns (column.c:101)
t3200-branch.out:==340==    by 0x48EB76: display_table (column.c:200)
t3200-branch.out:==340==    by 0x48ED1B: print_columns (column.c:241)
t3200-branch.out:==340==    by 0x41967F: cmd_branch (branch.c:793)
t3200-branch.out:==340==    by 0x40550B: run_builtin (git.c:308)
t3200-branch.out:==340==    by 0x4056A6: handle_internal_command (git.c:468)
t3200-branch.out:==340==    by 0x4057C0: run_argv (git.c:514)
t3200-branch.out:==340==    by 0x40594D: main (git.c:589)
t3200-branch.out:==340==  Address 0x57023ac is 0 bytes after a block of size 60 alloc'd
t3200-branch.out:==340==    at 0x4C226FA: malloc (vg_replace_malloc.c:263)
t3200-branch.out:==340==    by 0x521A11: xmalloc (wrapper.c:35)
t3200-branch.out:==340==    by 0x48EAE8: display_table (column.c:193)
t3200-branch.out:==340==    by 0x48ED1B: print_columns (column.c:241)
t3200-branch.out:==340==    by 0x41967F: cmd_branch (branch.c:793)
t3200-branch.out:==340==    by 0x40550B: run_builtin (git.c:308)
t3200-branch.out:==340==    by 0x4056A6: handle_internal_command (git.c:468)
t3200-branch.out:==340==    by 0x4057C0: run_argv (git.c:514)
t3200-branch.out:==340==    by 0x40594D: main (git.c:589)

and very similar errors in t7004-tag.out, t9002-column.out and
t7508-status.out.  Can you look into it?

-- 
Thomas Rast
trast@{inf,student}.ethz.ch

^ permalink raw reply	[flat|nested] 66+ messages in thread

* Re: [PATCH v7 01/10] Add git-column for columnar display
  2012-02-28 18:10     ` Junio C Hamano
@ 2012-03-02 12:36       ` Nguyen Thai Ngoc Duy
  0 siblings, 0 replies; 66+ messages in thread
From: Nguyen Thai Ngoc Duy @ 2012-03-02 12:36 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git, Ramsay Jones

2012/2/29 Junio C Hamano <gitster@pobox.com>:
>> +     memset(&copts, 0, sizeof(copts));
>> +     copts.width = term_columns();
>> +     copts.padding = 1;
>> +     argc = parse_options(argc, argv, "", options, builtin_column_usage, 0);
>
> Curious. The usual pattern is to set up the built-in default, call
> git_config() to override it with the configured values, and then call
> parse_options() to override at the runtime.  I see configuration callback
> defined in column.c but no call to git_config() here?

History reason. It was test-column and I did not care much whether it
respected config. Will fix.

>> +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)) {
>
> Curious why this is "1".  If your terminal is only 2 columns wide, you
> wouldn't be able to show your list items in two columns as you would want
> to have an inter-column gap, no?

Yeah, should drop that "width <= 1". Other layout functions should be
able to deal with too narrow width anyway.


>> +     if (set < 0) {  /* auto */
>> +             if (stdout_is_tty < 0)
>> +                     stdout_is_tty = isatty(1);
>> +             set = stdout_is_tty || (pager_in_use() && pager_use_color);
>
> Why does this have anything to do with the use of color?

Hm.. no it does not have to.

>> +     }
>> +     if (set)
>> +             *mode = *mode | COL_ENABLED | COL_ENABLED_SET;
>> +     else
>> +             *mode = (*mode & ~COL_ENABLED) | COL_ENABLED_SET;
>> +     return 0;
>> +}
>
> OK, so we record the desired value (either COL_ENABLED or not) and the
> fact that a call to set_enable_bit() function set it.  Which implies that
> this function must be called from only one codepath (either setting from
> the configuration mechanism, or by parsing the command line option) but
> not both.  I guess this is only called from configuration codepath?

Both share the same code path. COL_ENABLED_SET is probably badly
named, perhaps COL_ENABLED_VALID is better. It's there to deal
distinguish "never" and not explicitly stating any of
never/auto/always. In both cases, COL_ENABLED is 0. COL_ENABLED_SET is
set in the former, but not in the latter. I depend on that to make
default behavior "always". Now thinking again, it would make more
sense to set default "never", and drop COL_ENABLED_SET.

> I am afraid that we do not have enough to judge if this is sane in this
> patch, as there is no support for column.ui at this stage.  Perhaps the
> series is not structured well and has things that are not yet relevant in
> early part of it.  Sigh.

If it's not clear whether it's sane, it's more likely not. I'll
re-read the series again and redo. One good thing about git
development is only tidy things can become part of upstream, which
makes archaelogy a pleasant experience.
-- 
Duy

^ permalink raw reply	[flat|nested] 66+ messages in thread

* Re: [PATCH v7 04/10] column: add dense layout support
  2012-02-28 18:27     ` Junio C Hamano
@ 2012-03-02 12:47       ` Nguyen Thai Ngoc Duy
  2012-03-02 17:37         ` Junio C Hamano
  0 siblings, 1 reply; 66+ messages in thread
From: Nguyen Thai Ngoc Duy @ 2012-03-02 12:47 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git, Ramsay Jones

2012/2/29 Junio C Hamano <gitster@pobox.com>:
> Nguyễn Thái Ngọc Duy <pclouds@gmail.com> writes:
>
>> Normally, all items will be gone through once. The largest cell will set
>> column width for all columns. With this strategy, one long item will
>> stretch out all columns, wasting space in between them.
>>
>> With COL_DENSE enabled, it shrinks all columns to minimum, then attempts
>> to push the last row's cells over to the next column with hope that
>> everything still fits even there's one row less. The process is repeated
>> until the new layout cannot fit in given width anymore, or there's only
>> one row left (perfect!).
>
> As you have given 4 bits for COL_MODE in the previous patch, I expected
> that this will be one of the mode that you can use, e.g. column-first dense,
> or row-first no-dense, with two more bits for operating modes.  Not calling
> it COL_MODE_DENSE and assigning a "are we dense of not?" bit out of COL_MODE
> bits feels wrong.

I reserve the 4 bits for strategies to fill the layout. One day I'm
crazy enough maybe I'll add spiral mode that put the first item in the
center, then next items in a spiral outward. COL_DENSE is about cell
size and should be applicable to any layout strategies. Maybe
COL_LAYOUT_COLUMN, COL_LAYOUT_ROW, COL_WIDTH_DENSE (and
COL_WIDTH_EQUAL) would be better names
-- 
Duy

^ permalink raw reply	[flat|nested] 66+ messages in thread

* Re: [PATCH v7 04/10] column: add dense layout support
  2012-03-02 12:47       ` Nguyen Thai Ngoc Duy
@ 2012-03-02 17:37         ` Junio C Hamano
  0 siblings, 0 replies; 66+ messages in thread
From: Junio C Hamano @ 2012-03-02 17:37 UTC (permalink / raw)
  To: Nguyen Thai Ngoc Duy; +Cc: git, Ramsay Jones

Nguyen Thai Ngoc Duy <pclouds@gmail.com> writes:

> I reserve the 4 bits for strategies to fill the layout.
> ... Maybe
> COL_LAYOUT_COLUMN, COL_LAYOUT_ROW, COL_WIDTH_DENSE (and
> COL_WIDTH_EQUAL) would be better names

OK, so these 4-bits are more like layout-bits?  That makes sense.

^ permalink raw reply	[flat|nested] 66+ messages in thread

* Re: [PATCH v7 00/10] Column display
  2012-03-02 11:25   ` [PATCH v7 00/10] Column display Thomas Rast
@ 2012-03-11  7:02     ` Nguyen Thai Ngoc Duy
  2012-03-12  6:02       ` Junio C Hamano
  0 siblings, 1 reply; 66+ messages in thread
From: Nguyen Thai Ngoc Duy @ 2012-03-11  7:02 UTC (permalink / raw)
  To: Thomas Rast; +Cc: git, Junio C Hamano, Ramsay Jones

2012/3/2 Thomas Rast <trast@inf.ethz.ch>:
> I ran valgrind over the tests in pu, and I'm seeing these errors
> presumably coming from this series:
>
> t3200-branch.out:==340== Invalid read of size 4
> t3200-branch.out:==340==    at 0x48E676: shrink_columns (column.c:101)
> t3200-branch.out:==340==    by 0x48EB76: display_table (column.c:200)
> t3200-branch.out:==340==    by 0x48ED1B: print_columns (column.c:241)
> t3200-branch.out:==340==    by 0x41967F: cmd_branch (branch.c:793)
> t3200-branch.out:==340==    by 0x40550B: run_builtin (git.c:308)
> t3200-branch.out:==340==    by 0x4056A6: handle_internal_command (git.c:468)
> t3200-branch.out:==340==    by 0x4057C0: run_argv (git.c:514)
> t3200-branch.out:==340==    by 0x40594D: main (git.c:589)
> t3200-branch.out:==340==  Address 0x57023ac is 0 bytes after a block of size 60 alloc'd
> t3200-branch.out:==340==    at 0x4C226FA: malloc (vg_replace_malloc.c:263)
> t3200-branch.out:==340==    by 0x521A11: xmalloc (wrapper.c:35)
> t3200-branch.out:==340==    by 0x48EAE8: display_table (column.c:193)
> t3200-branch.out:==340==    by 0x48ED1B: print_columns (column.c:241)
> t3200-branch.out:==340==    by 0x41967F: cmd_branch (branch.c:793)
> t3200-branch.out:==340==    by 0x40550B: run_builtin (git.c:308)
> t3200-branch.out:==340==    by 0x4056A6: handle_internal_command (git.c:468)
> t3200-branch.out:==340==    by 0x4057C0: run_argv (git.c:514)
> t3200-branch.out:==340==    by 0x40594D: main (git.c:589)
>
> and very similar errors in t7004-tag.out, t9002-column.out and
> t7508-status.out.  Can you look into it?

Thanks. There is a bug in shrink_columns() that causes invalid access.
It wil be fixed in the next reroll.
-- 
Duy

^ permalink raw reply	[flat|nested] 66+ messages in thread

* Re: [PATCH v7 00/10] Column display
  2012-03-11  7:02     ` Nguyen Thai Ngoc Duy
@ 2012-03-12  6:02       ` Junio C Hamano
  0 siblings, 0 replies; 66+ messages in thread
From: Junio C Hamano @ 2012-03-12  6:02 UTC (permalink / raw)
  To: Nguyen Thai Ngoc Duy; +Cc: Thomas Rast, git, Ramsay Jones

Nguyen Thai Ngoc Duy <pclouds@gmail.com> writes:

> 2012/3/2 Thomas Rast <trast@inf.ethz.ch>:
>> I ran valgrind over the tests in pu, and I'm seeing these errors
>> ...
>> Can you look into it?
>
> Thanks. There is a bug in shrink_columns() that causes invalid access.
> It wil be fixed in the next reroll.

Thanks.  I thought this topic was the closest to completion and was
the only candidate for 1.7.10 among various topics with loose ends
you have in flight (even though it is way too late for 1.7.10 now).

^ permalink raw reply	[flat|nested] 66+ messages in thread

* Re: [PATCH v7 00/9] Column display
  2012-03-13 12:09   ` [PATCH v7 00/9] " Nguyễn Thái Ngọc Duy
@ 2012-03-13 12:08     ` Nguyen Thai Ngoc Duy
  2012-03-13 12:09     ` [PATCH v7 01/9] Add column layout skeleton and git-column Nguyễn Thái Ngọc Duy
  1 sibling, 0 replies; 66+ messages in thread
From: Nguyen Thai Ngoc Duy @ 2012-03-13 12:08 UTC (permalink / raw)
  To: git; +Cc: Junio C Hamano, Nguyễn Thái Ngọc Duy

2012/3/13 Nguyễn Thái Ngọc Duy <pclouds@gmail.com>:
> This series fixes problems found in v6:

This series is actually v8, changes compared to the replied-to v7.
Sorry I messed it up.
-- 
Duy

^ permalink raw reply	[flat|nested] 66+ messages in thread

* [PATCH v7 00/9] Column display
  2012-02-28 11:58 ` [PATCH v7 00/10] " Nguyễn Thái Ngọc Duy
                     ` (10 preceding siblings ...)
  2012-03-02 11:25   ` [PATCH v7 00/10] Column display Thomas Rast
@ 2012-03-13 12:09   ` Nguyễn Thái Ngọc Duy
  2012-03-13 12:08     ` Nguyen Thai Ngoc Duy
  2012-03-13 12:09     ` [PATCH v7 01/9] Add column layout skeleton and git-column Nguyễn Thái Ngọc Duy
  11 siblings, 2 replies; 66+ messages in thread
From: Nguyễn Thái Ngọc Duy @ 2012-03-13 12:09 UTC (permalink / raw)
  To: git; +Cc: Junio C Hamano, Nguyễn Thái Ngọc Duy

This series fixes problems found in v6:

 - rework config code. I think it looks nicer now.
 - fix valgrind issue in shrink_columns()
 - make "help -a" respect column.*
 - only use up to term_columns() - 1. If we try to print to the last
   column, the line may be wrapped around.

This series misses the 1.7.10 train already and I wasn't going to post
this until the 1.7.10 comes out, but I'd like to take feedback on 3
extra WIP patches at the end. They deal with cases where column
layout is not as efficient as it should be. More details in the patch
description.

Nguyễn Thái Ngọc Duy (9):
  Add column layout skeleton and git-column
  Stop starting pager recursively
  column: add columnar layout
  column: add dense layout support
  help: reuse print_columns() for help -a
  branch: add --column
  status: add --column
  column: support piping stdout to external git-column process
  tag: add --column

Nguyễn Thái Ngọc Duy (3):
  ls-files: support --column
  column: support "denser" mode
  column: support grouping entries

 .gitignore                   |    1 +
 Documentation/config.txt     |   38 +++
 Documentation/git-branch.txt |    9 +
 Documentation/git-column.txt |   53 +++
 Documentation/git-status.txt |    7 +
 Documentation/git-tag.txt    |    9 +
 Makefile                     |    3 +
 builtin.h                    |    1 +
 builtin/branch.c             |   32 ++-
 builtin/column.c             |   59 ++++
 builtin/commit.c             |    7 +
 builtin/help.c               |    7 +-
 builtin/ls-files.c           |    8 +
 builtin/tag.c                |   27 ++-
 column.c                     |  724 ++++++++++++++++++++++++++++++++++++++++++
 column.h                     |   44 +++
 command-list.txt             |    1 +
 git.c                        |    1 +
 help.c                       |   59 ++--
 help.h                       |    2 +-
 pager.c                      |    2 +-
 parse-options.h              |    2 +
 t/t3200-branch.sh            |   77 +++++
 t/t7004-tag.sh               |   44 +++
 t/t7508-status.sh            |   24 ++
 t/t9002-column.sh            |  179 +++++++++++
 wt-status.c                  |   28 ++-
 wt-status.h                  |    1 +
 28 files changed, 1401 insertions(+), 48 deletions(-)
 create mode 100644 Documentation/git-column.txt
 create mode 100644 builtin/column.c
 create mode 100644 column.c
 create mode 100644 column.h
 create mode 100755 t/t9002-column.sh

-- 
1.7.8.36.g69ee2

^ permalink raw reply	[flat|nested] 66+ messages in thread

* [PATCH v7 01/9] Add column layout skeleton and git-column
  2012-03-13 12:09   ` [PATCH v7 00/9] " Nguyễn Thái Ngọc Duy
  2012-03-13 12:08     ` Nguyen Thai Ngoc Duy
@ 2012-03-13 12:09     ` Nguyễn Thái Ngọc Duy
  2012-03-13 12:09       ` [PATCH v7 02/9] Stop starting pager recursively Nguyễn Thái Ngọc Duy
  2012-03-13 22:24       ` [PATCH v7 01/9] Add column layout skeleton and git-column Junio C Hamano
  1 sibling, 2 replies; 66+ messages in thread
From: Nguyễn Thái Ngọc Duy @ 2012-03-13 12:09 UTC (permalink / raw)
  To: git; +Cc: Junio C Hamano, Nguyễn Thái Ngọc Duy

A column option string consists of many token separated by either
a space or a  comma. A token belongs to one of three groups:

 - enabling: always, never and auto
 - layout mode: currently plain (which does not layout at all)
 - other future tuning flags

git-column can be used to pipe output to from a command that wants
column layout, but not to mess with its own output code. Simpler output
code can be changed to use column layout code directly.

Thanks-to: Ramsay Jones <ramsay@ramsay1.demon.co.uk>
Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 .gitignore                   |    1 +
 Documentation/config.txt     |   18 +++++
 Documentation/git-column.txt |   53 +++++++++++++
 Makefile                     |    3 +
 builtin.h                    |    1 +
 builtin/column.c             |   59 +++++++++++++++
 column.c                     |  170 ++++++++++++++++++++++++++++++++++++++++++
 column.h                     |   35 +++++++++
 command-list.txt             |    1 +
 git.c                        |    1 +
 parse-options.h              |    2 +
 t/t9002-column.sh            |   45 +++++++++++
 12 files changed, 389 insertions(+), 0 deletions(-)
 create mode 100644 Documentation/git-column.txt
 create mode 100644 builtin/column.c
 create mode 100644 column.c
 create mode 100644 column.h
 create mode 100755 t/t9002-column.sh

diff --git a/.gitignore b/.gitignore
index 87fcc5f..2540264 100644
--- a/.gitignore
+++ b/.gitignore
@@ -26,6 +26,7 @@
 /git-cherry-pick
 /git-clean
 /git-clone
+/git-column
 /git-commit
 /git-commit-tree
 /git-config
diff --git a/Documentation/config.txt b/Documentation/config.txt
index abeb82b..147c41c 100644
--- a/Documentation/config.txt
+++ b/Documentation/config.txt
@@ -821,6 +821,24 @@ color.ui::
 	`never` if you prefer git commands not to use color unless enabled
 	explicitly with some other configuration or the `--color` option.
 
+column.ui::
+	Specify whether supported commands should output in columns.
+	This variable consists of a list of tokens separated by spaces
+	or commas:
++
+--
+`always`;;
+	always show in columns
+`never`;;
+	never show in columns
+`auto`;;
+	show in columns if the output is to the terminal
+`plain`;;
+	show in one column
+--
++
+	This option defaults to 'never'.
+
 commit.status::
 	A boolean to enable/disable inclusion of status information in the
 	commit message template when using an editor to prepare the commit
diff --git a/Documentation/git-column.txt b/Documentation/git-column.txt
new file mode 100644
index 0000000..9be16ee
--- /dev/null
+++ b/Documentation/git-column.txt
@@ -0,0 +1,53 @@
+git-column(1)
+=============
+
+NAME
+----
+git-column - Display data in columns
+
+SYNOPSIS
+--------
+[verse]
+'git column' [--command=<name>] [--[raw-]mode=<mode>] [--width=<width>]
+	     [--indent=<string>] [--nl=<string>] [--pading=<n>]
+
+DESCRIPTION
+-----------
+This command formats its input into multiple columns.
+
+OPTIONS
+-------
+--command=<name>::
+	Look up layout mode using configuration variable column.<name> and
+	column.ui.
+
+--mode=<mode>::
+	Specify layout mode. See configuration variable column.ui for option
+	syntax.
+
+--raw-mode=<n>::
+	Same as --mode but take mode encoded as a number. This is mainly used
+	by other commands that have already parsed layout mode.
+
+--width=<width>::
+	Specify the terminal width. By default 'git column' will detect the
+	terminal width, or fall back to 80 if it is unable to do so.
+
+--indent=<string>::
+	String to be printed at the beginning of each line.
+
+--nl=<N>::
+	String to be printed at the end of each line,
+	including newline character.
+
+--padding=<N>::
+	The number of spaces between columns. One space by default.
+
+
+Author
+------
+Written by Nguyen Thai Ngoc Duy <pclouds@gmail.com>
+
+GIT
+---
+Part of the linkgit:git[1] suite
diff --git a/Makefile b/Makefile
index a0de4e9..0998f0d 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
@@ -774,6 +775,7 @@ BUILTIN_OBJS += builtin/checkout-index.o
 BUILTIN_OBJS += builtin/checkout.o
 BUILTIN_OBJS += builtin/clean.o
 BUILTIN_OBJS += builtin/clone.o
+BUILTIN_OBJS += builtin/column.o
 BUILTIN_OBJS += builtin/commit-tree.o
 BUILTIN_OBJS += builtin/commit.o
 BUILTIN_OBJS += builtin/config.o
@@ -2166,6 +2168,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/builtin.h b/builtin.h
index 857b9c8..338f540 100644
--- a/builtin.h
+++ b/builtin.h
@@ -61,6 +61,7 @@ extern int cmd_cherry(int argc, const char **argv, const char *prefix);
 extern int cmd_cherry_pick(int argc, const char **argv, const char *prefix);
 extern int cmd_clone(int argc, const char **argv, const char *prefix);
 extern int cmd_clean(int argc, const char **argv, const char *prefix);
+extern int cmd_column(int argc, const char **argv, const char *prefix);
 extern int cmd_commit(int argc, const char **argv, const char *prefix);
 extern int cmd_commit_tree(int argc, const char **argv, const char *prefix);
 extern int cmd_config(int argc, const char **argv, const char *prefix);
diff --git a/builtin/column.c b/builtin/column.c
new file mode 100644
index 0000000..5ea798a
--- /dev/null
+++ b/builtin/column.c
@@ -0,0 +1,59 @@
+#include "builtin.h"
+#include "cache.h"
+#include "strbuf.h"
+#include "parse-options.h"
+#include "string-list.h"
+#include "column.h"
+
+static const char * const builtin_column_usage[] = {
+	"git column [options]",
+	NULL
+};
+static unsigned int colopts;
+
+static int column_config(const char *var, const char *value, void *cb)
+{
+	return git_column_config(var, value, cb, &colopts);
+}
+
+int cmd_column(int argc, const char **argv, const char *prefix)
+{
+	struct string_list list = STRING_LIST_INIT_DUP;
+	struct strbuf sb = STRBUF_INIT;
+	struct column_options copts;
+	const char *command = NULL, *real_command = NULL;
+	struct option options[] = {
+		OPT_STRING(0, "command", &real_command, "name", "lookup config vars"),
+		OPT_COLUMN(0, "mode", &colopts, "layout to use"),
+		OPT_INTEGER(0, "raw-mode", &colopts, "layout to use"),
+		OPT_INTEGER(0, "width", &copts.width, "Maximum width"),
+		OPT_STRING(0, "indent", &copts.indent, "string", "Padding space on left border"),
+		OPT_INTEGER(0, "nl", &copts.nl, "Padding space on right border"),
+		OPT_INTEGER(0, "padding", &copts.padding, "Padding space between columns"),
+		OPT_END()
+	};
+
+	/* This one is special and must be the first one */
+	if (argc > 1 && !prefixcmp(argv[1], "--command=")) {
+		command = argv[1] + 10;
+		git_config(column_config, (void *)command);
+	} else
+		git_config(column_config, NULL);
+
+	memset(&copts, 0, sizeof(copts));
+	copts.width = term_columns();
+	copts.padding = 1;
+	argc = parse_options(argc, argv, "", options, builtin_column_usage, 0);
+	if (argc)
+		usage_with_options(builtin_column_usage, options);
+	if (real_command || command) {
+		if (!real_command || !command || strcmp(real_command, command))
+			die(_("--command must be the first argument"));
+	}
+	finalize_colopts(&colopts, -1);
+	while (!strbuf_getline(&sb, stdin, '\n'))
+		string_list_append(&list, sb.buf);
+
+	print_columns(&list, colopts, &copts);
+	return 0;
+}
diff --git a/column.c b/column.c
new file mode 100644
index 0000000..f0877c0
--- /dev/null
+++ b/column.c
@@ -0,0 +1,170 @@
+#include "cache.h"
+#include "column.h"
+#include "string-list.h"
+#include "parse-options.h"
+
+/* Display without layout when not enabled */
+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 colopts,
+		   const struct column_options *opts)
+{
+	struct column_options nopts;
+
+	if (!list->nr)
+		return;
+	assert(COL_ENABLE(colopts) != COL_AUTO);
+
+	memset(&nopts, 0, sizeof(nopts));
+	nopts.indent = opts && opts->indent ? opts->indent : "";
+	nopts.nl = opts && opts->nl ? opts->nl : "\n";
+	nopts.padding = opts ? opts->padding : 1;
+	nopts.width = opts && opts->width ? opts->width : term_columns() - 1;
+	if (!COL_ENABLE(colopts)) {
+		display_plain(list, "", "\n");
+		return;
+	}
+	switch (COL_LAYOUT(colopts)) {
+	case COL_PLAIN:
+		display_plain(list, nopts.indent, nopts.nl);
+		break;
+	default:
+		die("BUG: invalid layout mode %d", COL_LAYOUT(colopts));
+	}
+}
+
+int finalize_colopts(unsigned int *colopts, int stdout_is_tty)
+{
+	if (COL_ENABLE(*colopts) == COL_AUTO) {
+		if (stdout_is_tty < 0)
+			stdout_is_tty = isatty(1);
+		*colopts &= ~COL_ENABLE_MASK;
+		if (stdout_is_tty)
+			*colopts |= COL_ENABLED;
+	}
+	return 0;
+}
+
+struct colopt {
+	const char *name;
+	unsigned int value;
+	unsigned int mask;
+};
+
+#define LAYOUT_SET 1
+#define ENABLE_SET 2
+
+static int parse_option(const char *arg, int len, unsigned int *colopts,
+			int *group_set)
+{
+	struct colopt opts[] = {
+		{ "always", COL_ENABLED,  COL_ENABLE_MASK },
+		{ "never",  COL_DISABLED, COL_ENABLE_MASK },
+		{ "auto",   COL_AUTO,     COL_ENABLE_MASK },
+		{ "plain",  COL_PLAIN,    COL_LAYOUT_MASK },
+	};
+	int i;
+
+	for (i = 0; i < ARRAY_SIZE(opts); i++) {
+		int arg_len = len, name_len;
+		const char *arg_str = arg;
+
+		name_len = strlen(opts[i].name);
+		if (arg_len != name_len ||
+		    strncmp(arg_str, opts[i].name, name_len))
+			continue;
+
+		switch (opts[i].mask) {
+		case COL_ENABLE_MASK:
+			*group_set |= ENABLE_SET;
+			break;
+		case COL_LAYOUT_MASK:
+			*group_set |= LAYOUT_SET;
+			break;
+		}
+
+		if (opts[i].mask)
+			*colopts = (*colopts & ~opts[i].mask) | opts[i].value;
+		return 0;
+	}
+
+	return error("unsupported option '%s'", arg);
+}
+
+static int parse_config(unsigned int *colopts, const char *value)
+{
+	const char *sep = " ,";
+	int group_set = 0;
+
+	while (*value) {
+		int len = strcspn(value, sep);
+		if (len) {
+			if (parse_option(value, len, colopts, &group_set))
+				return -1;
+
+			value += len;
+		}
+		value += strspn(value, sep);
+	}
+	/*
+	 * Setting layout implies "always" if neither always, never
+	 * nor auto is specified.
+	 *
+	 * Current COL_ENABLE() value is disregarded. This means if
+	 * you set column.ui = auto and pass --column=row, then "auto"
+	 * will become "always".
+	 */
+	if ((group_set & LAYOUT_SET) && !(group_set & ENABLE_SET))
+		*colopts = (*colopts & ~COL_ENABLE_MASK) | COL_ENABLED;
+	return 0;
+}
+
+static int column_config(const char *var, const char *value,
+			 const char *key, unsigned int *colopts)
+{
+	if (parse_config(colopts, value))
+		return error("invalid %s mode %s", key, value);
+	return 0;
+}
+
+int git_column_config(const char *var, const char *value,
+		      const char *command, unsigned int *colopts)
+{
+	if (!strcmp(var, "column.ui"))
+		return column_config(var, value, "column.ui", colopts);
+
+	if (command) {
+		struct strbuf sb = STRBUF_INIT;
+		int ret = 0;
+		strbuf_addf(&sb, "column.%s", command);
+		if (!strcmp(var, sb.buf))
+			ret = column_config(var, value, sb.buf, colopts);
+		strbuf_release(&sb);
+		return ret;
+	}
+
+	return 0;
+}
+
+int parseopt_column_callback(const struct option *opt,
+			     const char *arg, int unset)
+{
+	unsigned int *colopts = opt->value;
+	*colopts |= COL_PARSEOPT;
+	*colopts &= ~COL_ENABLE_MASK;
+	if (unset)		/* --no-column == never */
+		return 0;
+	/* --column == always unless "arg" states otherwise */
+	*colopts |= COL_ENABLED;
+	if (arg)
+		return parse_config(colopts, arg);
+
+	return 0;
+}
diff --git a/column.h b/column.h
new file mode 100644
index 0000000..778a71c
--- /dev/null
+++ b/column.h
@@ -0,0 +1,35 @@
+#ifndef COLUMN_H
+#define COLUMN_H
+
+#define COL_LAYOUT_MASK   0x000F
+#define COL_ENABLE_MASK   0x0030   /* always, never or auto */
+#define COL_PARSEOPT      0x0040   /* --column is given from cmdline */
+
+#define COL_ENABLE(c) ((c) & COL_ENABLE_MASK)
+#define COL_DISABLED      0x0000   /* must be zero */
+#define COL_ENABLED       0x0010
+#define COL_AUTO          0x0020
+
+#define COL_LAYOUT(c) ((c) & COL_LAYOUT_MASK)
+#define COL_PLAIN             15   /* one column */
+
+#define explicitly_enable_column(c) \
+	(((c) & COL_PARSEOPT) && COL_ENABLE(c) == COL_ENABLED)
+
+struct column_options {
+	int width;
+	int padding;
+	const char *indent;
+	const char *nl;
+};
+
+struct option;
+extern int parseopt_column_callback(const struct option *, const char *, int);
+extern int git_column_config(const char *var, const char *value,
+			     const char *command, unsigned int *colopts);
+extern int finalize_colopts(unsigned int *colopts, int stdout_is_tty);
+
+extern void print_columns(const struct string_list *list, unsigned int colopts,
+			  const struct column_options *opts);
+
+#endif
diff --git a/command-list.txt b/command-list.txt
index a36ee9b..fe06f15 100644
--- a/command-list.txt
+++ b/command-list.txt
@@ -20,6 +20,7 @@ git-cherry-pick                         mainporcelain
 git-citool                              mainporcelain
 git-clean                               mainporcelain
 git-clone                               mainporcelain common
+git-column                              purehelpers
 git-commit                              mainporcelain common
 git-commit-tree                         plumbingmanipulators
 git-config                              ancillarymanipulators
diff --git a/git.c b/git.c
index 3805616..ee727cb 100644
--- a/git.c
+++ b/git.c
@@ -348,6 +348,7 @@ static void handle_internal_command(int argc, const char **argv)
 		{ "cherry-pick", cmd_cherry_pick, RUN_SETUP | NEED_WORK_TREE },
 		{ "clean", cmd_clean, RUN_SETUP | NEED_WORK_TREE },
 		{ "clone", cmd_clone },
+		{ "column", cmd_column, RUN_SETUP_GENTLY },
 		{ "commit", cmd_commit, RUN_SETUP | NEED_WORK_TREE },
 		{ "commit-tree", cmd_commit_tree, RUN_SETUP },
 		{ "config", cmd_config, RUN_SETUP_GENTLY },
diff --git a/parse-options.h b/parse-options.h
index 2e811dc..56fcafd 100644
--- a/parse-options.h
+++ b/parse-options.h
@@ -238,5 +238,7 @@ extern int parse_opt_noop_cb(const struct option *, const char *, int);
 	  PARSE_OPT_OPTARG, &parse_opt_abbrev_cb, 0 }
 #define OPT__COLOR(var, h) \
 	OPT_COLOR_FLAG(0, "color", (var), (h))
+#define OPT_COLUMN(s, l, v, h) \
+	{ OPTION_CALLBACK, (s), (l), (v), "style", (h), PARSE_OPT_OPTARG, parseopt_column_callback }
 
 #endif
diff --git a/t/t9002-column.sh b/t/t9002-column.sh
new file mode 100755
index 0000000..a7f3cd9
--- /dev/null
+++ b/t/t9002-column.sh
@@ -0,0 +1,45 @@
+#!/bin/sh
+
+test_description='git column'
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+	cat >lista <<\EOF
+one
+two
+three
+four
+five
+six
+seven
+eight
+nine
+ten
+eleven
+EOF
+'
+
+test_expect_success 'never' '
+	git column --indent=Z --mode=never <lista >actual &&
+	test_cmp lista actual
+'
+
+test_expect_success 'always' '
+	cat >expected <<\EOF &&
+Zone
+Ztwo
+Zthree
+Zfour
+Zfive
+Zsix
+Zseven
+Zeight
+Znine
+Zten
+Zeleven
+EOF
+	git column --indent=Z --mode=plain <lista >actual &&
+	test_cmp expected actual
+'
+
+test_done
-- 
1.7.8.36.g69ee2

^ permalink raw reply related	[flat|nested] 66+ messages in thread

* [PATCH v7 02/9] Stop starting pager recursively
  2012-03-13 12:09     ` [PATCH v7 01/9] Add column layout skeleton and git-column Nguyễn Thái Ngọc Duy
@ 2012-03-13 12:09       ` Nguyễn Thái Ngọc Duy
  2012-03-13 12:09         ` [PATCH v7 03/9] column: add columnar layout Nguyễn Thái Ngọc Duy
  2012-03-13 22:24       ` [PATCH v7 01/9] Add column layout skeleton and git-column Junio C Hamano
  1 sibling, 1 reply; 66+ messages in thread
From: Nguyễn Thái Ngọc Duy @ 2012-03-13 12:09 UTC (permalink / raw)
  To: git; +Cc: Junio C Hamano, Nguyễn Thái Ngọc Duy

git-column can be used as a pager for other git commands, something
like this:

    GIT_PAGER="git -p column --mode='dense color'" git -p branch

The problem with this is that "git -p column" also has $GIT_PAGER set so
the pager runs itself again as another pager. The end result is an
infinite loop of forking. Other git commands have the same problem if
being abused this way.

Check if $GIT_PAGER is already set and stop launching another pager.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 pager.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)

diff --git a/pager.c b/pager.c
index 05584de..4dcb08d 100644
--- a/pager.c
+++ b/pager.c
@@ -73,7 +73,7 @@ void setup_pager(void)
 {
 	const char *pager = git_pager(isatty(1));
 
-	if (!pager)
+	if (!pager || pager_in_use())
 		return;
 
 	/*
-- 
1.7.8.36.g69ee2

^ permalink raw reply related	[flat|nested] 66+ messages in thread

* [PATCH v7 03/9] column: add columnar layout
  2012-03-13 12:09       ` [PATCH v7 02/9] Stop starting pager recursively Nguyễn Thái Ngọc Duy
@ 2012-03-13 12:09         ` Nguyễn Thái Ngọc Duy
  2012-03-13 12:09           ` [PATCH v7 04/9] column: add dense layout support Nguyễn Thái Ngọc Duy
  0 siblings, 1 reply; 66+ messages in thread
From: Nguyễn Thái Ngọc Duy @ 2012-03-13 12:09 UTC (permalink / raw)
  To: git; +Cc: Junio C Hamano, Nguyễn Thái Ngọc Duy

COL_COLUMN and COL_ROW fill column by column (or row by row
respectively), given the terminal width and how many space between
columns. All cells have equal width.

Strings are supposed to be in UTF-8. Valid ANSI escape strings are OK.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 Documentation/config.txt |    4 ++
 column.c                 |  114 ++++++++++++++++++++++++++++++++++++++++++++++
 column.h                 |    2 +
 t/t9002-column.sh        |   86 ++++++++++++++++++++++++++++++++++
 4 files changed, 206 insertions(+), 0 deletions(-)

diff --git a/Documentation/config.txt b/Documentation/config.txt
index 147c41c..d1d74a1 100644
--- a/Documentation/config.txt
+++ b/Documentation/config.txt
@@ -833,6 +833,10 @@ column.ui::
 	never show in columns
 `auto`;;
 	show in columns if the output is to the terminal
+`column`;;
+	fill columns before rows (default)
+`row`;;
+	fill rows before columns
 `plain`;;
 	show in one column
 --
diff --git a/column.c b/column.c
index f0877c0..17c2479 100644
--- a/column.c
+++ b/column.c
@@ -2,6 +2,59 @@
 #include "column.h"
 #include "string-list.h"
 #include "parse-options.h"
+#include "utf8.h"
+
+#define XY2LINEAR(d, x, y) (COL_LAYOUT((d)->colopts) == COL_COLUMN ? \
+			    (x) * (d)->rows + (y) : \
+			    (y) * (d)->cols + (x))
+
+struct column_data {
+	const struct string_list *list;
+	unsigned int colopts;
+	struct column_options opts;
+
+	int rows, cols;
+	int *len;		/* cell length */
+};
+
+/* return length of 's' in letters, ANSI escapes stripped */
+static int item_length(unsigned int colopts, const char *s)
+{
+	int len, i = 0;
+	struct strbuf str = STRBUF_INIT;
+
+	strbuf_addstr(&str, s);
+	while ((s = strstr(str.buf + i, "\033[")) != NULL) {
+		int len = strspn(s + 2, "0123456789;");
+		i = s - str.buf;
+		strbuf_remove(&str, i, len + 3); /* \033[<len><func char> */
+	}
+	len = utf8_strwidth(str.buf);
+	strbuf_release(&str);
+	return len;
+}
+
+/*
+ * Calculate cell width, rows and cols for a table of equal cells, given
+ * table width and how many spaces between cells.
+ */
+static void layout(struct column_data *data, int *width)
+{
+	int i;
+
+	*width = 0;
+	for (i = 0; i < data->list->nr; i++)
+		if (*width < data->len[i])
+			*width = data->len[i];
+
+	*width += data->opts.padding;
+
+	data->cols = (data->opts.width - strlen(data->opts.indent)) / *width;
+	if (data->cols == 0)
+		data->cols = 1;
+
+	data->rows = DIV_ROUND_UP(data->list->nr, data->cols);
+}
 
 /* Display without layout when not enabled */
 static void display_plain(const struct string_list *list,
@@ -13,6 +66,61 @@ static void display_plain(const struct string_list *list,
 		printf("%s%s%s", indent, list->items[i].string, nl);
 }
 
+/* Print a cell to stdout with all necessary leading/traling space */
+static int display_cell(struct column_data *data, int initial_width,
+			const char *empty_cell, int x, int y)
+{
+	int i, len, newline;
+
+	i = XY2LINEAR(data, x, y);
+	if (i >= data->list->nr)
+		return -1;
+	len = data->len[i];
+	if (COL_LAYOUT(data->colopts) == COL_COLUMN)
+		newline = i + data->rows >= data->list->nr;
+	else
+		newline = x == data->cols - 1 || i == data->list->nr - 1;
+
+	printf("%s%s%s",
+	       x == 0 ? data->opts.indent : "",
+	       data->list->items[i].string,
+	       newline ? data->opts.nl : empty_cell + len);
+	return 0;
+}
+
+/* Display COL_COLUMN or COL_ROW */
+static void display_table(const struct string_list *list,
+			  unsigned int colopts,
+			  const struct column_options *opts)
+{
+	struct column_data data;
+	int x, y, i, initial_width;
+	char *empty_cell;
+
+	memset(&data, 0, sizeof(data));
+	data.list = list;
+	data.colopts = colopts;
+	data.opts = *opts;
+
+	data.len = xmalloc(sizeof(*data.len) * list->nr);
+	for (i = 0; i < list->nr; i++)
+		data.len[i] = item_length(colopts, list->items[i].string);
+
+	layout(&data, &initial_width);
+
+	empty_cell = xmalloc(initial_width + 1);
+	memset(empty_cell, ' ', initial_width);
+	empty_cell[initial_width] = '\0';
+	for (y = 0; y < data.rows; y++) {
+		for (x = 0; x < data.cols; x++)
+			if (display_cell(&data, initial_width, empty_cell, x, y))
+				break;
+	}
+
+	free(data.len);
+	free(empty_cell);
+}
+
 void print_columns(const struct string_list *list, unsigned int colopts,
 		   const struct column_options *opts)
 {
@@ -35,6 +143,10 @@ void print_columns(const struct string_list *list, unsigned int colopts,
 	case COL_PLAIN:
 		display_plain(list, nopts.indent, nopts.nl);
 		break;
+	case COL_ROW:
+	case COL_COLUMN:
+		display_table(list, colopts, &nopts);
+		break;
 	default:
 		die("BUG: invalid layout mode %d", COL_LAYOUT(colopts));
 	}
@@ -69,6 +181,8 @@ static int parse_option(const char *arg, int len, unsigned int *colopts,
 		{ "never",  COL_DISABLED, COL_ENABLE_MASK },
 		{ "auto",   COL_AUTO,     COL_ENABLE_MASK },
 		{ "plain",  COL_PLAIN,    COL_LAYOUT_MASK },
+		{ "column", COL_COLUMN,   COL_LAYOUT_MASK },
+		{ "row",    COL_ROW,      COL_LAYOUT_MASK },
 	};
 	int i;
 
diff --git a/column.h b/column.h
index 778a71c..79ac50d 100644
--- a/column.h
+++ b/column.h
@@ -11,6 +11,8 @@
 #define COL_AUTO          0x0020
 
 #define COL_LAYOUT(c) ((c) & COL_LAYOUT_MASK)
+#define COL_COLUMN             0   /* Fill columns before rows */
+#define COL_ROW                1   /* Fill rows before columns */
 #define COL_PLAIN             15   /* one column */
 
 #define explicitly_enable_column(c) \
diff --git a/t/t9002-column.sh b/t/t9002-column.sh
index a7f3cd9..ec288ae 100755
--- a/t/t9002-column.sh
+++ b/t/t9002-column.sh
@@ -42,4 +42,90 @@ EOF
 	test_cmp expected actual
 '
 
+test_expect_success '80 columns' '
+	cat >expected <<\EOF &&
+one    two    three  four   five   six    seven  eight  nine   ten    eleven
+EOF
+	COLUMNS=80 git column --mode=column <lista >actual &&
+	test_cmp expected actual
+'
+
+test_expect_success 'COLUMNS = 1' '
+	cat >expected <<\EOF &&
+one
+two
+three
+four
+five
+six
+seven
+eight
+nine
+ten
+eleven
+EOF
+	COLUMNS=1 git column --mode=column <lista >actual &&
+	test_cmp expected actual
+'
+
+test_expect_success 'width = 1' '
+	git column --mode=column --width=1 <lista >actual &&
+	test_cmp expected actual
+'
+
+COLUMNS=20
+export COLUMNS
+
+test_expect_success '20 columns' '
+	cat >expected <<\EOF &&
+one    seven
+two    eight
+three  nine
+four   ten
+five   eleven
+six
+EOF
+	git column --mode=column <lista >actual &&
+	test_cmp expected actual
+'
+
+test_expect_success '20 columns, padding 2' '
+	cat >expected <<\EOF &&
+one     seven
+two     eight
+three   nine
+four    ten
+five    eleven
+six
+EOF
+	git column --mode=column --padding 2 <lista >actual &&
+	test_cmp expected actual
+'
+
+test_expect_success '20 columns, indented' '
+	cat >expected <<\EOF &&
+  one    seven
+  two    eight
+  three  nine
+  four   ten
+  five   eleven
+  six
+EOF
+	git column --mode=column --indent="  " <lista >actual &&
+	test_cmp expected actual
+'
+
+test_expect_success '20 columns, row first' '
+	cat >expected <<\EOF &&
+one    two
+three  four
+five   six
+seven  eight
+nine   ten
+eleven
+EOF
+	git column --mode=row <lista >actual &&
+	test_cmp expected actual
+'
+
 test_done
-- 
1.7.8.36.g69ee2

^ permalink raw reply related	[flat|nested] 66+ messages in thread

* [PATCH v7 04/9] column: add dense layout support
  2012-03-13 12:09         ` [PATCH v7 03/9] column: add columnar layout Nguyễn Thái Ngọc Duy
@ 2012-03-13 12:09           ` Nguyễn Thái Ngọc Duy
  2012-03-13 12:09             ` [PATCH v7 05/9] help: reuse print_columns() for help -a Nguyễn Thái Ngọc Duy
  0 siblings, 1 reply; 66+ messages in thread
From: Nguyễn Thái Ngọc Duy @ 2012-03-13 12:09 UTC (permalink / raw)
  To: git; +Cc: Junio C Hamano, Nguyễn Thái Ngọc Duy

Normally all cells (and in turn columns) share the same width. This
layout mode can waste space because one long item can stretch our all
columns.

With COL_DENSE enabled, column width is calculated indepdendently. All
columns are shrunk to minimum, then it attempts to push cells of the
last row over to the next column with hope that everything still fits
even there's one row less. The process is repeated until the new layout
cannot fit in given width any more, or there's only one row left
(perfect!).

Apparently, this mode consumes more cpu than the old one, but it makes
better use of terminal space. For layouting one or two screens, cpu
usage should not be detectable.

This patch introduces option handling code besides layout modes and
enable/disable to expose this feature as "dense". The feature can be
turned off by specifying "nodense".

Thanks-to: Ramsay Jones <ramsay@ramsay1.demon.co.uk>
Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 Documentation/config.txt |    4 ++
 column.c                 |   84 +++++++++++++++++++++++++++++++++++++++++++++-
 column.h                 |    2 +
 t/t9002-column.sh        |   48 ++++++++++++++++++++++++++
 4 files changed, 137 insertions(+), 1 deletions(-)

diff --git a/Documentation/config.txt b/Documentation/config.txt
index d1d74a1..aacf8ab 100644
--- a/Documentation/config.txt
+++ b/Documentation/config.txt
@@ -839,6 +839,10 @@ column.ui::
 	fill rows before columns
 `plain`;;
 	show in one column
+`dense`;;
+	make unequal size columns to utilize more space
+`nodense`;;
+	make equal size columns
 --
 +
 	This option defaults to 'never'.
diff --git a/column.c b/column.c
index 17c2479..3aac28b 100644
--- a/column.c
+++ b/column.c
@@ -15,6 +15,7 @@ struct column_data {
 
 	int rows, cols;
 	int *len;		/* cell length */
+	int *width;	      /* index to the longest row in column */
 };
 
 /* return length of 's' in letters, ANSI escapes stripped */
@@ -56,6 +57,57 @@ static void layout(struct column_data *data, int *width)
 	data->rows = DIV_ROUND_UP(data->list->nr, data->cols);
 }
 
+static void compute_column_width(struct column_data *data)
+{
+	int i, x, y;
+	for (x = 0; x < data->cols; x++) {
+		data->width[x] = XY2LINEAR(data, x, 0);
+		for (y = 0; y < data->rows; y++) {
+			i = XY2LINEAR(data, x, y);
+			if (i < data->list->nr &&
+			    data->len[data->width[x]] < data->len[i])
+				data->width[x] = i;
+		}
+	}
+}
+
+/*
+ * Shrink all columns by shortening them one row each time (and adding
+ * more columns along the way). Hopefully the longest cell will be
+ * moved to the next column, column is shrunk so we have more space
+ * for new columns. The process ends when the whole thing no longer
+ * fits in data->total_width.
+ */
+static void shrink_columns(struct column_data *data)
+{
+	data->width = xrealloc(data->width,
+			       sizeof(*data->width) * data->cols);
+	while (data->rows > 1) {
+		int x, total_width, cols, rows;
+		rows = data->rows;
+		cols = data->cols;
+
+		data->rows--;
+		data->cols = DIV_ROUND_UP(data->list->nr, data->rows);
+		if (data->cols != cols)
+			data->width = xrealloc(data->width,
+					       sizeof(*data->width) * data->cols);
+		compute_column_width(data);
+
+		total_width = strlen(data->opts.indent);
+		for (x = 0; x < data->cols; x++) {
+			total_width += data->len[data->width[x]];
+			total_width += data->opts.padding;
+		}
+		if (total_width > data->opts.width) {
+			data->rows = rows;
+			data->cols = cols;
+			break;
+		}
+	}
+	compute_column_width(data);
+}
+
 /* Display without layout when not enabled */
 static void display_plain(const struct string_list *list,
 			  const char *indent, const char *nl)
@@ -75,7 +127,18 @@ static int display_cell(struct column_data *data, int initial_width,
 	i = XY2LINEAR(data, x, y);
 	if (i >= data->list->nr)
 		return -1;
+
 	len = data->len[i];
+	if (data->width && data->len[data->width[x]] < initial_width) {
+		/*
+		 * empty_cell has initial_width chars, if real column
+		 * is narrower, increase len a bit so we fill less
+		 * space.
+		 */
+		len += initial_width - data->len[data->width[x]];
+		len -= data->opts.padding;
+	}
+
 	if (COL_LAYOUT(data->colopts) == COL_COLUMN)
 		newline = i + data->rows >= data->list->nr;
 	else
@@ -108,6 +171,9 @@ static void display_table(const struct string_list *list,
 
 	layout(&data, &initial_width);
 
+	if (colopts & COL_DENSE)
+		shrink_columns(&data);
+
 	empty_cell = xmalloc(initial_width + 1);
 	memset(empty_cell, ' ', initial_width);
 	empty_cell[initial_width] = '\0';
@@ -118,6 +184,7 @@ static void display_table(const struct string_list *list,
 	}
 
 	free(data.len);
+	free(data.width);
 	free(empty_cell);
 }
 
@@ -183,13 +250,22 @@ static int parse_option(const char *arg, int len, unsigned int *colopts,
 		{ "plain",  COL_PLAIN,    COL_LAYOUT_MASK },
 		{ "column", COL_COLUMN,   COL_LAYOUT_MASK },
 		{ "row",    COL_ROW,      COL_LAYOUT_MASK },
+		{ "dense",  COL_DENSE,    0 },
 	};
 	int i;
 
 	for (i = 0; i < ARRAY_SIZE(opts); i++) {
-		int arg_len = len, name_len;
+		int set = 1, arg_len = len, name_len;
 		const char *arg_str = arg;
 
+		if (!opts[i].mask) {
+			if (arg_len > 2 && !strncmp(arg_str, "no", 2)) {
+				arg_str += 2;
+				arg_len -= 2;
+				set = 0;
+			}
+		}
+
 		name_len = strlen(opts[i].name);
 		if (arg_len != name_len ||
 		    strncmp(arg_str, opts[i].name, name_len))
@@ -206,6 +282,12 @@ static int parse_option(const char *arg, int len, unsigned int *colopts,
 
 		if (opts[i].mask)
 			*colopts = (*colopts & ~opts[i].mask) | opts[i].value;
+		else {
+			if (set)
+				*colopts |= opts[i].value;
+			else
+				*colopts &= ~opts[i].value;
+		}
 		return 0;
 	}
 
diff --git a/column.h b/column.h
index 79ac50d..b3e979f 100644
--- a/column.h
+++ b/column.h
@@ -4,6 +4,8 @@
 #define COL_LAYOUT_MASK   0x000F
 #define COL_ENABLE_MASK   0x0030   /* always, never or auto */
 #define COL_PARSEOPT      0x0040   /* --column is given from cmdline */
+#define COL_DENSE         0x0080   /* Shrink columns when possible,
+				      making space for more columns */
 
 #define COL_ENABLE(c) ((c) & COL_ENABLE_MASK)
 #define COL_DISABLED      0x0000   /* must be zero */
diff --git a/t/t9002-column.sh b/t/t9002-column.sh
index ec288ae..c7d7a65 100755
--- a/t/t9002-column.sh
+++ b/t/t9002-column.sh
@@ -89,6 +89,30 @@ EOF
 	test_cmp expected actual
 '
 
+test_expect_success '20 columns, nodense' '
+	cat >expected <<\EOF &&
+one    seven
+two    eight
+three  nine
+four   ten
+five   eleven
+six
+EOF
+	git column --mode=column,nodense < lista > actual &&
+	test_cmp expected actual
+'
+
+test_expect_success '20 columns, dense' '
+	cat >expected <<\EOF &&
+one   five  nine
+two   six   ten
+three seven eleven
+four  eight
+EOF
+	git column --mode=column,dense < lista > actual &&
+	test_cmp expected actual
+'
+
 test_expect_success '20 columns, padding 2' '
 	cat >expected <<\EOF &&
 one     seven
@@ -128,4 +152,28 @@ EOF
 	test_cmp expected actual
 '
 
+test_expect_success '20 columns, row first, nodense' '
+	cat >expected <<\EOF &&
+one    two
+three  four
+five   six
+seven  eight
+nine   ten
+eleven
+EOF
+	git column --mode=row,nodense <lista >actual &&
+	test_cmp expected actual
+'
+
+test_expect_success '20 columns, row first, dense' '
+	cat >expected <<\EOF &&
+one   two    three
+four  five   six
+seven eight  nine
+ten   eleven
+EOF
+	git column --mode=row,dense <lista >actual &&
+	test_cmp expected actual
+'
+
 test_done
-- 
1.7.8.36.g69ee2

^ permalink raw reply related	[flat|nested] 66+ messages in thread

* [PATCH v7 05/9] help: reuse print_columns() for help -a
  2012-03-13 12:09           ` [PATCH v7 04/9] column: add dense layout support Nguyễn Thái Ngọc Duy
@ 2012-03-13 12:09             ` Nguyễn Thái Ngọc Duy
  2012-03-13 12:09               ` [PATCH v7 06/9] branch: add --column Nguyễn Thái Ngọc Duy
  0 siblings, 1 reply; 66+ messages in thread
From: Nguyễn Thái Ngọc Duy @ 2012-03-13 12:09 UTC (permalink / raw)
  To: git; +Cc: Junio C Hamano, Nguyễn Thái Ngọc Duy

"help -a" also respects column.ui (and column.help if presents)

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 builtin/help.c |    7 +++++-
 help.c         |   59 +++++++++++++++++++++----------------------------------
 help.h         |    2 +-
 3 files changed, 30 insertions(+), 38 deletions(-)

diff --git a/builtin/help.c b/builtin/help.c
index 61ff798..c64f152 100644
--- a/builtin/help.c
+++ b/builtin/help.c
@@ -9,6 +9,7 @@
 #include "common-cmds.h"
 #include "parse-options.h"
 #include "run-command.h"
+#include "column.h"
 #include "help.h"
 
 static struct man_viewer_list {
@@ -30,6 +31,7 @@ enum help_format {
 };
 
 static int show_all = 0;
+static unsigned int colopts;
 static enum help_format help_format = HELP_FORMAT_NONE;
 static struct option builtin_help_options[] = {
 	OPT_BOOLEAN('a', "all", &show_all, "print all available commands"),
@@ -251,6 +253,8 @@ static int add_man_viewer_info(const char *var, const char *value)
 
 static int git_help_config(const char *var, const char *value, void *cb)
 {
+	if (!prefixcmp(var, "column."))
+		return git_column_config(var, value, "help", &colopts);
 	if (!strcmp(var, "help.format")) {
 		if (!value)
 			return config_error_nonbool(var);
@@ -424,8 +428,9 @@ int cmd_help(int argc, const char **argv, const char *prefix)
 	parsed_help_format = help_format;
 
 	if (show_all) {
+		git_config(git_help_config, NULL);
 		printf("usage: %s\n\n", git_usage_string);
-		list_commands("git commands", &main_cmds, &other_cmds);
+		list_commands("git commands", colopts, &main_cmds, &other_cmds);
 		printf("%s\n", git_more_info_string);
 		return 0;
 	}
diff --git a/help.c b/help.c
index 14eefc9..a815ae6 100644
--- a/help.c
+++ b/help.c
@@ -4,6 +4,8 @@
 #include "levenshtein.h"
 #include "help.h"
 #include "common-cmds.h"
+#include "string-list.h"
+#include "column.h"
 
 void add_cmdname(struct cmdnames *cmds, const char *name, int len)
 {
@@ -70,31 +72,25 @@ void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes)
 	cmds->cnt = cj;
 }
 
-static void pretty_print_string_list(struct cmdnames *cmds, int longest)
+static void pretty_print_string_list(struct cmdnames *cmds,
+				     unsigned int colopts)
 {
-	int cols = 1, rows;
-	int space = longest + 1; /* min 1 SP between words */
-	int max_cols = term_columns() - 1; /* don't print *on* the edge */
-	int i, j;
-
-	if (space < max_cols)
-		cols = max_cols / space;
-	rows = DIV_ROUND_UP(cmds->cnt, cols);
-
-	for (i = 0; i < rows; i++) {
-		printf("  ");
+	struct string_list list = STRING_LIST_INIT_NODUP;
+	struct column_options copts;
+	int i;
 
-		for (j = 0; j < cols; j++) {
-			int n = j * rows + i;
-			int size = space;
-			if (n >= cmds->cnt)
-				break;
-			if (j == cols-1 || n + rows >= cmds->cnt)
-				size = 1;
-			printf("%-*s", size, cmds->names[n]->name);
-		}
-		putchar('\n');
-	}
+	for (i = 0; i < cmds->cnt; i++)
+		string_list_append(&list, cmds->names[i]->name);
+	/*
+	 * always enable column display, we only consult column.*
+	 * about layout strategy and stuff
+	 */
+	colopts = (colopts & ~COL_ENABLE_MASK) | COL_ENABLED;
+	memset(&copts, 0, sizeof(copts));
+	copts.indent = "  ";
+	copts.padding = 2;
+	print_columns(&list, colopts, &copts);
+	string_list_clear(&list, 0);
 }
 
 static int is_executable(const char *name)
@@ -203,25 +199,16 @@ void load_command_list(const char *prefix,
 	exclude_cmds(other_cmds, main_cmds);
 }
 
-void list_commands(const char *title, struct cmdnames *main_cmds,
-		   struct cmdnames *other_cmds)
+void list_commands(const char *title, unsigned int colopts,
+		   struct cmdnames *main_cmds, struct cmdnames *other_cmds)
 {
-	int i, longest = 0;
-
-	for (i = 0; i < main_cmds->cnt; i++)
-		if (longest < main_cmds->names[i]->len)
-			longest = main_cmds->names[i]->len;
-	for (i = 0; i < other_cmds->cnt; i++)
-		if (longest < other_cmds->names[i]->len)
-			longest = other_cmds->names[i]->len;
-
 	if (main_cmds->cnt) {
 		const char *exec_path = git_exec_path();
 		printf("available %s in '%s'\n", title, exec_path);
 		printf("----------------");
 		mput_char('-', strlen(title) + strlen(exec_path));
 		putchar('\n');
-		pretty_print_string_list(main_cmds, longest);
+		pretty_print_string_list(main_cmds, colopts);
 		putchar('\n');
 	}
 
@@ -230,7 +217,7 @@ void list_commands(const char *title, struct cmdnames *main_cmds,
 		printf("---------------------------------------");
 		mput_char('-', strlen(title));
 		putchar('\n');
-		pretty_print_string_list(other_cmds, longest);
+		pretty_print_string_list(other_cmds, colopts);
 		putchar('\n');
 	}
 }
diff --git a/help.h b/help.h
index b6b12d5..854d2d4 100644
--- a/help.h
+++ b/help.h
@@ -25,7 +25,7 @@ extern void add_cmdname(struct cmdnames *cmds, const char *name, int len);
 /* Here we require that excludes is a sorted list. */
 extern void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes);
 extern int is_in_cmdlist(struct cmdnames *cmds, const char *name);
-extern void list_commands(const char *title,
+extern void list_commands(const char *title, unsigned int colopts,
 			  struct cmdnames *main_cmds,
 			  struct cmdnames *other_cmds);
 
-- 
1.7.8.36.g69ee2

^ permalink raw reply related	[flat|nested] 66+ messages in thread

* [PATCH v7 06/9] branch: add --column
  2012-03-13 12:09             ` [PATCH v7 05/9] help: reuse print_columns() for help -a Nguyễn Thái Ngọc Duy
@ 2012-03-13 12:09               ` Nguyễn Thái Ngọc Duy
  2012-03-13 12:09                 ` [PATCH v7 07/9] status: " Nguyễn Thái Ngọc Duy
  0 siblings, 1 reply; 66+ messages in thread
From: Nguyễn Thái Ngọc Duy @ 2012-03-13 12:09 UTC (permalink / raw)
  To: git; +Cc: Junio C Hamano, Nguyễn Thái Ngọc Duy

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 Documentation/config.txt     |    4 ++
 Documentation/git-branch.txt |    9 +++++
 Makefile                     |    2 +-
 builtin/branch.c             |   32 +++++++++++++++--
 t/t3200-branch.sh            |   77 ++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 119 insertions(+), 5 deletions(-)

diff --git a/Documentation/config.txt b/Documentation/config.txt
index aacf8ab..31878b7 100644
--- a/Documentation/config.txt
+++ b/Documentation/config.txt
@@ -847,6 +847,10 @@ column.ui::
 +
 	This option defaults to 'never'.
 
+column.branch::
+	Specify whether to output branch listing in `git branch` in columns.
+	See `column.ui` for details.
+
 commit.status::
 	A boolean to enable/disable inclusion of status information in the
 	commit message template when using an editor to prepare the commit
diff --git a/Documentation/git-branch.txt b/Documentation/git-branch.txt
index 0427e80..ba5cccb 100644
--- a/Documentation/git-branch.txt
+++ b/Documentation/git-branch.txt
@@ -10,6 +10,7 @@ SYNOPSIS
 [verse]
 'git branch' [--color[=<when>] | --no-color] [-r | -a]
 	[--list] [-v [--abbrev=<length> | --no-abbrev]]
+	[--column[=<options>] | --no-column]
 	[(--merged | --no-merged | --contains) [<commit>]] [<pattern>...]
 'git branch' [--set-upstream | --track | --no-track] [-l] [-f] <branchname> [<start-point>]
 'git branch' (-m | -M) [<oldbranch>] <newbranch>
@@ -107,6 +108,14 @@ OPTIONS
 	default to color output.
 	Same as `--color=never`.
 
+--column[=<options>]::
+--no-column::
+	Display branch listing in columns. See configuration variable
+	column.branch for option syntax.`--column` and `--no-column`
+	without options are equivalent to 'always' and 'never' respectively.
++
+This option is only applicable in non-verbose mode.
+
 -r::
 --remotes::
 	List or delete (if used with -d) the remote-tracking branches.
diff --git a/Makefile b/Makefile
index 0998f0d..320d3f8 100644
--- a/Makefile
+++ b/Makefile
@@ -2168,7 +2168,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
+builtin/branch.o 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/builtin/branch.c b/builtin/branch.c
index cb17bc3..2da0168 100644
--- a/builtin/branch.c
+++ b/builtin/branch.c
@@ -15,6 +15,8 @@
 #include "branch.h"
 #include "diff.h"
 #include "revision.h"
+#include "string-list.h"
+#include "column.h"
 
 static const char * const builtin_branch_usage[] = {
 	"git branch [options] [-r | -a] [--merged | --no-merged]",
@@ -53,6 +55,9 @@ static enum merge_filter {
 } merge_filter;
 static unsigned char merge_filter_ref[20];
 
+static struct string_list output = STRING_LIST_INIT_DUP;
+static unsigned int colopts;
+
 static int parse_branch_color_slot(const char *var, int ofs)
 {
 	if (!strcasecmp(var+ofs, "plain"))
@@ -70,6 +75,8 @@ static int parse_branch_color_slot(const char *var, int ofs)
 
 static int git_branch_config(const char *var, const char *value, void *cb)
 {
+	if (!prefixcmp(var, "column."))
+		return git_column_config(var, value, "branch", &colopts);
 	if (!strcmp(var, "color.branch")) {
 		branch_use_color = git_config_colorbool(var, value);
 		return 0;
@@ -474,7 +481,12 @@ static void print_ref_item(struct ref_item *item, int maxwidth, int verbose,
 	else if (verbose)
 		/* " f7c0c00 [ahead 58, behind 197] vcs-svn: drop obj_pool.h" */
 		add_verbose_info(&out, item, verbose, abbrev);
-	printf("%s\n", out.buf);
+	if (COL_ENABLE(colopts)) {
+		assert(!verbose && "--column and --verbose are incompatible");
+		string_list_append(&output, out.buf);
+	} else {
+		printf("%s\n", out.buf);
+	}
 	strbuf_release(&name);
 	strbuf_release(&out);
 }
@@ -727,6 +739,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
 			PARSE_OPT_LASTARG_DEFAULT | PARSE_OPT_NONEG,
 			opt_parse_merge_filter, (intptr_t) "HEAD",
 		},
+		OPT_COLUMN(0, "column", &colopts, "list branches in columns"),
 		OPT_END(),
 	};
 
@@ -749,6 +762,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
 	}
 	hashcpy(merge_filter_ref, head_sha1);
 
+
 	argc = parse_options(argc, argv, prefix, options, builtin_branch_usage,
 			     0);
 
@@ -760,12 +774,22 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
 
 	if (abbrev == -1)
 		abbrev = DEFAULT_ABBREV;
+	finalize_colopts(&colopts, -1);
+	if (verbose) {
+		if (explicitly_enable_column(colopts))
+			die(_("--column and --verbose are incompatible"));
+		colopts = 0;
+	}
 
 	if (delete)
 		return delete_branches(argc, argv, delete > 1, kinds);
-	else if (list)
-		return print_ref_list(kinds, detached, verbose, abbrev,
-				      with_commit, argv);
+	else if (list) {
+		int ret = print_ref_list(kinds, detached, verbose, abbrev,
+					 with_commit, argv);
+		print_columns(&output, colopts, NULL);
+		string_list_clear(&output, 0);
+		return ret;
+	}
 	else if (edit_description) {
 		const char *branch_name;
 		struct strbuf branch_ref = STRBUF_INIT;
diff --git a/t/t3200-branch.sh b/t/t3200-branch.sh
index dd1aceb..9f82d5e 100755
--- a/t/t3200-branch.sh
+++ b/t/t3200-branch.sh
@@ -160,6 +160,83 @@ test_expect_success 'git branch --list -d t should fail' '
 	test_path_is_missing .git/refs/heads/t
 '
 
+test_expect_success 'git branch --column' '
+	COLUMNS=81 git branch --column=column >actual &&
+	cat >expected <<\EOF &&
+  a/b/c     bam       foo       l       * master    n         o/p       r
+  abc       bar       j/k       m/m       master2   o/o       q
+EOF
+	test_cmp expected actual
+'
+
+test_expect_success 'git branch --column with an extremely long branch name' '
+	long=this/is/a/part/of/long/branch/name &&
+	long=z$long/$long/$long/$long &&
+	test_when_finished "git branch -d $long" &&
+	git branch $long &&
+	COLUMNS=80 git branch --column=column >actual &&
+	cat >expected <<EOF &&
+  a/b/c
+  abc
+  bam
+  bar
+  foo
+  j/k
+  l
+  m/m
+* master
+  master2
+  n
+  o/o
+  o/p
+  q
+  r
+  $long
+EOF
+	test_cmp expected actual
+'
+
+test_expect_success 'git branch with column.*' '
+	git config column.ui column &&
+	git config column.branch "dense" &&
+	COLUMNS=80 git branch >actual &&
+	git config --unset column.branch &&
+	git config --unset column.ui &&
+	cat >expected <<\EOF &&
+  a/b/c   bam   foo   l   * master    n     o/p   r
+  abc     bar   j/k   m/m   master2   o/o   q
+EOF
+	test_cmp expected actual
+'
+
+test_expect_success 'git branch --column -v should fail' '
+	test_must_fail git branch --column -v
+'
+
+test_expect_success 'git branch -v with column.ui ignored' '
+	git config column.ui column &&
+	COLUMNS=80 git branch -v | cut -c -10 | sed "s/ *$//" >actual &&
+	git config --unset column.ui &&
+	cat >expected <<\EOF &&
+  a/b/c
+  abc
+  bam
+  bar
+  foo
+  j/k
+  l
+  m/m
+* master
+  master2
+  n
+  o/o
+  o/p
+  q
+  r
+EOF
+	test_cmp expected actual
+'
+
 mv .git/config .git/config-saved
 
 test_expect_success 'git branch -m q q2 without config should succeed' '
-- 
1.7.8.36.g69ee2

^ permalink raw reply related	[flat|nested] 66+ messages in thread

* [PATCH v7 07/9] status: add --column
  2012-03-13 12:09               ` [PATCH v7 06/9] branch: add --column Nguyễn Thái Ngọc Duy
@ 2012-03-13 12:09                 ` Nguyễn Thái Ngọc Duy
  2012-03-13 12:09                   ` [PATCH v7 08/9] column: support piping stdout to external git-column process Nguyễn Thái Ngọc Duy
  0 siblings, 1 reply; 66+ messages in thread
From: Nguyễn Thái Ngọc Duy @ 2012-03-13 12:09 UTC (permalink / raw)
  To: git; +Cc: Junio C Hamano, Nguyễn Thái Ngọc Duy

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 Documentation/config.txt     |    4 ++++
 Documentation/git-status.txt |    7 +++++++
 Makefile                     |    2 +-
 builtin/commit.c             |    7 +++++++
 t/t7508-status.sh            |   24 ++++++++++++++++++++++++
 wt-status.c                  |   28 ++++++++++++++++++++++++++--
 wt-status.h                  |    1 +
 7 files changed, 70 insertions(+), 3 deletions(-)

diff --git a/Documentation/config.txt b/Documentation/config.txt
index 31878b7..acdd49f 100644
--- a/Documentation/config.txt
+++ b/Documentation/config.txt
@@ -851,6 +851,10 @@ column.branch::
 	Specify whether to output branch listing in `git branch` in columns.
 	See `column.ui` for details.
 
+column.status::
+	Specify whether to output untracked files in `git status` in columns.
+	See `column.ui` for details.
+
 commit.status::
 	A boolean to enable/disable inclusion of status information in the
 	commit message template when using an editor to prepare the commit
diff --git a/Documentation/git-status.txt b/Documentation/git-status.txt
index 3d51717..2f87207 100644
--- a/Documentation/git-status.txt
+++ b/Documentation/git-status.txt
@@ -77,6 +77,13 @@ configuration variable documented in linkgit:git-config[1].
 	Terminate entries with NUL, instead of LF.  This implies
 	the `--porcelain` output format if no other format is given.
 
+--column[=<options>]::
+--no-column::
+	Display untracked files in columns. See configuration variable
+	column.status for option syntax.`--column` and `--no-column`
+	without options are equivalent to 'always' and 'never'
+	respectively.
+
 
 OUTPUT
 ------
diff --git a/Makefile b/Makefile
index 320d3f8..d9c5f00 100644
--- a/Makefile
+++ b/Makefile
@@ -2168,7 +2168,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
-builtin/branch.o column.o help.o pager.o: column.h
+builtin/branch.o builtin/commit.o 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/builtin/commit.c b/builtin/commit.c
index 3714582..cc1a709 100644
--- a/builtin/commit.c
+++ b/builtin/commit.c
@@ -27,6 +27,7 @@
 #include "quote.h"
 #include "submodule.h"
 #include "gpg-interface.h"
+#include "column.h"
 
 static const char * const builtin_commit_usage[] = {
 	"git commit [options] [--] <filepattern>...",
@@ -88,6 +89,7 @@ static int quiet, verbose, no_verify, allow_empty, dry_run, renew_authorship;
 static int no_post_rewrite, allow_empty_message;
 static char *untracked_files_arg, *force_date, *ignore_submodule_arg;
 static char *sign_commit;
+static unsigned int colopts;
 
 /*
  * The default commit message cleanup mode will remove the lines
@@ -1145,6 +1147,8 @@ static int git_status_config(const char *k, const char *v, void *cb)
 {
 	struct wt_status *s = cb;
 
+	if (!prefixcmp(k, "column."))
+		return git_column_config(k, v, "status", &colopts);
 	if (!strcmp(k, "status.submodulesummary")) {
 		int is_bool;
 		s->submodule_summary = git_config_bool_or_int(k, v, &is_bool);
@@ -1210,6 +1214,7 @@ int cmd_status(int argc, const char **argv, const char *prefix)
 		{ OPTION_STRING, 0, "ignore-submodules", &ignore_submodule_arg, "when",
 		  "ignore changes to submodules, optional when: all, dirty, untracked. (Default: all)",
 		  PARSE_OPT_OPTARG, NULL, (intptr_t)"all" },
+		OPT_COLUMN(0, "column", &colopts, "list untracked files in columns"),
 		OPT_END(),
 	};
 
@@ -1223,6 +1228,8 @@ int cmd_status(int argc, const char **argv, const char *prefix)
 	argc = parse_options(argc, argv, prefix,
 			     builtin_status_options,
 			     builtin_status_usage, 0);
+	finalize_colopts(&colopts, -1);
+	s.colopts = colopts;
 
 	if (null_termination && status_format == STATUS_FORMAT_LONG)
 		status_format = STATUS_FORMAT_PORCELAIN;
diff --git a/t/t7508-status.sh b/t/t7508-status.sh
index fc57b13..8f5cfac 100755
--- a/t/t7508-status.sh
+++ b/t/t7508-status.sh
@@ -59,6 +59,30 @@ test_expect_success 'status (1)' '
 	test_i18ngrep "use \"git rm --cached <file>\.\.\.\" to unstage" output
 '
 
+test_expect_success 'status --column' '
+	COLUMNS=50 git status --column="column dense" >output &&
+	cat >expect <<\EOF &&
+# On branch master
+# Changes to be committed:
+#   (use "git reset HEAD <file>..." to unstage)
+#
+#	new file:   dir2/added
+#
+# Changes not staged for commit:
+#   (use "git add <file>..." to update what will be committed)
+#   (use "git checkout -- <file>..." to discard changes in working directory)
+#
+#	modified:   dir1/modified
+#
+# Untracked files:
+#   (use "git add <file>..." to include in what will be committed)
+#
+#	dir1/untracked dir2/untracked untracked
+#	dir2/modified  output
+EOF
+	test_cmp expect output
+'
+
 cat >expect <<\EOF
 # On branch master
 # Changes to be committed:
diff --git a/wt-status.c b/wt-status.c
index 9ffc535..c1dcf75 100644
--- a/wt-status.c
+++ b/wt-status.c
@@ -11,6 +11,7 @@
 #include "remote.h"
 #include "refs.h"
 #include "submodule.h"
+#include "column.h"
 
 static char default_wt_status_colors[][COLOR_MAXLEN] = {
 	GIT_COLOR_NORMAL, /* WT_STATUS_HEADER */
@@ -641,6 +642,8 @@ static void wt_status_print_other(struct wt_status *s,
 {
 	int i;
 	struct strbuf buf = STRBUF_INIT;
+	static struct string_list output = STRING_LIST_INIT_DUP;
+	struct column_options copts;
 
 	if (!l->nr)
 		return;
@@ -649,12 +652,33 @@ static void wt_status_print_other(struct wt_status *s,
 
 	for (i = 0; i < l->nr; i++) {
 		struct string_list_item *it;
+		const char *path;
 		it = &(l->items[i]);
+		path = quote_path(it->string, strlen(it->string),
+				  &buf, s->prefix);
+		if (COL_ENABLE(s->colopts)) {
+			string_list_append(&output, path);
+			continue;
+		}
 		status_printf(s, color(WT_STATUS_HEADER, s), "\t");
 		status_printf_more(s, color(WT_STATUS_UNTRACKED, s),
-			"%s\n", quote_path(it->string, strlen(it->string),
-					    &buf, s->prefix));
+				   "%s\n", path);
 	}
+
+	strbuf_release(&buf);
+	if (!COL_ENABLE(s->colopts))
+		return;
+
+	strbuf_addf(&buf, "%s#\t%s",
+		    color(WT_STATUS_HEADER, s),
+		    color(WT_STATUS_UNTRACKED, s));
+	memset(&copts, 0, sizeof(copts));
+	copts.padding = 1;
+	copts.indent = buf.buf;
+	if (want_color(s->use_color))
+		copts.nl = GIT_COLOR_RESET "\n";
+	print_columns(&output, s->colopts, &copts);
+	string_list_clear(&output, 0);
 	strbuf_release(&buf);
 }
 
diff --git a/wt-status.h b/wt-status.h
index 682b4c8..6dd7207 100644
--- a/wt-status.h
+++ b/wt-status.h
@@ -56,6 +56,7 @@ struct wt_status {
 	enum untracked_status_type show_untracked_files;
 	const char *ignore_submodule_arg;
 	char color_palette[WT_STATUS_MAXSLOT][COLOR_MAXLEN];
+	int colopts;
 
 	/* These are computed during processing of the individual sections */
 	int commitable;
-- 
1.7.8.36.g69ee2

^ permalink raw reply related	[flat|nested] 66+ messages in thread

* [PATCH v7 08/9] column: support piping stdout to external git-column process
  2012-03-13 12:09                 ` [PATCH v7 07/9] status: " Nguyễn Thái Ngọc Duy
@ 2012-03-13 12:09                   ` Nguyễn Thái Ngọc Duy
  2012-03-13 12:09                     ` [PATCH v7 09/9] tag: add --column Nguyễn Thái Ngọc Duy
  0 siblings, 1 reply; 66+ messages in thread
From: Nguyễn Thái Ngọc Duy @ 2012-03-13 12:09 UTC (permalink / raw)
  To: git; +Cc: Junio C Hamano, Nguyễn Thái Ngọc Duy

For too complicated output handling, it'd be easier to just spawn
git-column and redirect stdout to it. This patch provides helpers
to do that.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 column.c |   69 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 column.h |    3 ++
 2 files changed, 72 insertions(+), 0 deletions(-)

diff --git a/column.c b/column.c
index 3aac28b..1e545a0 100644
--- a/column.c
+++ b/column.c
@@ -2,6 +2,7 @@
 #include "column.h"
 #include "string-list.h"
 #include "parse-options.h"
+#include "run-command.h"
 #include "utf8.h"
 
 #define XY2LINEAR(d, x, y) (COL_LAYOUT((d)->colopts) == COL_COLUMN ? \
@@ -364,3 +365,71 @@ int parseopt_column_callback(const struct option *opt,
 
 	return 0;
 }
+
+static int fd_out = -1;
+static struct child_process column_process;
+
+int run_column_filter(int colopts, const struct column_options *opts)
+{
+	const char *av[10];
+	int ret, ac = 0;
+	struct strbuf sb_colopt  = STRBUF_INIT;
+	struct strbuf sb_width   = STRBUF_INIT;
+	struct strbuf sb_padding = STRBUF_INIT;
+
+	if (fd_out != -1)
+		return -1;
+
+	av[ac++] = "column";
+	strbuf_addf(&sb_colopt, "--raw-mode=%d", colopts);
+	av[ac++] = sb_colopt.buf;
+	if (opts && opts->width) {
+		strbuf_addf(&sb_width, "--width=%d", opts->width);
+		av[ac++] = sb_width.buf;
+	}
+	if (opts && opts->indent) {
+		av[ac++] = "--indent";
+		av[ac++] = opts->indent;
+	}
+	if (opts && opts->padding) {
+		strbuf_addf(&sb_padding, "--padding=%d", opts->padding);
+		av[ac++] = sb_padding.buf;
+	}
+	av[ac] = NULL;
+
+	fflush(stdout);
+	memset(&column_process, 0, sizeof(column_process));
+	column_process.in = -1;
+	column_process.out = dup(1);
+	column_process.git_cmd = 1;
+	column_process.argv = av;
+
+	ret = start_command(&column_process);
+
+	strbuf_release(&sb_colopt);
+	strbuf_release(&sb_width);
+	strbuf_release(&sb_padding);
+
+	if (ret)
+		return -2;
+
+	fd_out = dup(1);
+	close(1);
+	dup2(column_process.in, 1);
+	close(column_process.in);
+	return 0;
+}
+
+int stop_column_filter(void)
+{
+	if (fd_out == -1)
+		return -1;
+
+	fflush(stdout);
+	close(1);
+	finish_command(&column_process);
+	dup2(fd_out, 1);
+	close(fd_out);
+	fd_out = -1;
+	return 0;
+}
diff --git a/column.h b/column.h
index b3e979f..b6872fe 100644
--- a/column.h
+++ b/column.h
@@ -36,4 +36,7 @@ extern int finalize_colopts(unsigned int *colopts, int stdout_is_tty);
 extern void print_columns(const struct string_list *list, unsigned int colopts,
 			  const struct column_options *opts);
 
+extern int run_column_filter(int colopts, const struct column_options *);
+extern int stop_column_filter(void);
+
 #endif
-- 
1.7.8.36.g69ee2

^ permalink raw reply related	[flat|nested] 66+ messages in thread

* [PATCH v7 09/9] tag: add --column
  2012-03-13 12:09                   ` [PATCH v7 08/9] column: support piping stdout to external git-column process Nguyễn Thái Ngọc Duy
@ 2012-03-13 12:09                     ` Nguyễn Thái Ngọc Duy
  2012-03-13 12:11                       ` [PATCH v7 10/9] ls-files: support --column Nguyễn Thái Ngọc Duy
  0 siblings, 1 reply; 66+ messages in thread
From: Nguyễn Thái Ngọc Duy @ 2012-03-13 12:09 UTC (permalink / raw)
  To: git; +Cc: Junio C Hamano, Nguyễn Thái Ngọc Duy

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 Documentation/config.txt  |    4 ++++
 Documentation/git-tag.txt |    9 +++++++++
 Makefile                  |    2 +-
 builtin/tag.c             |   27 ++++++++++++++++++++++++---
 t/t7004-tag.sh            |   44 ++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 82 insertions(+), 4 deletions(-)

diff --git a/Documentation/config.txt b/Documentation/config.txt
index acdd49f..62c128c 100644
--- a/Documentation/config.txt
+++ b/Documentation/config.txt
@@ -855,6 +855,10 @@ column.status::
 	Specify whether to output untracked files in `git status` in columns.
 	See `column.ui` for details.
 
+column.tag::
+	Specify whether to output tag listing in `git tag` in columns.
+	See `column.ui` for details.
+
 commit.status::
 	A boolean to enable/disable inclusion of status information in the
 	commit message template when using an editor to prepare the commit
diff --git a/Documentation/git-tag.txt b/Documentation/git-tag.txt
index 8d32b9a..e36a7c3 100644
--- a/Documentation/git-tag.txt
+++ b/Documentation/git-tag.txt
@@ -13,6 +13,7 @@ SYNOPSIS
 	<tagname> [<commit> | <object>]
 'git tag' -d <tagname>...
 'git tag' [-n[<num>]] -l [--contains <commit>] [--points-at <object>]
+	[--column[=<options>] | --no-column] [<pattern>...]
 	[<pattern>...]
 'git tag' -v <tagname>...
 
@@ -84,6 +85,14 @@ OPTIONS
 	using fnmatch(3)).  Multiple patterns may be given; if any of
 	them matches, the tag is shown.
 
+--column[=<options>]::
+--no-column::
+	Display tag listing in columns. See configuration variable
+	column.tag for option syntax.`--column` and `--no-column`
+	without options are equivalent to 'always' and 'never' respectively.
++
+This option is only applicable when listing tags without annotation lines.
+
 --contains <commit>::
 	Only list tags which contain the specified commit.
 
diff --git a/Makefile b/Makefile
index d9c5f00..65fc6b9 100644
--- a/Makefile
+++ b/Makefile
@@ -2168,7 +2168,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
-builtin/branch.o builtin/commit.o column.o help.o pager.o: column.h
+builtin/branch.o builtin/commit.o builtin/tag.o 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/builtin/tag.c b/builtin/tag.c
index fe7e5e5..7104adf 100644
--- a/builtin/tag.c
+++ b/builtin/tag.c
@@ -16,6 +16,7 @@
 #include "revision.h"
 #include "gpg-interface.h"
 #include "sha1-array.h"
+#include "column.h"
 
 static const char * const git_tag_usage[] = {
 	"git tag [-a|-s|-u <key-id>] [-f] [-m <msg>|-F <file>] <tagname> [<head>]",
@@ -33,6 +34,7 @@ struct tag_filter {
 };
 
 static struct sha1_array points_at;
+static unsigned int colopts;
 
 static int match_pattern(const char **patterns, const char *ref)
 {
@@ -263,6 +265,8 @@ static int git_tag_config(const char *var, const char *value, void *cb)
 	int status = git_gpg_config(var, value, cb);
 	if (status)
 		return status;
+	if (!prefixcmp(var, "column."))
+		return git_column_config(var, value, "tag", &colopts);
 	return git_default_config(var, value, cb);
 }
 
@@ -459,6 +463,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
 		OPT_STRING('u', "local-user", &keyid, "key-id",
 					"use another key to sign the tag"),
 		OPT__FORCE(&force, "replace the tag if exists"),
+		OPT_COLUMN(0, "column", &colopts, "show tag list in columns"),
 
 		OPT_GROUP("Tag listing options"),
 		{
@@ -495,9 +500,25 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
 
 	if (list + delete + verify > 1)
 		usage_with_options(git_tag_usage, options);
-	if (list)
-		return list_tags(argv, lines == -1 ? 0 : lines,
-				 with_commit);
+	finalize_colopts(&colopts, -1);
+	if (list && lines != -1) {
+		if (explicitly_enable_column(colopts))
+			die(_("--column and -n are incompatible"));
+		colopts = 0;
+	}
+	if (list) {
+		int ret;
+		if (COL_ENABLE(colopts)) {
+			struct column_options copts;
+			memset(&copts, 0, sizeof(copts));
+			copts.padding = 2;
+			run_column_filter(colopts, &copts);
+		}
+		ret = list_tags(argv, lines == -1 ? 0 : lines, with_commit);
+		if (COL_ENABLE(colopts))
+			stop_column_filter();
+		return ret;
+	}
 	if (lines != -1)
 		die(_("-n option is only allowed with -l."));
 	if (with_commit)
diff --git a/t/t7004-tag.sh b/t/t7004-tag.sh
index f8c247a..5189446 100755
--- a/t/t7004-tag.sh
+++ b/t/t7004-tag.sh
@@ -263,6 +263,50 @@ test_expect_success 'tag -l can accept multiple patterns' '
 	test_cmp expect actual
 '
 
+test_expect_success 'listing tags in column' '
+	COLUMNS=40 git tag -l --column=row >actual &&
+	cat >expected <<\EOF &&
+a1      aa1     cba     t210    t211
+v0.2.1  v1.0    v1.0.1  v1.1.3
+EOF
+	test_cmp expected actual
+'
+
+test_expect_success 'listing tags in column with column.*' '
+	git config column.tag row &&
+	git config column.ui dense &&
+	COLUMNS=40 git tag -l >actual &&
+	git config --unset column.ui &&
+	git config --unset column.tag &&
+	cat >expected <<\EOF &&
+a1      aa1   cba     t210    t211
+v0.2.1  v1.0  v1.0.1  v1.1.3
+EOF
+	test_cmp expected actual
+'
+
+test_expect_success 'listing tag with -n --column should fail' '
+	test_must_fail git tag --column -n
+'
+
+test_expect_success 'listing tags -n in column with column.ui ignored' '
+	git config column.ui "row dense" &&
+	COLUMNS=40 git tag -l -n >actual &&
+	git config --unset column.ui &&
+	cat >expected <<\EOF &&
+a1              Foo
+aa1             Foo
+cba             Foo
+t210            Foo
+t211            Foo
+v0.2.1          Foo
+v1.0            Foo
+v1.0.1          Foo
+v1.1.3          Foo
+EOF
+	test_cmp expected actual
+'
+
 # creating and verifying lightweight tags:
 
 test_expect_success \
-- 
1.7.8.36.g69ee2

^ permalink raw reply related	[flat|nested] 66+ messages in thread

* [PATCH v7 10/9] ls-files: support --column
  2012-03-13 12:09                     ` [PATCH v7 09/9] tag: add --column Nguyễn Thái Ngọc Duy
@ 2012-03-13 12:11                       ` Nguyễn Thái Ngọc Duy
  2012-03-13 12:11                         ` [PATCH v7 11/9] column: support "denser" mode Nguyễn Thái Ngọc Duy
  0 siblings, 1 reply; 66+ messages in thread
From: Nguyễn Thái Ngọc Duy @ 2012-03-13 12:11 UTC (permalink / raw)
  To: git; +Cc: Nguyễn Thái Ngọc Duy

.. because ls-files is a good show case to the next patches..

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 builtin/ls-files.c |    8 ++++++++
 1 files changed, 8 insertions(+), 0 deletions(-)

diff --git a/builtin/ls-files.c b/builtin/ls-files.c
index 7cff175..e191f4f 100644
--- a/builtin/ls-files.c
+++ b/builtin/ls-files.c
@@ -13,6 +13,7 @@
 #include "parse-options.h"
 #include "resolve-undo.h"
 #include "string-list.h"
+#include "column.h"
 
 static int abbrev;
 static int show_deleted;
@@ -35,6 +36,7 @@ static int error_unmatch;
 static char *ps_matched;
 static const char *with_tree;
 static int exc_given;
+static unsigned int colopts;
 
 static const char *tag_cached = "";
 static const char *tag_unmerged = "";
@@ -496,6 +498,7 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)
 		OPT_STRING(0, "with-tree", &with_tree, "tree-ish",
 			"pretend that paths removed since <tree-ish> are still present"),
 		OPT__ABBREV(&abbrev),
+		OPT_COLUMN(0, "column", &colopts, "list branches in columns"),
 		OPT_BOOLEAN(0, "debug", &debug_mode, "show debugging data"),
 		OPT_END()
 	};
@@ -514,6 +517,7 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)
 
 	argc = parse_options(argc, argv, prefix, builtin_ls_files_options,
 			ls_files_usage, 0);
+	finalize_colopts(&colopts, -1);
 	if (show_tag || show_valid_bit) {
 		tag_cached = "H ";
 		tag_unmerged = "M ";
@@ -575,7 +579,11 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)
 			die("ls-files --with-tree is incompatible with -s or -u");
 		overlay_tree_on_cache(with_tree, max_prefix);
 	}
+	if (COL_ENABLE(colopts))
+		run_column_filter(colopts, NULL);
 	show_files(&dir);
+	if (COL_ENABLE(colopts))
+		stop_column_filter();
 	if (show_resolve_undo)
 		show_ru_info();
 
-- 
1.7.8.36.g69ee2

^ permalink raw reply related	[flat|nested] 66+ messages in thread

* [PATCH v7 11/9] column: support "denser" mode
  2012-03-13 12:11                       ` [PATCH v7 10/9] ls-files: support --column Nguyễn Thái Ngọc Duy
@ 2012-03-13 12:11                         ` Nguyễn Thái Ngọc Duy
  2012-03-13 12:11                           ` [PATCH v7 12/9] column: support grouping entries Nguyễn Thái Ngọc Duy
  0 siblings, 1 reply; 66+ messages in thread
From: Nguyễn Thái Ngọc Duy @ 2012-03-13 12:11 UTC (permalink / raw)
  To: git; +Cc: Nguyễn Thái Ngọc Duy

Sometimes a few long entries in the listing would stretch out columns,
wasting space. If these entries are cut out, the columns could become
smaller, hence more columns.

This new mode does that by looking for the longest entry, if cutting the
list before that entry results in much denser layout, then the entry
will be cut out and printed in a separate line. The remaining will put
in a new layout.

Multiple tables with different column width might be unpleasant to
look at, especially if the tables are really short. But on the other
hand it could be quite handy. For example,
"COLUMNS=80 ./git ls-files --column=dense -- '*.[ch]'" takes 415
lines, while "denser" only takes 223 because it break the layout at
contrib/credential/osxkeychain/git-credential-osxkeychain.c
and relayout again:

abspath.c                  builtin/rm.c
advice.c                   builtin/send-pack.c
<snip>
builtin/rev-parse.c        connected.h
builtin/revert.c           contrib/convert-objects/convert-objects.c

contrib/credential/osxkeychain/git-credential-osxkeychain.c

contrib/examples/builtin-fetch--tool.c refs.c                   xdiff/xutils.c
contrib/svn-fe/svn-fe.c                refs.h                   xdiff/xutils.h
convert.c                              remote-curl.c            zlib.c
convert.h                              remote.c
<snip>
reflog-walk.c                          xdiff/xprepare.h
reflog-walk.h                          xdiff/xtypes.h

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 column.c |   74 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---
 column.h |    1 +
 2 files changed, 71 insertions(+), 4 deletions(-)

diff --git a/column.c b/column.c
index 1e545a0..80eefa0 100644
--- a/column.c
+++ b/column.c
@@ -152,10 +152,55 @@ static int display_cell(struct column_data *data, int initial_width,
 	return 0;
 }
 
+/*
+ * Attempt to put the longest cell into a separate line, see if it
+ * improves the layout
+ */
+static int break_long_line(const struct column_data *old_data)
+{
+	struct column_data data;
+	struct string_list faked_list;
+	int initial_width, x, y, i, item = 0, row1, row2;
+	char *empty_cell;
+
+	memcpy(&data, old_data, sizeof(data));
+	for (i = 0; i < data.list->nr; i++)
+		if (data.len[i] > data.len[item])
+			item = i;
+	data.list = &faked_list;
+	data.width = NULL;
+	faked_list = *old_data->list;
+
+	faked_list.nr = item + 1;
+	layout(&data, &initial_width);
+	shrink_columns(&data);
+	row1 = data.rows;
+
+	faked_list.nr = item;
+	layout(&data, &initial_width);
+	shrink_columns(&data);
+	row2 = data.rows;
+
+	if (row1 - row2 < 3)
+		return -1;
+
+	empty_cell = xmalloc(initial_width + 1);
+	memset(empty_cell, ' ', initial_width);
+	empty_cell[initial_width] = '\0';
+	for (y = 0; y < data.rows; y++) {
+		for (x = 0; x < data.cols; x++)
+			if (display_cell(&data, initial_width, empty_cell, x, y))
+				break;
+	}
+	free(data.width);
+	free(empty_cell);
+	return item;
+}
+
 /* Display COL_COLUMN or COL_ROW */
-static void display_table(const struct string_list *list,
-			  unsigned int colopts,
-			  const struct column_options *opts)
+static int display_table(const struct string_list *list,
+			 unsigned int colopts,
+			 const struct column_options *opts)
 {
 	struct column_data data;
 	int x, y, i, initial_width;
@@ -174,6 +219,19 @@ static void display_table(const struct string_list *list,
 
 	if (colopts & COL_DENSE)
 		shrink_columns(&data);
+	if (colopts & COL_DENSER) {
+		i = break_long_line(&data);
+		if (i != -1) {
+			printf("%s%s" "%s%s%s" "%s%s",
+			       indent, nl,
+			       indent, list->items[i].string, nl,
+			       indent, nl);
+			free(data.len);
+			free(data.width);
+			return i + 1;
+		}
+		shrink_columns(&data);
+	}
 
 	empty_cell = xmalloc(initial_width + 1);
 	memset(empty_cell, ' ', initial_width);
@@ -187,12 +245,15 @@ static void display_table(const struct string_list *list,
 	free(data.len);
 	free(data.width);
 	free(empty_cell);
+	return list->nr;
 }
 
 void print_columns(const struct string_list *list, unsigned int colopts,
 		   const struct column_options *opts)
 {
 	struct column_options nopts;
+	int processed;
+	struct string_list l = *list;
 
 	if (!list->nr)
 		return;
@@ -213,7 +274,11 @@ void print_columns(const struct string_list *list, unsigned int colopts,
 		break;
 	case COL_ROW:
 	case COL_COLUMN:
-		display_table(list, colopts, &nopts);
+		while (l.nr &&
+		       (processed = display_table(&l, colopts, &nopts)) < l.nr) {
+			l.items += processed;
+			l.nr -= processed;
+		}
 		break;
 	default:
 		die("BUG: invalid layout mode %d", COL_LAYOUT(colopts));
@@ -252,6 +317,7 @@ static int parse_option(const char *arg, int len, unsigned int *colopts,
 		{ "column", COL_COLUMN,   COL_LAYOUT_MASK },
 		{ "row",    COL_ROW,      COL_LAYOUT_MASK },
 		{ "dense",  COL_DENSE,    0 },
+		{ "denser", COL_DENSER,   0 },
 	};
 	int i;
 
diff --git a/column.h b/column.h
index b6872fe..dbc5da2 100644
--- a/column.h
+++ b/column.h
@@ -6,6 +6,7 @@
 #define COL_PARSEOPT      0x0040   /* --column is given from cmdline */
 #define COL_DENSE         0x0080   /* Shrink columns when possible,
 				      making space for more columns */
+#define COL_DENSER        0x0100
 
 #define COL_ENABLE(c) ((c) & COL_ENABLE_MASK)
 #define COL_DISABLED      0x0000   /* must be zero */
-- 
1.7.8.36.g69ee2

^ permalink raw reply related	[flat|nested] 66+ messages in thread

* [PATCH v7 12/9] column: support grouping entries
  2012-03-13 12:11                         ` [PATCH v7 11/9] column: support "denser" mode Nguyễn Thái Ngọc Duy
@ 2012-03-13 12:11                           ` Nguyễn Thái Ngọc Duy
  0 siblings, 0 replies; 66+ messages in thread
From: Nguyễn Thái Ngọc Duy @ 2012-03-13 12:11 UTC (permalink / raw)
  To: git; +Cc: Nguyễn Thái Ngọc Duy

If many entries share the same prefix (e.g. "git ls-files Documentation/"),
cutting out the prefix helps put more information on the same space.

If "group" is specified, the list of entries will be searched for
largest non-overlapping rectangles of text. Estimation is done on each
rectangle to see if there are any savings in rows if we group them.
Groups are printed first, then the remaining as the last group.

Handling the remaining part this way may not be ideal but I don't want
to split all directories like "ls -R". That takes too many lines.
Maybe I should prepend ".../" to all grouped items to make it clear
they are grouped.  There's also problem with ansi escape codes that
I'll need to handle if this sounds like a good way to go.

This code may be used for diffstat too (e.g. when most of the diff is
in Documentation/).

For demonstration, this is what
"COLUMNS=80 git ls-files --column=group -- '*.[ch]'" looks like

builtin/:
add.c                gc.c                 read-tree.c
annotate.c           grep.c               receive-pack.c
<snip>
for-each-ref.c       prune.c              verify-tag.c
fsck.c               push.c               write-tree.c

compat/:
basename.c                  regex/regexec.c
bswap.h                     setenv.c
<snip>
regex/regex_internal.c      win32mmap.c
regex/regex_internal.h      winansi.c

contrib/:
convert-objects/convert-objects.c
credential/osxkeychain/git-credential-osxkeychain.c
examples/builtin-fetch--tool.c
svn-fe/svn-fe.c

vcs-svn/:
fast_export.c    line_buffer.h    sliding_window.c svndiff.h
fast_export.h    repo_tree.c      sliding_window.h svndump.c
line_buffer.c    repo_tree.h      svndiff.c        svndump.h

xdiff/:
xdiff.h      xemit.c      xinclude.h   xpatience.c  xtypes.h
xdiffi.c     xemit.h      xmacros.h    xprepare.c   xutils.c
xdiffi.h     xhistogram.c xmerge.c     xprepare.h   xutils.h

...:
abspath.c                  pack-check.c
advice.c                   pack-refs.c
<snip>
object.c                   xdiff-interface.h
object.h                   zlib.c

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 column.c |  229 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 column.h |    1 +
 2 files changed, 227 insertions(+), 3 deletions(-)

diff --git a/column.c b/column.c
index 80eefa0..8214a9b 100644
--- a/column.c
+++ b/column.c
@@ -19,6 +19,11 @@ struct column_data {
 	int *width;	      /* index to the longest row in column */
 };
 
+struct area {
+	int start, end;		/* in string_list */
+	int size;
+};
+
 /* return length of 's' in letters, ANSI escapes stripped */
 static int item_length(unsigned int colopts, const char *s)
 {
@@ -223,9 +228,9 @@ static int display_table(const struct string_list *list,
 		i = break_long_line(&data);
 		if (i != -1) {
 			printf("%s%s" "%s%s%s" "%s%s",
-			       indent, nl,
-			       indent, list->items[i].string, nl,
-			       indent, nl);
+			       opts->indent, opts->nl,
+			       opts->indent, list->items[i].string, opts->nl,
+			       opts->indent, opts->nl);
 			free(data.len);
 			free(data.width);
 			return i + 1;
@@ -248,6 +253,218 @@ static int display_table(const struct string_list *list,
 	return list->nr;
 }
 
+/*
+ * Find out the contiguous list of entries sharing the same directory
+ * prefix that nr * (prefix_len - skip) is largest, where nr is the
+ * number of entries and prefix_len is the shared directory prefix's
+ * length.
+ */
+static int largest_block(const struct string_list *list, int start, int skip, int *len)
+{
+	const char *str = list->items[start].string;
+	const char *slash;
+	int largest_area = 0;
+
+	for (slash = str + strlen(str) - 1; slash > str + skip; slash--) {
+		int i, area;
+		if (*slash != '/')
+			continue;
+		for (i = start; i < list->nr; i++) {
+			const char *s = list->items[i].string;
+			if (strlen(s) < slash + 1 - str ||
+			    memcmp(str + skip, s + skip, slash + 1 - (str + skip)))
+				break;
+		}
+		area = (i - start) * (slash + 1 - str - skip);
+		if (area > largest_area) {
+			largest_area = area;
+			*len = i - start;
+		}
+	}
+	return largest_area;
+}
+
+static int area_size_cmp(const void *a, const void *b)
+{
+	const struct area *area1 = a;
+	const struct area *area2 = b;
+	return area2->size - area1->size;
+}
+
+/*
+ * Make a sorted list of non-overlapping blocks, largest ones first
+ */
+static struct area *find_large_blocks(const struct string_list *list, int *nr_p)
+{
+	int i, nr = 0, alloc = 16;
+	struct area *areas = xmalloc(sizeof(*areas) * alloc);
+	struct area last;
+	memset(&last, 0, sizeof(last));
+
+	for (i = 0; i < list->nr; i++) {
+		int len, size = largest_block(list, i, 0, &len);
+		if (!size || len == 1)
+			continue;
+		/* the new found area is overlapped with the old one,
+		   but smaller, skip it */
+		if (i < last.end) {
+			if (size < last.size)
+				continue;
+			last.start = i;
+			last.end = i + len;
+			last.size = size;
+			continue;
+		}
+		if (last.size) {
+			if (nr + 1 < alloc)
+				ALLOC_GROW(areas, nr + 1, alloc);
+			areas[nr++] = last;
+		}
+		last.start = i;
+		last.end = i + len;
+		last.size = size;
+	}
+	if (last.size) {
+		if (nr + 1 >= alloc)
+			ALLOC_GROW(areas, nr + 1, alloc);
+		areas[nr++] = last;
+	}
+	qsort(areas, nr, sizeof(*areas), area_size_cmp);
+	*nr_p = nr;
+	return areas;
+}
+
+static int area_start_cmp(const void *a, const void *b)
+{
+	const struct area *area1 = a;
+	const struct area *area2 = b;
+	return area1->start - area2->start;
+}
+
+/*
+ * Assume list is split into two tables: one from "start" to "stop",
+ * where all strings are truncated "skip" bytes, the other the rest of
+ * the strings. Calculate how many rows required if all cells of each
+ * table are of the same width.
+ */
+static int split_layout_gain(const struct string_list *list, int *lengths,
+			     const struct column_options *opts,
+			     int start, int stop, int skip)
+{
+	int i, width0, width1, width2, cols, rows0, rows1;
+	int indent = strlen(opts->indent);
+
+	width0 = width1 = width2 = 0;
+	for (i = 0; i < list->nr; i++) {
+		int len = lengths[i];
+		if (width0 < len)
+			width0 = len;
+		if (i >= start && i < stop) {
+			len -= skip;
+			if (width2 < len)
+				width2 = len;
+		} else {
+			if (width1 < len)
+				width1 = len;
+		}
+	}
+
+	width0 += opts->padding;
+	cols = (opts->width - indent) / width0;
+	if (cols == 0)
+		cols = 1;
+	rows0 = DIV_ROUND_UP(list->nr, cols);
+
+	width1 += opts->padding;
+	cols = (opts->width - indent) / width1;
+	if (cols == 0)
+		cols = 1;
+	rows1 = DIV_ROUND_UP(list->nr - (stop - start), cols);
+
+	width2 += opts->padding;
+	cols = (opts->width - indent) / width2;
+	if (cols == 0)
+		cols = 1;
+	rows1 += DIV_ROUND_UP(stop - start, cols);
+	return rows0 - rows1;
+}
+
+static void group_by_prefix(const struct string_list *list, unsigned int colopts,
+			    const struct column_options *opts)
+{
+	int i, nr;
+	struct area *areas = find_large_blocks(list, &nr);
+	struct string_list new_list = STRING_LIST_INIT_NODUP;
+	struct area *dst;
+	int *len;
+
+	assert(colopts & COL_GROUP);
+	/* avoid inifinite loop when calling print_columns again */
+	colopts &= ~COL_GROUP;
+
+	len = xmalloc(sizeof(*len) * list->nr);
+	for (i = 0; i < list->nr; i++)
+		len[i] = item_length(colopts, list->items[i].string);
+
+	/*
+	 * Calculate and see if there is any saving when print this as
+	 * a group. Base our calculation on non-dense mode even if
+	 * users want dense output because the calculation would be
+	 * less expensive.
+	 */
+	dst = areas;
+	for (i = 0; i < nr; i++) {
+		struct area *area = areas + i;
+		int rows, skip = area->size / (area->end - area->start);
+		rows = split_layout_gain(list, len, opts,
+					 area->start, area->end, skip);
+
+		if (rows > 3) {
+			if (areas + i != dst)
+				*dst = *area;
+			dst++;
+		}
+	}
+	free(len);
+
+	nr = dst - areas;
+	if (!nr) {
+		print_columns(list, colopts, opts);
+		return;
+	}
+	qsort(areas, nr, sizeof(*areas), area_start_cmp);
+
+	/*
+	 * We now have list of worthy groups, sorted by offset. Print
+	 * group by group, then the rest.
+	 */
+	for (i = 0; i < nr; i++) {
+		struct area *area = areas + i;
+		int j, skip = area->size / (area->end - area->start);
+
+		for (j = area->start; j < area->end; j++)
+			string_list_append(&new_list,
+					   list->items[j].string + skip);
+		printf("\n%.*s:\n", skip, list->items[area->start].string);
+		print_columns(&new_list, colopts, opts);
+		string_list_clear(&new_list, 0);
+	}
+
+	printf("\n%s:\n", "...");
+	for (i = 0; i < nr; i++) {
+		struct area *area = areas + i;
+		int j;
+		for (j = i ? area[-1].end : 0; j < area->start; j++)
+			string_list_append(&new_list, list->items[j].string);
+	}
+	for (i = areas[nr-1].end; i < list->nr; i++)
+		string_list_append(&new_list, list->items[i].string);
+	print_columns(&new_list, colopts, opts);
+	string_list_clear(&new_list, 0);
+
+	free(areas);
+}
+
 void print_columns(const struct string_list *list, unsigned int colopts,
 		   const struct column_options *opts)
 {
@@ -264,6 +481,11 @@ void print_columns(const struct string_list *list, unsigned int colopts,
 	nopts.nl = opts && opts->nl ? opts->nl : "\n";
 	nopts.padding = opts ? opts->padding : 1;
 	nopts.width = opts && opts->width ? opts->width : term_columns() - 1;
+
+	if (colopts & COL_GROUP) {
+		group_by_prefix(list, colopts, &nopts);
+		return;
+	}
 	if (!COL_ENABLE(colopts)) {
 		display_plain(list, "", "\n");
 		return;
@@ -318,6 +540,7 @@ static int parse_option(const char *arg, int len, unsigned int *colopts,
 		{ "row",    COL_ROW,      COL_LAYOUT_MASK },
 		{ "dense",  COL_DENSE,    0 },
 		{ "denser", COL_DENSER,   0 },
+		{ "group",  COL_GROUP,    0 },
 	};
 	int i;
 
diff --git a/column.h b/column.h
index dbc5da2..f3934cf 100644
--- a/column.h
+++ b/column.h
@@ -7,6 +7,7 @@
 #define COL_DENSE         0x0080   /* Shrink columns when possible,
 				      making space for more columns */
 #define COL_DENSER        0x0100
+#define COL_GROUP         0x0200
 
 #define COL_ENABLE(c) ((c) & COL_ENABLE_MASK)
 #define COL_DISABLED      0x0000   /* must be zero */
-- 
1.7.8.36.g69ee2

^ permalink raw reply related	[flat|nested] 66+ messages in thread

* Re: [PATCH v7 01/9] Add column layout skeleton and git-column
  2012-03-13 12:09     ` [PATCH v7 01/9] Add column layout skeleton and git-column Nguyễn Thái Ngọc Duy
  2012-03-13 12:09       ` [PATCH v7 02/9] Stop starting pager recursively Nguyễn Thái Ngọc Duy
@ 2012-03-13 22:24       ` Junio C Hamano
  2012-03-14 11:17         ` Nguyen Thai Ngoc Duy
  1 sibling, 1 reply; 66+ messages in thread
From: Junio C Hamano @ 2012-03-13 22:24 UTC (permalink / raw)
  To: Nguyễn Thái Ngọc Duy; +Cc: git

Nguyễn Thái Ngọc Duy  <pclouds@gmail.com> writes:

> +#define COL_ENABLE(c) ((c) & COL_ENABLE_MASK)

That is a misleading name for a boolean macro.  It looked as if this

> +	assert(COL_ENABLE(colopts) != COL_AUTO);

was asking the helper to *enable* the column machinery with the given set
of option in colopts, and expecting the helper to answer how it enabled
("I took the 'automatic' decision path").  But that is not what is
happening.

Unfortunately, COL_ENABLED?(c) is not an option, but this seriously needs
a better name to avoid reader confusion.

Regarding the "denser" mode, I very much like the simplicity of the idea.
I was wondering if a solution that aims for the maximum density that does
not shuffle the original order of items would end up taking the output
from "fmt" and distributing the words on each line evenly to the width,
which would be totally unusable. Your "punt at an item that does not fit
and restart from there" is simple and seems to work well.

I haven't formed an opinion on your "grouping" mode yet.  The hardcoded
slash hierarchy delimiter somewhat bothers me, but I haven't thought it
deeply enough to judge if it is worth making it more generic. My gut
feeling is that '/' probably is OK.

Thanks.

^ permalink raw reply	[flat|nested] 66+ messages in thread

* Re: [PATCH v7 01/9] Add column layout skeleton and git-column
  2012-03-13 22:24       ` [PATCH v7 01/9] Add column layout skeleton and git-column Junio C Hamano
@ 2012-03-14 11:17         ` Nguyen Thai Ngoc Duy
  2012-03-14 18:29           ` Junio C Hamano
  0 siblings, 1 reply; 66+ messages in thread
From: Nguyen Thai Ngoc Duy @ 2012-03-14 11:17 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git

2012/3/14 Junio C Hamano <gitster@pobox.com>:
> Nguyễn Thái Ngọc Duy  <pclouds@gmail.com> writes:
>
>> +#define COL_ENABLE(c) ((c) & COL_ENABLE_MASK)
>
> That is a misleading name for a boolean macro.  It looked as if this
>
>> +     assert(COL_ENABLE(colopts) != COL_AUTO);
>
> was asking the helper to *enable* the column machinery with the given set
> of option in colopts, and expecting the helper to answer how it enabled
> ("I took the 'automatic' decision path").  But that is not what is
> happening.
>
> Unfortunately, COL_ENABLED?(c) is not an option, but this seriously needs
> a better name to avoid reader confusion.

I'm running out of names. Suggestions are welcome.

> I haven't formed an opinion on your "grouping" mode yet.  The hardcoded
> slash hierarchy delimiter somewhat bothers me, but I haven't thought it
> deeply enough to judge if it is worth making it more generic. My gut
> feeling is that '/' probably is OK.

The column user should be able to decide how to display the group
line. I rely a lot on coloring, and I was already thinking about
giving group line a highlight color, which must be configurable
because different column user uses different base colors.
-- 
Duy

^ permalink raw reply	[flat|nested] 66+ messages in thread

* Re: [PATCH v7 01/9] Add column layout skeleton and git-column
  2012-03-14 11:17         ` Nguyen Thai Ngoc Duy
@ 2012-03-14 18:29           ` Junio C Hamano
  0 siblings, 0 replies; 66+ messages in thread
From: Junio C Hamano @ 2012-03-14 18:29 UTC (permalink / raw)
  To: Nguyen Thai Ngoc Duy; +Cc: git

Nguyen Thai Ngoc Duy <pclouds@gmail.com> writes:

> 2012/3/14 Junio C Hamano <gitster@pobox.com>:
>> Nguyễn Thái Ngọc Duy  <pclouds@gmail.com> writes:
>>
>>> +#define COL_ENABLE(c) ((c) & COL_ENABLE_MASK)
>>
>> That is a misleading name for a boolean macro.  It looked as if this
>>
>>> +     assert(COL_ENABLE(colopts) != COL_AUTO);
>>
>> was asking the helper to *enable* the column machinery with the given set
>> of option in colopts, and expecting the helper to answer how it enabled
>> ("I took the 'automatic' decision path").  But that is not what is
>> happening.
> ...
> I'm running out of names. Suggestions are welcome.

An obvious interface to expose to API users (as opposed to internal
details to the implementation) would be "extern bool COL_ENABLED(opts);",
no?

^ permalink raw reply	[flat|nested] 66+ messages in thread

end of thread, other threads:[~2012-03-14 18:30 UTC | newest]

Thread overview: 66+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-02-25 11:41 [PATCH v6 00/11] Column display Nguyễn Thái Ngọc Duy
2012-02-25 11:41 ` [PATCH v6 01/11] column: add API to print items in columns Nguyễn Thái Ngọc Duy
2012-02-25 11:41 ` [PATCH v6 02/11] Add git-column and column mode parsing Nguyễn Thái Ngọc Duy
2012-02-27 20:09   ` Ramsay Jones
2012-02-28 11:00     ` Nguyen Thai Ngoc Duy
2012-02-25 11:41 ` [PATCH v6 03/11] Stop starting pager recursively Nguyễn Thái Ngọc Duy
2012-02-25 11:41 ` [PATCH v6 04/11] column: add columnar layout Nguyễn Thái Ngọc Duy
2012-02-25 11:41 ` [PATCH v6 05/11] column: support columns with different widths Nguyễn Thái Ngọc Duy
2012-02-26 23:12   ` Junio C Hamano
2012-02-25 11:41 ` [PATCH v6 06/11] column: add column.ui for default column output settings Nguyễn Thái Ngọc Duy
2012-02-27  6:20   ` Junio C Hamano
2012-02-27  7:04     ` Nguyen Thai Ngoc Duy
2012-02-25 11:41 ` [PATCH v6 07/11] help: reuse print_columns() for help -a Nguyễn Thái Ngọc Duy
2012-02-25 11:41 ` [PATCH v6 08/11] branch: add --column Nguyễn Thái Ngọc Duy
2012-02-25 11:41 ` [PATCH v6 09/11] status: " Nguyễn Thái Ngọc Duy
2012-02-25 11:41 ` [PATCH v6 10/11] column: support piping stdout to external git-column process Nguyễn Thái Ngọc Duy
2012-02-25 11:41 ` [PATCH v6 11/11] tag: add --column Nguyễn Thái Ngọc Duy
2012-02-26 23:02 ` [PATCH v6 00/11] Column display Junio C Hamano
2012-02-27  0:40   ` Nguyen Thai Ngoc Duy
2012-02-27  1:37     ` Junio C Hamano
2012-02-27  7:46       ` Junio C Hamano
2012-02-27  8:14         ` Nguyen Thai Ngoc Duy
2012-02-28 11:58 ` [PATCH v7 00/10] " Nguyễn Thái Ngọc Duy
2012-02-28 11:58   ` [PATCH v7 01/10] Add git-column for columnar display Nguyễn Thái Ngọc Duy
2012-02-28 18:10     ` Junio C Hamano
2012-03-02 12:36       ` Nguyen Thai Ngoc Duy
2012-02-28 11:58   ` [PATCH v7 02/10] Stop starting pager recursively Nguyễn Thái Ngọc Duy
2012-02-28 18:13     ` Junio C Hamano
2012-02-28 19:10       ` Junio C Hamano
2012-02-29  1:54         ` Nguyen Thai Ngoc Duy
2012-02-29  3:37           ` Junio C Hamano
2012-02-29  3:40             ` Nguyen Thai Ngoc Duy
2012-02-29  4:51               ` Junio C Hamano
2012-02-28 11:58   ` [PATCH v7 03/10] column: add columnar layout Nguyễn Thái Ngọc Duy
2012-02-28 18:22     ` Junio C Hamano
2012-02-28 11:58   ` [PATCH v7 04/10] column: add dense layout support Nguyễn Thái Ngọc Duy
2012-02-28 18:27     ` Junio C Hamano
2012-03-02 12:47       ` Nguyen Thai Ngoc Duy
2012-03-02 17:37         ` Junio C Hamano
2012-02-28 11:58   ` [PATCH v7 05/10] column: add column.ui for default column output settings Nguyễn Thái Ngọc Duy
2012-02-28 18:44     ` Junio C Hamano
2012-02-28 11:58   ` [PATCH v7 06/10] help: reuse print_columns() for help -a Nguyễn Thái Ngọc Duy
2012-02-28 11:58   ` [PATCH v7 07/10] branch: add --column Nguyễn Thái Ngọc Duy
2012-02-28 11:58   ` [PATCH v7 08/10] status: " Nguyễn Thái Ngọc Duy
2012-02-28 11:58   ` [PATCH v7 09/10] column: support piping stdout to external git-column process Nguyễn Thái Ngọc Duy
2012-02-28 11:58   ` [PATCH v7 10/10] tag: add --column Nguyễn Thái Ngọc Duy
2012-03-02 11:25   ` [PATCH v7 00/10] Column display Thomas Rast
2012-03-11  7:02     ` Nguyen Thai Ngoc Duy
2012-03-12  6:02       ` Junio C Hamano
2012-03-13 12:09   ` [PATCH v7 00/9] " Nguyễn Thái Ngọc Duy
2012-03-13 12:08     ` Nguyen Thai Ngoc Duy
2012-03-13 12:09     ` [PATCH v7 01/9] Add column layout skeleton and git-column Nguyễn Thái Ngọc Duy
2012-03-13 12:09       ` [PATCH v7 02/9] Stop starting pager recursively Nguyễn Thái Ngọc Duy
2012-03-13 12:09         ` [PATCH v7 03/9] column: add columnar layout Nguyễn Thái Ngọc Duy
2012-03-13 12:09           ` [PATCH v7 04/9] column: add dense layout support Nguyễn Thái Ngọc Duy
2012-03-13 12:09             ` [PATCH v7 05/9] help: reuse print_columns() for help -a Nguyễn Thái Ngọc Duy
2012-03-13 12:09               ` [PATCH v7 06/9] branch: add --column Nguyễn Thái Ngọc Duy
2012-03-13 12:09                 ` [PATCH v7 07/9] status: " Nguyễn Thái Ngọc Duy
2012-03-13 12:09                   ` [PATCH v7 08/9] column: support piping stdout to external git-column process Nguyễn Thái Ngọc Duy
2012-03-13 12:09                     ` [PATCH v7 09/9] tag: add --column Nguyễn Thái Ngọc Duy
2012-03-13 12:11                       ` [PATCH v7 10/9] ls-files: support --column Nguyễn Thái Ngọc Duy
2012-03-13 12:11                         ` [PATCH v7 11/9] column: support "denser" mode Nguyễn Thái Ngọc Duy
2012-03-13 12:11                           ` [PATCH v7 12/9] column: support grouping entries Nguyễn Thái Ngọc Duy
2012-03-13 22:24       ` [PATCH v7 01/9] Add column layout skeleton and git-column Junio C Hamano
2012-03-14 11:17         ` Nguyen Thai Ngoc Duy
2012-03-14 18:29           ` Junio C Hamano

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.