Skip to content

Commit cd5e309

Browse files
authored
Merge pull request #4083 from davep/progress-diet
Small simplification to the composition of `ProgressBar`
2 parents 45ee02c + ccd829f commit cd5e309

File tree

1 file changed

+34
-36
lines changed

1 file changed

+34
-36
lines changed

src/textual/widgets/_progress_bar.py

Lines changed: 34 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
from .._types import UnusedParameter
1212
from ..app import ComposeResult, RenderResult
13-
from ..containers import Horizontal
1413
from ..geometry import clamp
1514
from ..reactive import reactive
1615
from ..renderables.bar import Bar as BarRenderable
@@ -43,18 +42,19 @@ class Bar(Widget, can_focus=False):
4342
Bar {
4443
width: 32;
4544
height: 1;
46-
}
47-
Bar > .bar--bar {
48-
color: $warning;
49-
background: $foreground 10%;
50-
}
51-
Bar > .bar--indeterminate {
52-
color: $error;
53-
background: $foreground 10%;
54-
}
55-
Bar > .bar--complete {
56-
color: $success;
57-
background: $foreground 10%;
45+
46+
&> .bar--bar {
47+
color: $warning;
48+
background: $foreground 10%;
49+
}
50+
&> .bar--indeterminate {
51+
color: $error;
52+
background: $foreground 10%;
53+
}
54+
&> .bar--complete {
55+
color: $success;
56+
background: $foreground 10%;
57+
}
5858
}
5959
"""
6060

@@ -263,13 +263,10 @@ class ProgressBar(Widget, can_focus=False):
263263
"""A progress bar widget."""
264264

265265
DEFAULT_CSS = """
266-
ProgressBar > Horizontal {
267-
width: auto;
268-
height: auto;
269-
}
270266
ProgressBar {
271267
width: auto;
272268
height: 1;
269+
layout: horizontal;
273270
}
274271
"""
275272

@@ -343,7 +340,9 @@ def compose(self) -> ComposeResult:
343340
# We create a closure so that we can determine what are the sub-widgets
344341
# that are present and, therefore, will need to be notified about changes
345342
# to the percentage.
346-
def update_percentage(widget: Widget) -> Callable[[float | None], None]:
343+
def update_percentage(
344+
widget: Bar | PercentageStatus | ETAStatus,
345+
) -> Callable[[float | None], None]:
347346
"""Closure to allow updating the percentage of a given widget."""
348347

349348
def updater(percentage: float | None) -> None:
@@ -352,37 +351,36 @@ def updater(percentage: float | None) -> None:
352351

353352
return updater
354353

355-
with Horizontal():
356-
if self.show_bar:
357-
bar = Bar(id="bar")
358-
self.watch(self, "percentage", update_percentage(bar))
359-
yield bar
360-
if self.show_percentage:
361-
percentage_status = PercentageStatus(id="percentage")
362-
self.watch(self, "percentage", update_percentage(percentage_status))
363-
yield percentage_status
364-
if self.show_eta:
365-
eta_status = ETAStatus(id="eta")
366-
self.watch(self, "percentage", update_percentage(eta_status))
367-
yield eta_status
368-
369-
def validate_progress(self, progress: float) -> float:
354+
if self.show_bar:
355+
bar = Bar(id="bar")
356+
self.watch(self, "percentage", update_percentage(bar))
357+
yield bar
358+
if self.show_percentage:
359+
percentage_status = PercentageStatus(id="percentage")
360+
self.watch(self, "percentage", update_percentage(percentage_status))
361+
yield percentage_status
362+
if self.show_eta:
363+
eta_status = ETAStatus(id="eta")
364+
self.watch(self, "percentage", update_percentage(eta_status))
365+
yield eta_status
366+
367+
def _validate_progress(self, progress: float) -> float:
370368
"""Clamp the progress between 0 and the maximum total."""
371369
if self.total is not None:
372370
return clamp(progress, 0, self.total)
373371
return progress
374372

375-
def validate_total(self, total: float | None) -> float | None:
373+
def _validate_total(self, total: float | None) -> float | None:
376374
"""Ensure the total is not negative."""
377375
if total is None:
378376
return total
379377
return max(0, total)
380378

381-
def watch_total(self, total: float | None) -> None:
379+
def _watch_total(self) -> None:
382380
"""Re-validate progress."""
383381
self.progress = self.progress
384382

385-
def compute_percentage(self) -> float | None:
383+
def _compute_percentage(self) -> float | None:
386384
"""Keep the percentage of progress updated automatically.
387385
388386
This will report a percentage of `1` if the total is zero.

0 commit comments

Comments
 (0)