-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwidgets.py
More file actions
577 lines (486 loc) · 18.7 KB
/
widgets.py
File metadata and controls
577 lines (486 loc) · 18.7 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
from __future__ import annotations
from dataclasses import dataclass
from typing import Callable, Optional
from enum import Enum
from rich.style import Style
from textual import on
from textual.app import App, ComposeResult, RenderResult
from textual.binding import Binding
from textual.containers import Center, Horizontal, Middle
from textual.geometry import clamp
from textual.message import Message
from textual.reactive import reactive
from textual.renderables.bar import Bar as BarRenderable
from textual.timer import Timer
from textual.widget import Widget
from textual.widgets import Button, Footer, Label
class DeltaType(Enum):
DELTA = 0
PERCENT = 1
FRACTION = 2
class Bar(Widget, can_focus=True):
"""The bar portion of the progress bar."""
COMPONENT_CLASSES = {"bar--bar", "bar--complete"}
"""
The bar sub-widget provides the component classes that follow.
These component classes let you modify the foreground and background color of the
bar in its different states.
| Class | Description |
| :- | :- |
| `bar--bar` | Style of the bar (may be used to change the color). |
| `bar--complete` | Style of the bar when it's complete. |
| `bar--focus` | Style of the bar when it has focus. |
"""
DEFAULT_CSS = """
Bar {
width: 32;
height: 1;
}
Bar > .bar--bar {
color: $warning;
background: $foreground 10%;
}
Bar:focus > .bar--bar {
color: $error;
background: $foreground 30%;
}
Bar > .bar--complete {
color: $success;
background: $foreground 10%;
}
Bar:focus > .bar--complete {
color: $error;
background: $foreground 30%;
}
"""
BINDINGS = [
Binding("up", "increase", "Increase (+1)", show=True),
Binding("down", "decrease", "Decrease (-1)", show=True),
Binding("pageup", "page_up", "Increase (course +5%)", show=True),
Binding("pagedown", "page_down", "Decrease (course -5%)", show=True),
Binding("shift+up,shift+pageup", "inc1pc", "Increase (+.1%)", show=True),
Binding("shift+down,shift+pagedown", "dec1pc", "Decrease (-.1%)", show=True),
Binding("z", "zero", "Zero", show=True),
Binding("h", "half", "Half", show=True),
Binding("f", "full", "Full", show=True),
]
_percentage: reactive[float] = reactive[float](0.0)
"""The percentage of progress that has been completed."""
class PositionDelta(Message):
"""Posted when the value of the slider changes.
This message can be handled using an `on_slider_changed` method.
"""
def __init__(self, bar: Bar, change: float, deltatype: DeltaType) -> None:
super().__init__()
self.change: float = change
self.deltatype: DeltaType = deltatype
self.bar: Bar = bar
@property
def control(self) -> Bar:
return self.bar
def __init__(
self,
name: str | None = None,
id: str | None = None,
classes: str | None = None,
disabled: bool = False,
):
"""Create a bar for a [`ProgressBar`][textual.widgets.ProgressBar]."""
super().__init__(name=name, id=id, classes=classes, disabled=disabled)
self._percentage = 0.0
def watch__percentage(self, percentage: float) -> None:
pass
def render(self) -> RenderResult:
"""Render the bar with the correct portion filled."""
bar_style = (
self.get_component_rich_style("bar--bar")
if self._percentage < 1
else self.get_component_rich_style("bar--complete")
)
return BarRenderable(
highlight_range=(0, self.size.width * self._percentage),
highlight_style=Style.from_color(bar_style.color),
background_style=Style.from_color(bar_style.bgcolor),
)
def action_increase(self) -> None:
self.post_message(self.PositionDelta(self, +1, DeltaType.DELTA))
def action_decrease(self) -> None:
self.post_message(self.PositionDelta(self, -1, DeltaType.DELTA))
def action_inc1pc(self) -> None:
self.post_message(self.PositionDelta(self, +0.1, DeltaType.PERCENT))
def action_dec1pc(self) -> None:
self.post_message(self.PositionDelta(self, -0.1, DeltaType.PERCENT))
def action_page_up(self) -> None:
self.post_message(self.PositionDelta(self, +5, DeltaType.PERCENT))
def action_page_down(self) -> None:
self.post_message(self.PositionDelta(self, -5, DeltaType.PERCENT))
def action_zero(self) -> None:
self.post_message(self.PositionDelta(self, 0, DeltaType.FRACTION))
def action_half(self) -> None:
self.post_message(self.PositionDelta(self, 0.5, DeltaType.FRACTION))
def action_full(self) -> None:
self.post_message(self.PositionDelta(self, 1.0, DeltaType.FRACTION))
class PercentageStatus(Label):
"""A label to display the percentage status of the progress bar."""
DEFAULT_CSS = """
PercentageStatus {
width: 4;
content-align-horizontal: right;
}
"""
_label_text: reactive[str] = reactive("", repaint=False)
"""This is used as an auxiliary reactive to only refresh the label when needed."""
_percentage: reactive[float | None] = reactive[Optional[float]](None)
"""The percentage of progress that has been completed."""
def __init__(
self,
name: str | None = None,
id: str | None = None,
classes: str | None = None,
disabled: bool = False,
):
super().__init__(name=name, id=id, classes=classes, disabled=disabled)
self._percentage = None
self._label_text = "--%"
def watch__percentage(self, percentage: float | None) -> None:
"""Manage the text that shows the percentage of progress."""
if percentage is None:
self._label_text = "---%"
else:
self._label_text = f"{int(100 * percentage)}%"
def watch__label_text(self, label_text: str) -> None:
"""If the label text changed, update the renderable (which also refreshes)."""
self.update(label_text)
class FormattedValueLabel(Label):
"""A label to display the estimated time until completion of the progress bar."""
DEFAULT_CSS = """
FormattedValueLabel {
width: 7;
content-align-horizontal: right;
}
"""
_label_text: reactive[str] = reactive("", repaint=False)
"""This is used as an auxiliary reactive to only refresh the label when needed."""
_value: reactive[int] = reactive[int](1)
def __init__(
self,
name: str | None = None,
id: str | None = None,
classes: str | None = None,
disabled: bool = False,
formatter: Callable[[int], str] = str,
):
self.formatter = formatter
super().__init__(name=name, id=id, classes=classes, disabled=disabled)
self._value = 0
self._label_text = f"{self.formatter(self._value)}"
def watch__value(self, position: int | None) -> None:
self._label_text = f"{self.formatter(self._value)}"
def watch__label_text(self, label_text: str) -> None:
"""If the ETA label changed, update the renderable (which also refreshes)."""
self.update(label_text)
class PositionBar(Widget, can_focus=False):
"""A progress bar widget."""
DEFAULT_CSS = """
PositionBar > Horizontal {
width: auto;
height: auto;
}
PositionBar {
width: auto;
height: 1;
}
PositionBar > Horizontal > #label_dash {
color: grey;
}
PositionBar > Horizontal > #label_min {
content-align-horizontal: right;
color: grey;
margin-right: 1;
}
PositionBar > Horizontal > #label_max {
content-align-horizontal: left;
color: grey;
margin-left: 1;
}
"""
@dataclass
class PositionChanged(Message):
"""Posted when the value of the slider changes.
This message can be handled using an `on_slider_changed` method.
"""
bar: PositionBar
position_min: int
position: int
position_max: int
"""The progress so far, in number of steps."""
position: reactive[int] = reactive(0)
position_min: reactive[int] = reactive(0)
position_max: reactive[int] = reactive(100)
percentage: reactive[float | None] = reactive[Optional[float]](None)
"""The percentage of progress that has been completed.
The percentage is a value between 0 and 1
Example:
```py
position_bar = PositionBar(0,0,100)
print(position_bar.percentage) # 0
position_bar.update(total=100)
position_bar.adjust(50)
print(position_bar.percentage) # 0.5
```
"""
def __init__(
self,
position_min: int,
position: int,
position_max: int,
formatter: Callable[[int], str] = str,
*,
show_bar: bool = True,
show_percentage: bool = True,
show_value: bool = True,
show_range: bool = True,
name: str | None = None,
id: str | None = None,
classes: str | None = None,
disabled: bool = False,
):
"""Create a Position Bar widget.
The progress bar uses "steps" as the measurement unit.
Example:
```py
class MyApp(App):
def compose(self):
yield ProgressBar(position_min=0, position_max=100, position=5)
def key_space(self):
self.query_one(ProgressBar).adjust(5)
```
Args:
show_bar: Whether to show the bar portion of the progress bar.
show_percentage: Whether to show the percentage status of the bar.
show_value: Whether to show the min/value/max.
name: The name of the widget.
id: The ID of the widget in the DOM.
classes: The CSS classes for the widget.
disabled: Whether the widget is disabled or not.
"""
super().__init__(name=name, id=id, classes=classes, disabled=disabled)
self.formatter = formatter
self.show_bar = show_bar
self.show_percentage = show_percentage
self.show_value = show_value
self.show_range = show_range
self.position_max = position_max
self.position_min = position_min
self.position = position
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_widget_value(
widget: Widget, attrib: str
) -> Callable[[float | None], None]:
"""Closure to allow updating the percentage of a given widget."""
def updater(percentage: float | None) -> None:
"""Update the percentage reactive of the enclosed widget."""
setattr(widget, attrib, percentage)
return updater
with Horizontal():
if self.show_range:
min_label = FormattedValueLabel(
id="label_min", formatter=self.formatter
)
self.watch(
self, "position_min", update_widget_value(min_label, "_value")
)
yield min_label
if self.show_bar:
bar = Bar(id="bar")
self.watch(self, "percentage", update_widget_value(bar, "_percentage"))
yield bar
elif self.show_range:
yield Label("-", id="label_dash")
if self.show_range:
max_label = FormattedValueLabel(
id="label_max", formatter=self.formatter
)
self.watch(
self, "position_max", update_widget_value(max_label, "_value")
)
yield max_label
if self.show_percentage:
percentage_status = PercentageStatus(id="percentage")
self.watch(
self,
"percentage",
update_widget_value(percentage_status, "_percentage"),
)
yield percentage_status
if self.show_value:
value_label = FormattedValueLabel(id="value", formatter=self.formatter)
self.watch(self, "position", update_widget_value(value_label, "_value"))
yield value_label
def validate_position(self, position: float) -> float:
"""Clamp the position between minimum and the maximum."""
return clamp(position, self.position_min, self.position_max)
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.
"""
if self.position_min == self.position_max:
return 0.0
return (self.position - self.position_min) / (
self.position_max - self.position_min
)
def adjust(self, adjust: int = 1) -> None:
"""Adjust the value of the position bar by the given amount.
Example:
```py
progress_bar.adjust(-10) # Back 10 steps.
```
Args:
adjust: Number of steps to adjust position by.
"""
self.position += adjust
def update(
self,
*,
position_min: int | None = None,
position_max: int | None = None,
position: int | None = None,
adjust: int | None = None,
) -> None:
"""Update the progress bar with the given options.
Options only affect the progress bar if they are not `None`.
Example:
```py
progress_bar.update(
position=50,
position_max=150,
)
```
Args:
position_max: New maximum value (if not `None`)
position_min: New minimum value (if not `None`)
position: Set the progress to the given number of steps (if not `None`).
adjust: Adjust position by this number of steps (if not `None`).
"""
if position_min is not None:
self.position_min = position_min
if position_max is not None:
self.position_max = position_max
if position is not None:
self.position = position
if adjust is not None:
self.position += adjust
@on(Bar.PositionDelta)
def _update_position(self, event) -> None:
old_position = self.position
if event.deltatype == DeltaType.PERCENT:
pc1 = (self.position_max - self.position_min) / 100.0
self.position = int(self.position + event.change * pc1)
elif event.deltatype == DeltaType.FRACTION:
fr = (self.position_max - self.position_min) * event.change
self.position = fr
else:
self.position = self.position + event.change
if self.position != old_position:
self.post_message(
self.PositionChanged(
self, self.position_min, self.position, self.position_max
)
)
class DemoPositionBar(App[None]):
BINDINGS = [("r", "reset", "Reset")]
progress_timer: Timer
def compose(self) -> ComposeResult:
def degrees(n):
return f"{(n/10)}°"
def pounds(n):
return f"£{(n/100):.2f}"
def ppmco2(n):
return f"{(n/100):.2f}ppm CO²"
with Center():
with Middle():
yield Label("PositionBar Demo")
yield Label("\nFully featured")
yield PositionBar(0, 1, 1, id="hi1f")
yield PositionBar(0, 100, 300, id="hi1")
yield Label("\nWith unit formatter")
yield PositionBar(-200, 300, 400, id="hi2", formatter=degrees)
yield PositionBar(200, 300, 400, id="hi3", formatter=pounds)
yield PositionBar(0, 56, 255, id="hi4", formatter=hex)
yield PositionBar(0, 0, 0, id="hi4z")
yield Label("\nWithout range")
yield PositionBar(
-200, 300, 400, id="hi5", formatter=degrees, show_range=False
)
yield Label("\nWithout range, percentage")
yield PositionBar(
200,
300,
400,
id="hi6",
formatter=pounds,
show_range=False,
show_percentage=False,
)
yield Label("\nWithout range, percentage, value")
yield PositionBar(
20,
30,
40,
id="hi7",
show_range=False,
show_percentage=False,
show_value=False,
)
yield Label("\nWithout bar")
yield PositionBar(
20,
30,
40,
id="hi8",
show_range=True,
show_percentage=False,
show_bar=False,
)
yield PositionBar(
20,
30,
40,
id="hi9",
show_range=False,
show_percentage=False,
show_bar=False,
formatter=ppmco2,
)
yield Label("Last value: N/A", id="last_value")
yield Label("\nMassive range")
yield PositionBar(
0,
30,
0xFFFF,
id="hi10",
)
yield Button("Quit")
yield Footer()
def on_mount(self) -> None:
"""Set up a timer to simulate progess happening."""
self.progress_timer = self.set_interval(1 / 10, self.make_progress)
def make_progress(self) -> None:
"""Called automatically to adjust the progress bar."""
self.query_one("#hi1", PositionBar).adjust(1)
self.query_one("#hi2", PositionBar).adjust(1)
def action_reset(self) -> None:
self.query_one("#hi2", PositionBar).update(position=175)
self.query_one("#hi3", PositionBar).update(
position_max=200, position=175, position_min=50
)
def on_button_pressed(self, button):
exit()
@on(PositionBar.PositionChanged)
def on_position_changed(self, event):
self.query_one("#last_value", Label).update(f"Last value: {event}")
if __name__ == "__main__":
DemoPositionBar().run()