From 2063436ccc1101424c16dbb0a00a74b4e4cf502b Mon Sep 17 00:00:00 2001 From: Jarrod Johnson Date: Tue, 15 Aug 2017 16:11:38 -0400 Subject: [PATCH] Reduce context of collate diff diff mode is less useful with superfluous context. Suppress context to focus more on differences. --- confluent_client/confluent/textgroup.py | 28 ++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/confluent_client/confluent/textgroup.py b/confluent_client/confluent/textgroup.py index 4c4a7a4a..2960bbaa 100644 --- a/confluent_client/confluent/textgroup.py +++ b/confluent_client/confluent/textgroup.py @@ -54,15 +54,37 @@ def _colorize_line(orig, mask): return newline +def near_diff(idx, diffdata): + maxidx = idx + 3 + if maxidx > len(diffdata) -1: + maxidx = len(diffdata) - 1 + idx = idx - 3 + if idx < 0: + idx = 0 + while idx <= maxidx: + if len(diffdata[idx]) > 0 and diffdata[idx][0] in ('+', '-'): + return True + idx += 1 + return False + + def colordiff(first, second): diffdata = list(difflib.ndiff(first, second)) + quiet = True for i in range(len(diffdata)): if i < len(diffdata) - 1 and diffdata[i + 1].startswith('?'): + if quiet: + quiet = False + yield '@@' yield _colorize_line(diffdata[i], diffdata[i + 1]) - elif diffdata[i].startswith('?'): - continue - else: + elif not diffdata[i].startswith('?') and near_diff(i, diffdata): + if quiet: + quiet = False + yield '@@' yield diffdata[i] + elif not diffdata[i].startswith('?'): + quiet = True + class GroupedData(object):