File: progress_bar_isolated_.py

package info (click to toggle)
textual 2.1.2-1.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 55,080 kB
  • sloc: python: 85,423; lisp: 1,669; makefile: 101
file content (52 lines) | stat: -rw-r--r-- 1,663 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
from textual.app import App, ComposeResult
from textual.clock import MockClock
from textual.containers import Center, Middle
from textual.timer import Timer
from textual.widgets import Footer, ProgressBar


class IndeterminateProgressBar(App[None]):
    BINDINGS = [("s", "start", "Start")]

    progress_timer: Timer
    """Timer to simulate progress happening."""

    def compose(self) -> ComposeResult:
        self.clock = MockClock()
        with Center():
            with Middle():
                yield ProgressBar(clock=self.clock)
        yield Footer()

    def on_mount(self) -> None:
        """Set up a timer to simulate progress happening."""
        self.progress_timer = self.set_interval(1 / 10, self.make_progress, pause=True)

    def make_progress(self) -> None:
        """Called automatically to advance the progress bar."""
        self.query_one(ProgressBar).advance(1)

    def action_start(self) -> None:
        """Start the progress tracking."""
        self.query_one(ProgressBar).update(total=100)
        self.progress_timer.resume()

    def key_f(self) -> None:
        # Freeze time for the indeterminate progress bar.
        self.clock.set_time(5)
        self.refresh()

    def key_t(self) -> None:
        # Freeze time to show always the same ETA.
        self.clock.set_time(0)
        self.query_one(ProgressBar).update(total=100, progress=0)
        self.clock.set_time(3.9)
        self.query_one(ProgressBar).update(progress=39)

    def key_u(self) -> None:
        self.refresh()
        self.query_one(ProgressBar).update(total=100, progress=100)


if __name__ == "__main__":
    IndeterminateProgressBar().run()