Refactor layout to remove dependency on ui

This commit is contained in:
Šarūnas Nejus 2026-03-14 11:26:59 +00:00
parent ffb4329006
commit 72eaa98eaf
No known key found for this signature in database
2 changed files with 14 additions and 20 deletions

View file

@ -12,7 +12,7 @@ from beets.autotag import hooks
from beets.util import displayable_path
from beets.util.color import colorize, dist_colorize, uncolorize
from beets.util.diff import colordiff
from beets.util.layout import indent, print_column_layout, print_newline_layout
from beets.util.layout import get_column_layout, get_newline_layout, indent
from beets.util.units import human_seconds_short
if TYPE_CHECKING:
@ -84,10 +84,10 @@ class ChangeRepresentation:
if not max_width:
# If no max_width provided, use terminal width
max_width = ui.term_width()
if self.layout == 0:
print_column_layout(indent, left, right, separator, max_width)
else:
print_newline_layout(indent, left, right, separator, max_width)
method = get_column_layout if self.layout == 0 else get_newline_layout
for line in method(indent, left, right, separator, max_width):
ui.print_(line)
def show_match_header(self) -> None:
"""Print out a 'header' identifying the suggested match (album name,

View file

@ -1,5 +1,3 @@
from beets import ui
from .color import (
ESC_TEXT_REGEX,
RESET_COLOR,
@ -146,9 +144,7 @@ def split_into_lines(string, width_tuple):
return result
def print_column_layout(
indent_str, left, right, separator=" -> ", max_width=ui.term_width()
):
def get_column_layout(indent_str, left, right, separator, max_width):
"""Print left & right data, with separator inbetween
'left' and 'right' have a structure of:
{'prefix':u'','contents':u'','suffix':u'','width':0}
@ -170,7 +166,7 @@ def print_column_layout(
)
if color_len(first_line_no_wrap) < max_width:
# Everything fits, print out line.
ui.print_(first_line_no_wrap)
yield first_line_no_wrap
else:
# Wrap into columns
if "width" not in left or "width" not in right:
@ -273,12 +269,10 @@ def print_column_layout(
out += "\n"
# Constructed all of the columns, now print
ui.print_(out)
yield out
def print_newline_layout(
indent_str, left, right, separator=" -> ", max_width=ui.term_width()
):
def get_newline_layout(indent_str, left, right, separator, max_width):
"""Prints using a newline separator between left & right if
they go over their allocated widths. The datastructures are
shared with the column layout. In contrast to the column layout,
@ -301,7 +295,7 @@ def print_newline_layout(
)
if color_len(first_line_no_wrap) < max_width:
# Everything fits, print out line.
ui.print_(first_line_no_wrap)
yield first_line_no_wrap
else:
# Newline separation, with wrapping
empty_space = max_width - len(indent_str)
@ -323,12 +317,12 @@ def print_newline_layout(
right_split = split_into_lines(right_str, right_width_tuple)
for i, line in enumerate(left_split):
if i == 0:
ui.print_(f"{indent_str}{line}")
yield f"{indent_str}{line}"
elif line != "":
# Ignore empty lines
ui.print_(f"{indent_str * 2}{line}")
yield f"{indent_str * 2}{line}"
for i, line in enumerate(right_split):
if i == 0:
ui.print_(f"{indent_str}{separator}{line}")
yield f"{indent_str}{separator}{line}"
elif line != "":
ui.print_(f"{indent_str * 2}{line}")
yield f"{indent_str * 2}{line}"