diff --git a/beets/ui/__init__.py b/beets/ui/__init__.py index 956252505..1957f79dc 100644 --- a/beets/ui/__init__.py +++ b/beets/ui/__init__.py @@ -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. diff --git a/beets/ui/commands.py b/beets/ui/commands.py index 92e48b711..c48484aa5 100644 --- a/beets/ui/commands.py +++ b/beets/ui/commands.py @@ -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: diff --git a/docs/changelog.rst b/docs/changelog.rst index 727f66477..ed1278eec 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -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.