Skip to content

Commit

Permalink
Fixed diff colorization
Browse files Browse the repository at this point in the history
Fixed minor, but annoying, glitch in the colorization of diff output.  This
only works on supporting TTYs.
  • Loading branch information
lowell80 committed Jun 5, 2019
1 parent a9e87da commit 153fba3
Showing 1 changed file with 17 additions and 15 deletions.
32 changes: 17 additions & 15 deletions ksconf/conf/delta.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ def compare_stanzas(a, b, stanza_name):
return [DiffOp(DIFF_OP_EQUAL, DiffStanza("stanza", stanza_name), a, b) ]
elif not b:
# A only
return [ DiffOp(DIFF_OP_DELETE, DiffStanza("stanza", stanza_name), None, a) ]
return [ DiffOp(DIFF_OP_DELETE, DiffStanza("stanza", stanza_name), a, None) ]
elif not a:
# B only
return [ DiffOp(DIFF_OP_INSERT, DiffStanza("stanza", stanza_name), b, None) ]
return [ DiffOp(DIFF_OP_INSERT, DiffStanza("stanza", stanza_name), None, b) ]
else:
return list(_compare_stanzas(a, b, stanza_name))

Expand All @@ -72,9 +72,9 @@ def _compare_stanzas(a, b, stanza_name):

# Level 2 - Key comparisons
for key in kv_a:
yield DiffOp(DIFF_OP_DELETE, DiffStzKey("key", stanza_name, key), None, a[key])
yield DiffOp(DIFF_OP_DELETE, DiffStzKey("key", stanza_name, key), a[key], None)
for key in kv_b:
yield DiffOp(DIFF_OP_INSERT, DiffStzKey("key", stanza_name, key), b[key], None)
yield DiffOp(DIFF_OP_INSERT, DiffStzKey("key", stanza_name, key), None, b[key])
for key in kv_common:
a_ = a[key]
b_ = b[key]
Expand Down Expand Up @@ -155,10 +155,10 @@ def compare_cfgs(a, b, allow_level0=True):
delta.extend(_compare_stanzas(a[stanza], b[stanza], stanza))
elif stanza in a:
# A only
delta.append(DiffOp(DIFF_OP_DELETE, DiffStanza("stanza", stanza), None, a[stanza]))
delta.append(DiffOp(DIFF_OP_DELETE, DiffStanza("stanza", stanza), a[stanza], None))
else:
# B only
delta.append(DiffOp(DIFF_OP_INSERT, DiffStanza("stanza", stanza), b[stanza], None))
delta.append(DiffOp(DIFF_OP_INSERT, DiffStanza("stanza", stanza), None, b[stanza]))
return delta


Expand Down Expand Up @@ -231,11 +231,13 @@ def write_key(key, value, prefix_=" "):
if is_multiline(value):
write_multiline_key(key, value, prefix_)
else:
if key.startswith("#-"):
template = "{0}{2}\n"
else:
template = "{0}{1} = {2}\n"
stream.write(template.format(prefix_, key, value))
with tc:
tc.color(_diff_color_mapping.get(prefix_))
if key.startswith("#-"):
template = "{0}{2}\n"
else:
template = "{0}{1} = {2}\n"
stream.write(template.format(prefix_, key, value))

def write_multiline_key(key, value, prefix_=" "):
with tc:
Expand Down Expand Up @@ -301,9 +303,9 @@ def f(v):
for op in diffs:
if isinstance(op.location, DiffStanza):
if op.tag in (DIFF_OP_DELETE, DIFF_OP_REPLACE):
show_value(op.b, op.location.stanza, None, "-")
show_value(op.a, op.location.stanza, None, "-")
if op.tag in (DIFF_OP_INSERT, DIFF_OP_REPLACE):
show_value(op.a, op.location.stanza, None, "+")
show_value(op.b, op.location.stanza, None, "+")
continue # pragma: no cover (peephole optimization)

if op.location.stanza != last_stanza:
Expand All @@ -316,9 +318,9 @@ def f(v):
last_stanza = op.location.stanza

if op.tag == DIFF_OP_INSERT:
show_value(op.a, op.location.stanza, op.location.key, "+")
show_value(op.b, op.location.stanza, op.location.key, "+")
elif op.tag == DIFF_OP_DELETE:
show_value(op.b, op.location.stanza, op.location.key, "-")
show_value(op.a, op.location.stanza, op.location.key, "-")
elif op.tag == DIFF_OP_REPLACE:
if is_multiline(op.a) or is_multiline(op.b):
show_multiline_diff(op.a, op.b, op.location.key)
Expand Down

0 comments on commit 153fba3

Please sign in to comment.