git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Adam Simpkins <adam@adamsimpkins.net>
To: git@vger.kernel.org
Cc: Adam Simpkins <adam@adamsimpkins.net>
Subject: [PATCH] graph API: eliminate unnecessary indentation
Date: Mon,  5 May 2008 00:57:03 -0700	[thread overview]
Message-ID: <1209974223-2875-1-git-send-email-adam@adamsimpkins.net> (raw)
In-Reply-To: <1209897414-10091-4-git-send-email-adam@adamsimpkins.net>

This change improves the calculation of the amount of horizontal
padding, so that there is always exactly 1 space of padding.
Previously, most commits had 3 spaces of padding, but commits that
didn't have any children in the graph had only 1 space of padding.

Signed-off-by: Adam Simpkins <adam@adamsimpkins.net>
---

This fixes the issue reported by Ping Yin.

 graph.c |   66 +++++++++++++++++++++++++++++++++++++++++++++++++-------------
 1 files changed, 52 insertions(+), 14 deletions(-)

diff --git a/graph.c b/graph.c
index b575d10..809a582 100644
--- a/graph.c
+++ b/graph.c
@@ -61,6 +61,12 @@ struct git_graph {
 	 */
 	int num_parents;
 	/*
+	 * The width of the graph output for this commit.
+	 * All rows for this commit are padded to this width, so that
+	 * messages printed after the graph output are aligned.
+	 */
+	int width;
+	/*
 	 * The next expansion row to print
 	 * when state is GRAPH_PRE_COMMIT
 	 */
@@ -207,13 +213,48 @@ static void graph_insert_into_new_columns(struct git_graph *graph,
 	graph->num_new_columns++;
 }
 
+static void graph_update_width(struct git_graph *graph,
+			       int is_commit_in_existing_columns)
+{
+	/*
+	 * Compute the width needed to display the graph for this commit.
+	 * This is the maximum width needed for any row.  All other rows
+	 * will be padded to this width.
+	 *
+	 * Compute the number of columns in the widest row:
+	 * Count each existing column (graph->num_columns), and each new
+	 * column added by this commit.
+	 */
+	int max_cols = graph->num_columns + graph->num_parents;
+
+	/*
+	 * Even if the current commit has no parents, it still takes up a
+	 * column for itself.
+	 */
+	if (graph->num_parents < 1)
+		max_cols++;
+
+	/*
+	 * We added a column for the the current commit as part of
+	 * graph->num_parents.  If the current commit was already in
+	 * graph->columns, then we have double counted it.
+	 */
+	if (is_commit_in_existing_columns)
+		max_cols--;
+
+	/*
+	 * Each column takes up 2 spaces
+	 */
+	graph->width = max_cols * 2;
+}
+
 static void graph_update_columns(struct git_graph *graph)
 {
 	struct commit_list *parent;
 	struct column *tmp_columns;
 	int max_new_columns;
 	int mapping_idx;
-	int i, seen_this;
+	int i, seen_this, is_commit_in_columns;
 
 	/*
 	 * Swap graph->columns with graph->new_columns
@@ -259,11 +300,13 @@ static void graph_update_columns(struct git_graph *graph)
 	 */
 	seen_this = 0;
 	mapping_idx = 0;
+	is_commit_in_columns = 1;
 	for (i = 0; i <= graph->num_columns; i++) {
 		struct commit *col_commit;
 		if (i == graph->num_columns) {
 			if (seen_this)
 				break;
+			is_commit_in_columns = 0;
 			col_commit = graph->commit;
 		} else {
 			col_commit = graph->columns[i].commit;
@@ -290,6 +333,11 @@ static void graph_update_columns(struct git_graph *graph)
 	while (graph->mapping_size > 1 &&
 	       graph->mapping[graph->mapping_size - 1] < 0)
 		graph->mapping_size--;
+
+	/*
+	 * Compute graph->width for this commit
+	 */
+	graph_update_width(graph, is_commit_in_columns);
 }
 
 void graph_update(struct git_graph *graph, struct commit *commit)
@@ -368,22 +416,12 @@ static void graph_pad_horizontally(struct git_graph *graph, struct strbuf *sb)
 	 *
 	 * This way, fields printed to the right of the graph will remain
 	 * aligned for the entire commit.
-	 *
-	 * This computation results in 3 extra space to the right in most
-	 * cases, but only 1 extra space if the commit doesn't have any
-	 * children that have already been displayed in the graph (i.e.,
-	 * if the current commit isn't in graph->columns).
 	 */
-	size_t extra;
-	size_t final_width = graph->num_columns + graph->num_parents;
-	if (graph->num_parents < 1)
-		final_width++;
-	final_width *= 2;
-
-	if (sb->len >= final_width)
+	int extra;
+	if (sb->len >= graph->width)
 		return;
 
-	extra = final_width - sb->len;
+	extra = graph->width - sb->len;
 	strbuf_addf(sb, "%*s", extra, "");
 }
 
-- 
1.5.3.6

  reply	other threads:[~2008-05-05  7:57 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-05-04 10:36 [PATCH 0/3] log --graph and rev-list --graph Adam Simpkins
2008-05-04 10:36 ` [PATCH 1/3] revision API: split parent rewriting and parent printing options Adam Simpkins
2008-05-04 10:36   ` [PATCH 2/3] Add history graph API Adam Simpkins
2008-05-04 10:36     ` [PATCH 3/3] log and rev-list: add --graph option Adam Simpkins
2008-05-05  7:57       ` Adam Simpkins [this message]
2008-05-05 11:38         ` [PATCH] graph API: eliminate unnecessary indentation Ping Yin
2008-05-06  6:41       ` [PATCH 3/3] log and rev-list: add --graph option Junio C Hamano
2008-05-06  7:01         ` Adam Simpkins
2008-05-06  6:41     ` [PATCH 2/3] Add history graph API Junio C Hamano
2008-05-04 11:06 ` [PATCH] bash: Add more option completions for 'git log' Teemu Likonen
2008-05-05  2:13 ` [PATCH 0/3] log --graph and rev-list --graph Ping Yin
2008-05-05  6:19   ` Adam Simpkins
2008-05-06 19:03 ` Teemu Likonen

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=1209974223-2875-1-git-send-email-adam@adamsimpkins.net \
    --to=adam@adamsimpkins.net \
    --cc=git@vger.kernel.org \
    /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).