suffix-based colorization for numeric value diffs

This commit is contained in:
Adrian Sampson 2013-02-02 12:24:05 -08:00
parent d050211c2c
commit 44d195a119
3 changed files with 39 additions and 7 deletions

View file

@ -414,6 +414,29 @@ def colordiff(a, b, highlight='red'):
else:
return unicode(a), unicode(b)
def color_diff_suffix(a, b, highlight='red'):
"""Colorize the differing suffix between two strings."""
a, b = unicode(a), unicode(b)
if not config['color']:
return a, b
# Fast path.
if a == b:
return a, b
# Find the longest common prefix.
first_diff = None
for i in range(min(len(a), len(b))):
if a[i] != b[i]:
first_diff = i
break
else:
first_diff = min(len(a), len(b))
# Colorize from the first difference on.
return a[:first_diff] + colorize(highlight, a[first_diff:]), \
b[:first_diff] + colorize(highlight, b[first_diff:])
def get_path_formats():
"""Get the configuration's path formats as a list of query/template
pairs.

View file

@ -201,9 +201,11 @@ def show_change(cur_artist, cur_album, match):
# Track number change.
if item.track not in (track_info.index, track_info.medium_index):
cur_track, new_track = unicode(item.track), format_index(track_info)
lhs_track, rhs_track = ui.colordiff(cur_track, new_track)
lhs += u' (#%s)' % lhs_track
rhs += u' (#%s)' % rhs_track
lhs_track, rhs_track = ui.color_diff_suffix(cur_track, new_track)
templ = ui.colorize('red', u' (#') + u'{0}' + \
ui.colorize('red', u')')
lhs += templ.format(lhs_track)
rhs += templ.format(rhs_track)
lhs_width += len(cur_track) + 4
# Length change.
@ -212,9 +214,12 @@ def show_change(cur_artist, cur_album, match):
config['ui']['length_diff_thresh'].as_number():
cur_length = ui.human_seconds_short(item.length)
new_length = ui.human_seconds_short(track_info.length)
lhs_length, rhs_length = ui.colordiff(cur_length, new_length)
lhs += u' (%s)' % lhs_length
rhs += u' (%s)' % rhs_length
lhs_length, rhs_length = ui.color_diff_suffix(cur_length,
new_length)
templ = ui.colorize('red', u' (') + u'{0}' + \
ui.colorize('red', u')')
lhs += templ.format(lhs_length)
rhs += templ.format(rhs_length)
lhs_width += len(cur_length) + 3
if lhs != rhs:

View file

@ -30,10 +30,14 @@ Other new stuff:
bad: "low" or "none" recommendations or when choosing a candidate
other than the first.
* Album listings in the importer UI now show the release medium (CD, LP,
* Album listings in the importer UI now show the release medium (CD, Vinyl,
etc.). Thanks to Peter Schnebel.
* :doc:`/plugins/mpdupdate`: Sends an update message whenever *anything* in the
database changes---not just when importing. Thanks to Dang Mai Hai.
* When the importer UI shows a difference in track numbers or durations, they
are now colorized based on the *suffixes* that differ. For example, when
showing the difference between 2:01 and 2:09, only the last digit will be
highlighted.
* The importer UI no longer shows a change when the track length difference is
less than 10 seconds. (This threshold was previously 2 seconds.)
* Fix an error when migrating the ``.beetsstate`` file on Windows.