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: "Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>
Subject: [PATCH 7/7] diff --highlight-words: actually highlight words
Date: Thu, 31 Dec 2015 19:37:37 +0700	[thread overview]
Message-ID: <1451565457-18756-8-git-send-email-pclouds@gmail.com> (raw)
In-Reply-To: <1451565457-18756-1-git-send-email-pclouds@gmail.com>

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 diff.c | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 50 insertions(+), 3 deletions(-)

diff --git a/diff.c b/diff.c
index 47d22e3..68a847d 100644
--- a/diff.c
+++ b/diff.c
@@ -457,6 +457,50 @@ struct tagged_pointer {
 	enum pointer_tag tag;
 };
 
+static void emit_tagged_line(struct diff_options *o, const char *set,
+			     const char *line, int len,
+			     struct tagged_pointer *begin,
+			     struct tagged_pointer *end)
+{
+	struct tagged_pointer *current;
+	FILE *file = o->file;
+	const char *last_color = set;
+
+	for (current = begin; current < end; current++) {
+		struct tagged_pointer *next = current + 1;
+		const char *start = current->str;
+		const char *end = next->str;
+		const char *color = NULL;
+
+		switch (current->tag) {
+		case TAG_END_WORD:
+			color = set;
+			break;
+
+		case TAG_BEGIN_OLD_WORD:
+			color = GIT_COLOR_BG_RED GIT_COLOR_YELLOW;
+			break;
+
+		case TAG_BEGIN_NEW_WORD:
+			color = GIT_COLOR_BG_GREEN GIT_COLOR_BLUE;
+			break;
+
+		default:
+			break;
+		}
+
+		if (color && color != last_color) {
+			if (color == set)
+				fputs(GIT_COLOR_RESET, file);
+			fputs(color, file);
+			last_color = color;
+		}
+		while (end > start && end[-1] == '\n')
+			end--;
+		fwrite(start, end - start, 1, file);
+	}
+}
+
 static void emit_line_0(struct diff_options *o, const char *set, const char *reset,
 			int first, const char *line, int len,
 			struct tagged_pointer *begin,
@@ -487,7 +531,10 @@ static void emit_line_0(struct diff_options *o, const char *set, const char *res
 		fputs(set, file);
 		if (!nofirst)
 			fputc(first, file);
-		fwrite(line, len, 1, file);
+		if (begin)
+			emit_tagged_line(o, set, line, len, begin, end);
+		else
+			fwrite(line, len, 1, file);
 		fputs(reset, file);
 	}
 	if (has_trailing_carriage_return)
@@ -531,7 +578,7 @@ static void emit_line_checked(const char *reset,
 			ws = NULL;
 	}
 
-	if (!ws)
+	if (!ws || begin)
 		emit_line_0(ecbdata->opt, set, reset, sign,
 			    line, len, begin, end);
 	else if (sign == '+' && new_blank_line_at_eof(ecbdata, line, len))
@@ -1312,7 +1359,7 @@ static void diff_words_flush_unified(struct emit_callback *ecb,
 		emit_line_checked(reset, ecb, begin_line->str,
 				  end_line->str - begin_line->str,
 				  color, ws_error_highlight, sign,
-				  NULL, NULL);
+				  begin_line, end_line);
 	}
 	b->mark_nr = 0;
 }
-- 
2.3.0.rc1.137.g477eb31

      parent reply	other threads:[~2015-12-31 12:38 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-12-31 12:37 A failed attempt to integrate diff-highlight to the core Nguyễn Thái Ngọc Duy
2015-12-31 12:37 ` [PATCH 1/7] diff.c: keep all word diff structs together Nguyễn Thái Ngọc Duy
2015-12-31 12:37 ` [PATCH 2/7] diff.c: refactor diff_words_append() Nguyễn Thái Ngọc Duy
2015-12-31 12:37 ` [PATCH 3/7] diff --color-words: another special diff case Nguyễn Thái Ngọc Duy
2015-12-31 12:37 ` [PATCH 4/7] diff.c: refactor fn_out_diff_words_write_helper() Nguyễn Thái Ngọc Duy
2015-12-31 12:37 ` [PATCH 5/7] diff: unified diff with colored words, step 1, unified diff only Nguyễn Thái Ngọc Duy
2015-12-31 12:37 ` [PATCH 6/7] diff.c: add new arguments to emit_line_0() Nguyễn Thái Ngọc Duy
2015-12-31 12:37 ` Nguyễn Thái Ngọc Duy [this message]

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=1451565457-18756-8-git-send-email-pclouds@gmail.com \
    --to=pclouds@gmail.com \
    --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).