Subject: [PATCH 06/16] Add core.column

From: Nguyễn Thái Ngọc Duy <[email-masked]>
Date: Wed, 9 Feb 2011 19:24:34 +0700

FIXME: should probably go as color.* (i.e. column.*)

Signed-off-by: Nguyễn Thái Ngọc Duy <[email-masked]>
---
 Documentation/config.txt |   11 ++++++++
 cache.h                  |    1 +
 column.c                 |   64 ++++++++++++++++++++++++++++++++++++++++++++++
 column.h                 |    1 +
 config.c                 |    4 +++
 environment.c            |    1 +
 6 files changed, 82 insertions(+), 0 deletions(-)

diff --git a/Documentation/config.txt b/Documentation/config.txt
index c5e1835..45b5f5b 100644
--- a/Documentation/config.txt
+++ b/Documentation/config.txt
@@ -567,6 +567,17 @@ core.sparseCheckout::
 	Enable "sparse checkout" feature. See section "Sparse checkout" in
 	linkgit:git-read-tree[1] for more information.
 
+core.column::
+	Specify whether a command should output in columns. Only commands
+	that support `--column` will be affected by this. This variable
+	consists of a list of tokens separated by either spaces or commas:
+	`never` (do not output in columns), `auto` (output in columns if
+	the output is to a terminal), `always` (always output in
+	columns), `column` (fill column before row), `row` (fill row
+	before column), `dense` (unequal column width).
+	Setting `--column` or `--no-column` will override this
+	variable. This option defaults to never.
+
 add.ignore-errors::
 add.ignoreErrors::
 	Tells 'git add' to continue adding files when some files cannot be
diff --git a/cache.h b/cache.h
index d83d68c..b370657 100644
--- a/cache.h
+++ b/cache.h
@@ -559,6 +559,7 @@ extern int read_replace_refs;
 extern int fsync_object_files;
 extern int core_preload_index;
 extern int core_apply_sparse_checkout;
+extern int core_column;
 
 enum safe_crlf {
 	SAFE_CRLF_FALSE = 0,
diff --git a/column.c b/column.c
index e7facf4..615a698 100644
--- a/column.c
+++ b/column.c
@@ -206,3 +206,67 @@ void display_columns(const struct string_list *list, int mode, int width, int pa
 		die("BUG: invalid mode %d", MODE(mode));
 	}
 }
+
+static int parse_column_option(const char *arg, int len, int *mode)
+{
+	int negate = !strncmp(arg, "no", 2);
+
+	if (negate)
+		arg += 2;
+
+	if (!negate && !strncmp(arg, "column", 6)) {
+		*mode &= ~COL_MODE;
+		*mode |= COL_MODE_COLUMN;
+		return 0;
+	}
+	else if (!negate && !strncmp(arg, "row", 3)) {
+		*mode &= ~COL_MODE;
+		*mode |= COL_MODE_ROW;
+		return 0;
+	}
+	else if (!strncmp(arg, "dense", 5)) {
+		if (negate)
+			*mode &= ~COL_DENSE;
+		else
+			*mode |= COL_DENSE;
+		return 0;
+	}
+	else
+		return error("unsupported style '%s'", arg);
+	return 0;
+}
+
+int git_config_column(const char *var, const char *value, int stdout_is_tty)
+{
+	const char *sep = " ,";
+	int enable = 0;
+
+	while (*value) {
+		int len = strcspn(value, sep);
+		if (len) {
+			if (!strncasecmp(value, "never", 5))
+				enable = 0;
+			else if (!strncasecmp(value, "always", 6))
+				enable = 1;
+			else if (!strncasecmp(value, "auto", 4)) {
+				if (stdout_is_tty < 0)
+					stdout_is_tty = isatty(1);
+				if (stdout_is_tty || (pager_in_use() && pager_use_color))
+					enable = 1;
+			}
+			else if (!parse_column_option(value, len, &core_column))
+				;
+			else
+				return -1;
+
+			value += len;
+		}
+		value += strspn(value, sep);
+	}
+	if (!enable)
+		core_column = 0;
+	else if (MODE(core_column) == COL_MODE_PLAIN)
+		core_column |= COL_MODE_COLUMN;
+
+	return 0;
+}
diff --git a/column.h b/column.h
index cef354d..0749793 100644
--- a/column.h
+++ b/column.h
@@ -10,5 +10,6 @@
 
 extern int term_columns(void);
 extern void display_columns(const struct string_list *list, int mode, int width, int padding, const char *indent);
+extern int git_config_column(const char *var, const char *value, int stdout_is_tty);
 
 #endif
diff --git a/config.c b/config.c
index 625e051..68a432a 100644
--- a/config.c
+++ b/config.c
@@ -9,6 +9,7 @@
 #include "exec_cmd.h"
 #include "strbuf.h"
 #include "quote.h"
+#include "column.h"
 
 #define MAXNAME (256)
 
@@ -660,6 +661,9 @@ static int git_default_core_config(const char *var, const char *value)
 		return 0;
 	}
 
+	if (!strcmp(var, "core.column"))
+		return git_config_column(var, value, -1);
+
 	/* Add other config variables here and to Documentation/config.txt. */
 	return 0;
 }
diff --git a/environment.c b/environment.c
index 9564475..b420ea2 100644
--- a/environment.c
+++ b/environment.c
@@ -56,6 +56,7 @@ char *notes_ref_name;
 int grafts_replace_parents = 1;
 int core_apply_sparse_checkout;
 struct startup_info *startup_info;
+int core_column;
 
 /* Parallel index stat data preload? */
 int core_preload_index = 0;
-- 
1.7.2.2