Skip to content

Commit 5927aae

Browse files
committed
refactor(widgets): extract Bar._render_borders for the shared border preamble
The identical left/right border resolution (resolve callables, subtract their visible length from width) was copy-pasted across five bar __call__ methods. Extract it into Bar._render_borders(progress, data, width) -> (left, right, remaining_width) and call it from Bar, BouncingBar, MultiRangeBar and JobStatusBar (all Bar subclasses). GranularBar descends from AutoWidthWidgetBase (not Bar) so it can't reach the helper; rather than hoisting border concerns onto a shared width-widget base (a responsibility smell affecting widgets that have no borders) or changing any MRO, its copy is kept with an explanatory comment. Behavior-preserving: widget render output byte-identical, full suite + render goldens pass, widgets.py at 100% branch coverage.
1 parent 59b3cb2 commit 5927aae

1 file changed

Lines changed: 25 additions & 12 deletions

File tree

‎progressbar/widgets.py‎

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,6 +1114,23 @@ def __init__(
11141114

11151115
super().__init__(**kwargs)
11161116

1117+
def _render_borders(
1118+
self,
1119+
progress: ProgressBarMixinBase,
1120+
data: Data,
1121+
width: int,
1122+
) -> tuple[str, str, int]:
1123+
"""Resolve the left/right borders and the width left for the body.
1124+
1125+
The borders may be callables, so they are resolved against
1126+
``progress``/``data`` and their visible length subtracted from
1127+
``width``. Shared by every :class:`Bar` subclass' ``__call__``.
1128+
"""
1129+
left = converters.to_unicode(self.left(progress, data, width))
1130+
right = converters.to_unicode(self.right(progress, data, width))
1131+
width -= progress.custom_len(left) + progress.custom_len(right)
1132+
return left, right, width
1133+
11171134
def __call__(
11181135
self,
11191136
progress: ProgressBarMixinBase,
@@ -1122,9 +1139,7 @@ def __call__(
11221139
color=True,
11231140
):
11241141
"""Updates the progress bar and its subcomponents."""
1125-
left = converters.to_unicode(self.left(progress, data, width))
1126-
right = converters.to_unicode(self.right(progress, data, width))
1127-
width -= progress.custom_len(left) + progress.custom_len(right)
1142+
left, right, width = self._render_borders(progress, data, width)
11281143
marker = converters.to_unicode(self.marker(progress, data, width))
11291144
fill = converters.to_unicode(self.fill(progress, data, width))
11301145

@@ -1185,9 +1200,7 @@ def __call__(
11851200
color=True,
11861201
):
11871202
"""Updates the progress bar and its subcomponents."""
1188-
left = converters.to_unicode(self.left(progress, data, width))
1189-
right = converters.to_unicode(self.right(progress, data, width))
1190-
width -= progress.custom_len(left) + progress.custom_len(right)
1203+
left, right, width = self._render_borders(progress, data, width)
11911204
marker = converters.to_unicode(self.marker(progress, data, width))
11921205

11931206
fill = converters.to_unicode(self.fill(progress, data, width))
@@ -1285,9 +1298,7 @@ def __call__(
12851298
color=True,
12861299
):
12871300
"""Updates the progress bar and its subcomponents."""
1288-
left = converters.to_unicode(self.left(progress, data, width))
1289-
right = converters.to_unicode(self.right(progress, data, width))
1290-
width -= progress.custom_len(left) + progress.custom_len(right)
1301+
left, right, width = self._render_borders(progress, data, width)
12911302
values = self.get_values(progress, data)
12921303

12931304
values_sum = sum(values)
@@ -1414,6 +1425,10 @@ def __call__(
14141425
data: Data,
14151426
width: int = 0,
14161427
):
1428+
# GranularBar descends from AutoWidthWidgetBase, not Bar, so it can't
1429+
# reach Bar._render_borders. The border preamble is intentionally
1430+
# duplicated here rather than hoisting the helper onto a shared base
1431+
# (which would put border concerns on width widgets that have none).
14171432
left = converters.to_unicode(self.left(progress, data, width))
14181433
right = converters.to_unicode(self.right(progress, data, width))
14191434
width -= progress.custom_len(left) + progress.custom_len(right)
@@ -1682,9 +1697,7 @@ def __call__(
16821697
width: int = 0,
16831698
color=True,
16841699
):
1685-
left = converters.to_unicode(self.left(progress, data, width))
1686-
right = converters.to_unicode(self.right(progress, data, width))
1687-
width -= progress.custom_len(left) + progress.custom_len(right)
1700+
left, right, width = self._render_borders(progress, data, width)
16881701

16891702
status: str | bool | None = data['variables'].get(self.name)
16901703

0 commit comments

Comments
 (0)