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


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

diff --git a/column.c b/column.c
index be4e07c..fcff2fd 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, set, name_len;
 
diff --git a/column.h b/column.h
index 52181d4..2255851 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 */
 
 struct column_options {
 	int width;
diff --git a/t/t9002-column.sh b/t/t9002-column.sh
index cffb029..23d340e 100755
--- a/t/t9002-column.sh
+++ b/t/t9002-column.sh
@@ -71,6 +71,17 @@ EOF
 	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 +121,15 @@ EOF
 	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

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

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

Reply instructions:

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

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

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

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

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

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

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