Commits

Tai Lee  committed 3f51bc7

Improved change alignment. Align "->" when possible, otherwise split across two lines.

  • Participants
  • Parent commits 2935423

Comments (0)

Files changed (2)

File beets/ui/commands.py

     # Tracks.
     pairs = match.mapping.items()
     pairs.sort(key=lambda (_, track_info): track_info.index)
+
+    def do_lengths_differ(item, track_info):
+        return item.length and track_info.length and \
+            abs(item.length - track_info.length) > 2.0
+
+    def do_tracks_differ(item, track_info):
+        return item.track not in (track_info.index, track_info.medium_index)
+
+    def get_title(item):
+        # Return filename (non-colorized) when title is not set.
+        if not item.title.strip():
+            return displayable_path(os.path.basename(item.path))
+        return item.title.strip()
+
+    def get_pad_length(pairs, col_width):
+        pad_len = 0
+        for item, track_info in pairs:
+            title = get_title(item)
+            tracks_differ = do_tracks_differ(item, track_info)
+            # Will this track be displayed?
+            if title != track_info.title or tracks_differ or \
+                    do_lengths_differ(item, track_info):
+                # Include track when it differs.
+                if tracks_differ:
+                    title = '%s (%s)' % (title, item.track)
+                pad_len = max([pad_len, len(title)])
+                if pad_len > col_width:
+                    return 0
+        return pad_len
+
+    def get_term_width():
+        import fcntl
+        import struct
+        import termios
+        buf = fcntl.ioctl(0, termios.TIOCGWINSZ, ' '*4)
+        height, width = struct.unpack('hh', buf)
+        return width
+
+    # Calculate terminal width, max column width and padding length.
+    col_width = (get_term_width() - len(''.join([' * ', ' -> ']))) / 2
+    pad_len = get_pad_length(pairs, col_width)
+
     for item, track_info in pairs:
         # Get displayable LHS and RHS values.
+        cur_title = get_title(item)
+        new_title = track_info.title
         cur_track = unicode(item.track)
         new_track = format_index(track_info)
-        tracks_differ = item.track not in (track_info.index,
-                                           track_info.medium_index)
-        cur_title = item.title
-        new_title = track_info.title
-        if item.length and track_info.length:
-            cur_length = ui.colorize('red',
-                                     ui.human_seconds_short(item.length))
-            new_length = ui.colorize('red',
-                                     ui.human_seconds_short(track_info.length))
+        tracks_differ = do_tracks_differ(item, track_info)
 
         # Colorize changes.
         cur_title, new_title = ui.colordiff(cur_title, new_title)
         cur_track = ui.colorize('red', cur_track)
         new_track = ui.colorize('red', new_track)
 
-        # Show filename (non-colorized) when title is not set.
-        if not item.title.strip():
-            cur_title = displayable_path(os.path.basename(item.path))
-
         if cur_title != new_title:
             lhs, rhs = cur_title, new_title
             if tracks_differ:
                 lhs += u' (%s)' % cur_track
                 rhs += u' (%s)' % new_track
-            print_(u" * %s ->\n   %s" % (lhs, rhs))
+                pad = pad_len - len(u'%s (%s)' % (item.title, item.track))
+            else:
+                pad = pad_len - len(item.title)
+            # Split across two lines or align in two columns.
+            if pad_len == 0 or pad_len - pad > col_width:
+                print_(u" * %s ->\n   %s" % (lhs, rhs))
+            else:
+                print_(u" * %s%s -> %s" % (lhs, ' ' * pad, rhs))
         else:
-            line = u' * %s' % item.title
+            line = u' * %s ->' % item.title.ljust(pad_len)
             display = False
             if tracks_differ:
                 display = True
                 line += u' (%s -> %s)' % (cur_track, new_track)
-            if item.length and track_info.length and \
-                    abs(item.length - track_info.length) > 2.0:
+            if do_lengths_differ(item, track_info):
                 display = True
-                line += u' (%s vs. %s)' % (cur_length, new_length)
+                lhs = ui.colorize('red', ui.human_seconds_short(item.length))
+                rhs = ui.colorize('red',
+                                  ui.human_seconds_short(track_info.length))
+                line += u' (%s vs. %s)' % (lhs, rhs)
             if display:
                 print_(line)
 

File test/test_ui.py

     def test_item_data_change(self):
         self.items[0].title = 'different'
         msg = self._show_change()
-        self.assertTrue('different ->\n   the title' in msg)
+        self.assertTrue('different -> the title' in msg)
 
     def test_item_data_change_with_unicode(self):
         self.items[0].title = u'caf\xe9'
         msg = self._show_change()
-        self.assertTrue(u'caf\xe9 ->\n   the title' in msg.decode('utf8'))
+        self.assertTrue(u'caf\xe9 -> the title' in msg.decode('utf8'))
 
     def test_album_data_change_with_unicode(self):
         msg = self._show_change(cur_artist=u'caf\xe9',
     def test_item_data_change_title_missing(self):
         self.items[0].title = ''
         msg = self._show_change()
-        self.assertTrue('file.mp3 ->\n   the title' in msg)
+        self.assertTrue('file.mp3         -> the title' in msg)
 
     def test_item_data_change_title_missing_with_unicode_filename(self):
         self.items[0].title = ''
         self.items[0].path = u'/path/to/caf\xe9.mp3'.encode('utf8')
         msg = self._show_change().decode('utf8')
-        self.assertTrue(u'caf\xe9.mp3 ->' in msg
-                        or u'caf.mp3 ->' in msg)
+        self.assertTrue(u'caf\xe9.mp3         -> the title' in msg
+                        or u'caf.mp3         ->' in msg)
 
 class PathFormatTest(_common.TestCase):
     def test_custom_paths_prepend(self):