|
14 | 14 | # Author: Tobias Sterbak |
15 | 15 |
|
16 | 16 | import webbrowser |
| 17 | +import regex as re |
17 | 18 | from functools import partial |
18 | 19 | from typing import Callable |
19 | 20 |
|
20 | 21 | from flet import ( |
| 22 | + UserControl, |
| 23 | + colors, |
21 | 24 | Container, |
22 | 25 | ElevatedButton, |
| 26 | + ProgressRing, |
| 27 | + ProgressBar, |
23 | 28 | Row, |
24 | 29 | Text, |
25 | 30 | alignment, |
|
30 | 35 | ) |
31 | 36 |
|
32 | 37 |
|
| 38 | +class TerminalBox(UserControl): |
| 39 | + def __init__(self, expand: bool = True, visible: bool = False): |
| 40 | + super().__init__(expand=expand) |
| 41 | + self.visible = visible |
| 42 | + |
| 43 | + def build(self): |
| 44 | + self._box = Container( |
| 45 | + content=Column(scroll="auto", expand=True, auto_scroll=True), |
| 46 | + margin=10, |
| 47 | + padding=10, |
| 48 | + alignment=alignment.top_left, |
| 49 | + bgcolor=colors.BLACK38, |
| 50 | + height=300, |
| 51 | + border_radius=2, |
| 52 | + expand=True, |
| 53 | + visible=self.visible, |
| 54 | + ) |
| 55 | + return self._box |
| 56 | + |
| 57 | + def write_line(self, line: str): |
| 58 | + """ |
| 59 | + Write the line to the window box and update. |
| 60 | +
|
| 61 | + Ignores empty lines. |
| 62 | + """ |
| 63 | + if (type(line) == str) and line.strip(): |
| 64 | + self._box.content.controls.append(Text(f">{line.strip()}", selectable=True)) |
| 65 | + self.update() |
| 66 | + |
| 67 | + def toggle_visibility(self): |
| 68 | + """Toggle the visibility of the terminal box.""" |
| 69 | + self._box.visible = not self._box.visible |
| 70 | + self.visible = not self.visible |
| 71 | + self.update() |
| 72 | + |
| 73 | + def clear(self): |
| 74 | + """Clear terminal output.""" |
| 75 | + self._box.content.controls = [] |
| 76 | + self.update() |
| 77 | + |
| 78 | + def update(self): |
| 79 | + """Update the view.""" |
| 80 | + self._box.update() |
| 81 | + |
| 82 | + |
| 83 | +class ProgressIndicator(UserControl): |
| 84 | + def __init__(self, expand: bool = True): |
| 85 | + super().__init__(expand=expand) |
| 86 | + # placeholder for the flashing progressbar |
| 87 | + self.progress_bar = None |
| 88 | + # progress ring to display |
| 89 | + self.progress_ring = None |
| 90 | + |
| 91 | + def build(self): |
| 92 | + self._container = Container( |
| 93 | + content=Column(scroll="auto", expand=True), |
| 94 | + margin=10, |
| 95 | + alignment=alignment.center, |
| 96 | + height=50, |
| 97 | + expand=True, |
| 98 | + visible=True, |
| 99 | + ) |
| 100 | + return self._container |
| 101 | + |
| 102 | + def display_progress_bar(self, line: str): |
| 103 | + """Display and update the progress bar for the given line.""" |
| 104 | + percentage_done = None |
| 105 | + result = None |
| 106 | + # get the progress numbers from the output lines |
| 107 | + if (type(line) == str) and line.strip(): |
| 108 | + result = re.search( |
| 109 | + r"\(\~(\d{1,3})\%\)|(Total xfer:|adb: failed to read command: Success)", |
| 110 | + line.strip(), |
| 111 | + ) |
| 112 | + if result: |
| 113 | + if result.group(2): |
| 114 | + percentage_done = 100 |
| 115 | + elif result.group(1): |
| 116 | + percentage_done = int(result.group(1)) |
| 117 | + |
| 118 | + # create the progress bar on first occurrence |
| 119 | + if percentage_done == 0: |
| 120 | + self.progress_bar = ProgressBar( |
| 121 | + width=500, bar_height=32, color="#00d886", bgcolor="#eeeeee" |
| 122 | + ) |
| 123 | + self.percentage_text = Text(f"{percentage_done}%") |
| 124 | + self._container.content.controls.append( |
| 125 | + Row([self.percentage_text, self.progress_bar]) |
| 126 | + ) |
| 127 | + # update the progress bar |
| 128 | + if self.progress_bar: |
| 129 | + self.progress_bar.value = percentage_done / 100 |
| 130 | + self.percentage_text.value = f"{percentage_done}%" |
| 131 | + |
| 132 | + def display_progress_ring( |
| 133 | + self, |
| 134 | + ): |
| 135 | + """Display a progress ring to signal progress.""" |
| 136 | + if not self.progress_ring: |
| 137 | + self.progress_ring = ProgressRing(color="#00d886") |
| 138 | + self._container.content.controls.append(self.progress_ring) |
| 139 | + self._container.update() |
| 140 | + |
| 141 | + def clear(self): |
| 142 | + """Clear output.""" |
| 143 | + self._container.content.controls = [] |
| 144 | + self.progress_ring = None |
| 145 | + self.progress_bar = None |
| 146 | + self.update() |
| 147 | + |
| 148 | + def update(self): |
| 149 | + """Update the view.""" |
| 150 | + self._container.update() |
| 151 | + |
| 152 | + |
33 | 153 | def get_title( |
34 | 154 | title: str, info_button: IconButton = None, step_indicator_img: str = None |
35 | 155 | ) -> Container: |
| 156 | + """Function to get the title header element for the right side view.""" |
36 | 157 | if info_button: |
37 | 158 | content = Row([Text(f"{title}", style="titleLarge"), info_button]) |
38 | 159 | else: |
|
0 commit comments