Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Small simplification to the composition of ProgressBar #4083

Merged
merged 4 commits into from
Jan 31, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 34 additions & 36 deletions src/textual/widgets/_progress_bar.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

from .._types import UnusedParameter
from ..app import ComposeResult, RenderResult
from ..containers import Horizontal
from ..geometry import clamp
from ..reactive import reactive
from ..renderables.bar import Bar as BarRenderable
Expand Down Expand Up @@ -43,18 +42,19 @@ class Bar(Widget, can_focus=False):
Bar {
width: 32;
height: 1;
}
Bar > .bar--bar {
color: $warning;
background: $foreground 10%;
}
Bar > .bar--indeterminate {
color: $error;
background: $foreground 10%;
}
Bar > .bar--complete {
color: $success;
background: $foreground 10%;
&> .bar--bar {
color: $warning;
background: $foreground 10%;
}
&> .bar--indeterminate {
color: $error;
background: $foreground 10%;
}
&> .bar--complete {
color: $success;
background: $foreground 10%;
}
}
"""

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

DEFAULT_CSS = """
ProgressBar > Horizontal {
width: auto;
height: auto;
}
ProgressBar {
width: auto;
height: 1;
layout: horizontal;
}
"""

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

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

return updater

with Horizontal():
if self.show_bar:
bar = Bar(id="bar")
self.watch(self, "percentage", update_percentage(bar))
yield bar
if self.show_percentage:
percentage_status = PercentageStatus(id="percentage")
self.watch(self, "percentage", update_percentage(percentage_status))
yield percentage_status
if self.show_eta:
eta_status = ETAStatus(id="eta")
self.watch(self, "percentage", update_percentage(eta_status))
yield eta_status

def validate_progress(self, progress: float) -> float:
if self.show_bar:
bar = Bar(id="bar")
self.watch(self, "percentage", update_percentage(bar))
yield bar
if self.show_percentage:
percentage_status = PercentageStatus(id="percentage")
self.watch(self, "percentage", update_percentage(percentage_status))
yield percentage_status
if self.show_eta:
eta_status = ETAStatus(id="eta")
self.watch(self, "percentage", update_percentage(eta_status))
yield eta_status

def _validate_progress(self, progress: float) -> float:
"""Clamp the progress between 0 and the maximum total."""
if self.total is not None:
return clamp(progress, 0, self.total)
return progress

def validate_total(self, total: float | None) -> float | None:
def _validate_total(self, total: float | None) -> float | None:
"""Ensure the total is not negative."""
if total is None:
return total
return max(0, total)

def watch_total(self, total: float | None) -> None:
def _watch_total(self) -> None:
"""Re-validate progress."""
self.progress = self.progress

def compute_percentage(self) -> float | None:
def _compute_percentage(self) -> float | None:
"""Keep the percentage of progress updated automatically.
This will report a percentage of `1` if the total is zero.
Expand Down
Loading