Skip to content

Commit

Permalink
[beets.ui] Simplify some formatting logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Arav K. committed Sep 8, 2024
1 parent ff5c1dd commit 3f559c6
Showing 1 changed file with 26 additions and 20 deletions.
46 changes: 26 additions & 20 deletions beets/ui/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@


import errno
import itertools
import optparse
import os.path
import re
Expand Down Expand Up @@ -1187,33 +1188,37 @@ def show_model_changes(new, old=None, fields=None, always=False):
restrict the detection to. `always` indicates whether the object is
always identified, regardless of whether any changes are present.
"""

# TODO: require 'fields' to be 'Optional[set[str]]'.
fields = set(fields) if fields else None

old = old or new._db._get(type(new), new.id)

# Keep the formatted views around instead of re-creating them in each
# iteration step
old_fmt = old.formatted()
new_fmt = new.formatted()

# Build up lines showing changed fields.
# Calculate which fields need to be shown.
changed_fields = [f for f in old if f != "mtime"]
new_fields = set(new) - set(old)
if fields is not None:
# Restrict the presented fields to the given set.
changed_fields = [f for f in changed_fields if f in fields]
new_fields &= set(fields)

# Build up the list of changes.
changes = []
for field in old:
# Subset of the fields. Never show mtime.
if field == "mtime" or (fields and field not in fields):
continue

for field in changed_fields:
# Detect and show difference for this field.
line = _field_diff(field, old, old_fmt, new, new_fmt)
if line:
changes.append(f" {field}: {line}")

# New fields.
for field in set(new) - set(old):
if fields and field not in fields:
continue

changes.append(
" {}: {}".format(field, colorize("text_highlight", new_fmt[field]))
)
for field in new_fields:
value = colorize("text_highlight", new_fmt[field])
changes.append(f" {field}: {value}")

# Print changes.
if changes or always:
Expand Down Expand Up @@ -1246,22 +1251,23 @@ def show_path_changes(path_changes):
destinations = list(map(util.displayable_path, destinations))

# Calculate widths for terminal split
col_width = (term_width() - len(" -> ")) // 2
max_width = len(max(sources + destinations, key=len))
src_width = max(map(len, sources))
dst_width = max(map(len, destinations))

if max_width > col_width:
if src_width + len(" -> ") + dst_width > term_width():
# Print every change over two lines
for source, dest in zip(sources, destinations):
color_source, color_dest = colordiff(source, dest)
print_(f"{color_source} \n -> {color_dest}")
print_(f"{color_source}\n -> {color_dest}")
else:
# Print every change on a single line, and add a header
source = "Source "
print_(f"{source:<{max_width}} Destination")
print_(f"{source:<{src_width}} Destination")
for source, dest in zip(sources, destinations):
color_source, color_dest = colordiff(source, dest)
width = max_width - len(source) + len(color_source)
print_(f"{color_source:<{width}} -> {color_dest}")
# Account for color control codes in the padding width.
width = src_width - len(source) + len(color_source)
print_(f"{color_source:<{width}} -> {color_dest}")


# Helper functions for option parsing.
Expand Down

0 comments on commit 3f559c6

Please sign in to comment.