git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>
To: git@vger.kernel.org
Cc: "Junio C Hamano" <gitster@pobox.com>,
	"Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>
Subject: [PATCH v7 03/9] column: add columnar layout
Date: Tue, 13 Mar 2012 19:09:15 +0700	[thread overview]
Message-ID: <c439d91ff126e2a9d7fe627de7346e19801dd6d4.1331638663.git.pclouds@gmail.com> (raw)
In-Reply-To: <dd6e9c2189fb4502aaf9f7368cb7f13216961cfa.1331638663.git.pclouds@gmail.com>

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

  reply	other threads:[~2012-03-13 12:06 UTC|newest]

Thread overview: 66+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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         ` Nguyễn Thái Ngọc Duy [this message]
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

Reply instructions:

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

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

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

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

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

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

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