progressbar2 4.3.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- docs/_theme/LICENSE +46 -0
- docs/_theme/flask_theme_support.py +89 -0
- docs/_theme/wolph/layout.html +27 -0
- docs/_theme/wolph/relations.html +19 -0
- docs/_theme/wolph/static/flasky.css_t +431 -0
- docs/_theme/wolph/static/small_flask.css +70 -0
- docs/_theme/wolph/theme.conf +7 -0
- progressbar/__about__.py +27 -0
- progressbar/__init__.py +81 -0
- progressbar/bar.py +1099 -0
- progressbar/base.py +31 -0
- progressbar/env.py +159 -0
- progressbar/multi.py +372 -0
- progressbar/py.typed +0 -0
- progressbar/shortcuts.py +22 -0
- progressbar/terminal/__init__.py +4 -0
- progressbar/terminal/base.py +524 -0
- progressbar/terminal/colors.py +1070 -0
- progressbar/terminal/os_specific/__init__.py +22 -0
- progressbar/terminal/os_specific/posix.py +15 -0
- progressbar/terminal/os_specific/windows.py +137 -0
- progressbar/terminal/stream.py +138 -0
- progressbar/utils.py +445 -0
- progressbar/widgets.py +1564 -0
- progressbar2-4.3.0.dist-info/LICENSE +28 -0
- progressbar2-4.3.0.dist-info/METADATA +442 -0
- progressbar2-4.3.0.dist-info/RECORD +30 -0
- progressbar2-4.3.0.dist-info/WHEEL +5 -0
- progressbar2-4.3.0.dist-info/entry_points.txt +2 -0
- progressbar2-4.3.0.dist-info/top_level.txt +2 -0
progressbar/widgets.py
ADDED
|
@@ -0,0 +1,1564 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import abc
|
|
4
|
+
import contextlib
|
|
5
|
+
import datetime
|
|
6
|
+
import functools
|
|
7
|
+
import logging
|
|
8
|
+
import typing
|
|
9
|
+
|
|
10
|
+
# Ruff is being stupid and doesn't understand `ClassVar` if it comes from the
|
|
11
|
+
# `types` module
|
|
12
|
+
from typing import ClassVar
|
|
13
|
+
|
|
14
|
+
from python_utils import containers, converters, types
|
|
15
|
+
|
|
16
|
+
from . import base, terminal, utils
|
|
17
|
+
from .terminal import colors
|
|
18
|
+
|
|
19
|
+
if types.TYPE_CHECKING:
|
|
20
|
+
from .bar import ProgressBarMixinBase
|
|
21
|
+
|
|
22
|
+
logger = logging.getLogger(__name__)
|
|
23
|
+
|
|
24
|
+
MAX_DATE = datetime.date.max
|
|
25
|
+
MAX_TIME = datetime.time.max
|
|
26
|
+
MAX_DATETIME = datetime.datetime.max
|
|
27
|
+
|
|
28
|
+
Data = types.Dict[str, types.Any]
|
|
29
|
+
FormatString = typing.Optional[str]
|
|
30
|
+
|
|
31
|
+
T = typing.TypeVar('T')
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def string_or_lambda(input_):
|
|
35
|
+
if isinstance(input_, str):
|
|
36
|
+
|
|
37
|
+
def render_input(progress, data, width):
|
|
38
|
+
return input_ % data
|
|
39
|
+
|
|
40
|
+
return render_input
|
|
41
|
+
else:
|
|
42
|
+
return input_
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def create_wrapper(wrapper):
|
|
46
|
+
'''Convert a wrapper tuple or format string to a format string.
|
|
47
|
+
|
|
48
|
+
>>> create_wrapper('')
|
|
49
|
+
|
|
50
|
+
>>> print(create_wrapper('a{}b'))
|
|
51
|
+
a{}b
|
|
52
|
+
|
|
53
|
+
>>> print(create_wrapper(('a', 'b')))
|
|
54
|
+
a{}b
|
|
55
|
+
'''
|
|
56
|
+
if isinstance(wrapper, tuple) and len(wrapper) == 2:
|
|
57
|
+
a, b = wrapper
|
|
58
|
+
wrapper = (a or '') + '{}' + (b or '')
|
|
59
|
+
elif not wrapper:
|
|
60
|
+
return None
|
|
61
|
+
|
|
62
|
+
if isinstance(wrapper, str):
|
|
63
|
+
assert '{}' in wrapper, 'Expected string with {} for formatting'
|
|
64
|
+
else:
|
|
65
|
+
raise RuntimeError( # noqa: TRY004
|
|
66
|
+
'Pass either a begin/end string as a tuple or a template string '
|
|
67
|
+
'with `{}`',
|
|
68
|
+
)
|
|
69
|
+
|
|
70
|
+
return wrapper
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def wrapper(function, wrapper_):
|
|
74
|
+
'''Wrap the output of a function in a template string or a tuple with
|
|
75
|
+
begin/end strings.
|
|
76
|
+
|
|
77
|
+
'''
|
|
78
|
+
wrapper_ = create_wrapper(wrapper_)
|
|
79
|
+
if not wrapper_:
|
|
80
|
+
return function
|
|
81
|
+
|
|
82
|
+
@functools.wraps(function)
|
|
83
|
+
def wrap(*args, **kwargs):
|
|
84
|
+
return wrapper_.format(function(*args, **kwargs))
|
|
85
|
+
|
|
86
|
+
return wrap
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def create_marker(marker, wrap=None):
|
|
90
|
+
def _marker(progress, data, width):
|
|
91
|
+
if (
|
|
92
|
+
progress.max_value is not base.UnknownLength
|
|
93
|
+
and progress.max_value > 0
|
|
94
|
+
):
|
|
95
|
+
length = int(progress.value / progress.max_value * width)
|
|
96
|
+
return marker * length
|
|
97
|
+
else:
|
|
98
|
+
return marker
|
|
99
|
+
|
|
100
|
+
if isinstance(marker, str):
|
|
101
|
+
marker = converters.to_unicode(marker)
|
|
102
|
+
assert (
|
|
103
|
+
utils.len_color(marker) == 1
|
|
104
|
+
), 'Markers are required to be 1 char'
|
|
105
|
+
return wrapper(_marker, wrap)
|
|
106
|
+
else:
|
|
107
|
+
return wrapper(marker, wrap)
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
class FormatWidgetMixin(abc.ABC):
|
|
111
|
+
'''Mixin to format widgets using a formatstring.
|
|
112
|
+
|
|
113
|
+
Variables available:
|
|
114
|
+
- max_value: The maximum value (can be None with iterators)
|
|
115
|
+
- value: The current value
|
|
116
|
+
- total_seconds_elapsed: The seconds since the bar started
|
|
117
|
+
- seconds_elapsed: The seconds since the bar started modulo 60
|
|
118
|
+
- minutes_elapsed: The minutes since the bar started modulo 60
|
|
119
|
+
- hours_elapsed: The hours since the bar started modulo 24
|
|
120
|
+
- days_elapsed: The hours since the bar started
|
|
121
|
+
- time_elapsed: Shortcut for HH:MM:SS time since the bar started including
|
|
122
|
+
days
|
|
123
|
+
- percentage: Percentage as a float
|
|
124
|
+
'''
|
|
125
|
+
|
|
126
|
+
def __init__(self, format: str, new_style: bool = False, **kwargs):
|
|
127
|
+
self.new_style = new_style
|
|
128
|
+
self.format = format
|
|
129
|
+
|
|
130
|
+
def get_format(
|
|
131
|
+
self,
|
|
132
|
+
progress: ProgressBarMixinBase,
|
|
133
|
+
data: Data,
|
|
134
|
+
format: types.Optional[str] = None,
|
|
135
|
+
) -> str:
|
|
136
|
+
return format or self.format
|
|
137
|
+
|
|
138
|
+
def __call__(
|
|
139
|
+
self,
|
|
140
|
+
progress: ProgressBarMixinBase,
|
|
141
|
+
data: Data,
|
|
142
|
+
format: types.Optional[str] = None,
|
|
143
|
+
) -> str:
|
|
144
|
+
'''Formats the widget into a string.'''
|
|
145
|
+
format_ = self.get_format(progress, data, format)
|
|
146
|
+
try:
|
|
147
|
+
if self.new_style:
|
|
148
|
+
return format_.format(**data)
|
|
149
|
+
else:
|
|
150
|
+
return format_ % data
|
|
151
|
+
except (TypeError, KeyError):
|
|
152
|
+
logger.exception(
|
|
153
|
+
'Error while formatting %r with data: %r',
|
|
154
|
+
format_,
|
|
155
|
+
data,
|
|
156
|
+
)
|
|
157
|
+
raise
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
class WidthWidgetMixin(abc.ABC):
|
|
161
|
+
'''Mixing to make sure widgets are only visible if the screen is within a
|
|
162
|
+
specified size range so the progressbar fits on both large and small
|
|
163
|
+
screens.
|
|
164
|
+
|
|
165
|
+
Variables available:
|
|
166
|
+
- min_width: Only display the widget if at least `min_width` is left
|
|
167
|
+
- max_width: Only display the widget if at most `max_width` is left
|
|
168
|
+
|
|
169
|
+
>>> class Progress:
|
|
170
|
+
... term_width = 0
|
|
171
|
+
|
|
172
|
+
>>> WidthWidgetMixin(5, 10).check_size(Progress)
|
|
173
|
+
False
|
|
174
|
+
>>> Progress.term_width = 5
|
|
175
|
+
>>> WidthWidgetMixin(5, 10).check_size(Progress)
|
|
176
|
+
True
|
|
177
|
+
>>> Progress.term_width = 10
|
|
178
|
+
>>> WidthWidgetMixin(5, 10).check_size(Progress)
|
|
179
|
+
True
|
|
180
|
+
>>> Progress.term_width = 11
|
|
181
|
+
>>> WidthWidgetMixin(5, 10).check_size(Progress)
|
|
182
|
+
False
|
|
183
|
+
'''
|
|
184
|
+
|
|
185
|
+
def __init__(self, min_width=None, max_width=None, **kwargs):
|
|
186
|
+
self.min_width = min_width
|
|
187
|
+
self.max_width = max_width
|
|
188
|
+
|
|
189
|
+
def check_size(self, progress: ProgressBarMixinBase):
|
|
190
|
+
max_width = self.max_width
|
|
191
|
+
min_width = self.min_width
|
|
192
|
+
if min_width and min_width > progress.term_width:
|
|
193
|
+
return False
|
|
194
|
+
elif max_width and max_width < progress.term_width: # noqa: SIM103
|
|
195
|
+
return False
|
|
196
|
+
else:
|
|
197
|
+
return True
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
class TGradientColors(typing.TypedDict):
|
|
201
|
+
fg: types.Optional[terminal.OptionalColor | None]
|
|
202
|
+
bg: types.Optional[terminal.OptionalColor | None]
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
class TFixedColors(typing.TypedDict):
|
|
206
|
+
fg_none: types.Optional[terminal.Color | None]
|
|
207
|
+
bg_none: types.Optional[terminal.Color | None]
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
class WidgetBase(WidthWidgetMixin, metaclass=abc.ABCMeta):
|
|
211
|
+
'''The base class for all widgets.
|
|
212
|
+
|
|
213
|
+
The ProgressBar will call the widget's update value when the widget should
|
|
214
|
+
be updated. The widget's size may change between calls, but the widget may
|
|
215
|
+
display incorrectly if the size changes drastically and repeatedly.
|
|
216
|
+
|
|
217
|
+
The INTERVAL timedelta informs the ProgressBar that it should be
|
|
218
|
+
updated more often because it is time sensitive.
|
|
219
|
+
|
|
220
|
+
The widgets are only visible if the screen is within a
|
|
221
|
+
specified size range so the progressbar fits on both large and small
|
|
222
|
+
screens.
|
|
223
|
+
|
|
224
|
+
WARNING: Widgets can be shared between multiple progressbars so any state
|
|
225
|
+
information specific to a progressbar should be stored within the
|
|
226
|
+
progressbar instead of the widget.
|
|
227
|
+
|
|
228
|
+
Variables available:
|
|
229
|
+
- min_width: Only display the widget if at least `min_width` is left
|
|
230
|
+
- max_width: Only display the widget if at most `max_width` is left
|
|
231
|
+
- weight: Widgets with a higher `weight` will be calculated before widgets
|
|
232
|
+
with a lower one
|
|
233
|
+
- copy: Copy this widget when initializing the progress bar so the
|
|
234
|
+
progressbar can be reused. Some widgets such as the FormatCustomText
|
|
235
|
+
require the shared state so this needs to be optional
|
|
236
|
+
|
|
237
|
+
'''
|
|
238
|
+
|
|
239
|
+
copy = True
|
|
240
|
+
|
|
241
|
+
@abc.abstractmethod
|
|
242
|
+
def __call__(self, progress: ProgressBarMixinBase, data: Data) -> str:
|
|
243
|
+
'''Updates the widget.
|
|
244
|
+
|
|
245
|
+
progress - a reference to the calling ProgressBar
|
|
246
|
+
'''
|
|
247
|
+
|
|
248
|
+
_fixed_colors: ClassVar[TFixedColors] = TFixedColors(
|
|
249
|
+
fg_none=None,
|
|
250
|
+
bg_none=None,
|
|
251
|
+
)
|
|
252
|
+
_gradient_colors: ClassVar[TGradientColors] = TGradientColors(
|
|
253
|
+
fg=None,
|
|
254
|
+
bg=None,
|
|
255
|
+
)
|
|
256
|
+
# _fixed_colors: ClassVar[dict[str, terminal.Color | None]] = dict()
|
|
257
|
+
# _gradient_colors: ClassVar[dict[str, terminal.OptionalColor | None]] = (
|
|
258
|
+
# dict())
|
|
259
|
+
_len: typing.Callable[[str | bytes], int] = len
|
|
260
|
+
|
|
261
|
+
@functools.cached_property
|
|
262
|
+
def uses_colors(self):
|
|
263
|
+
for value in self._gradient_colors.values(): # pragma: no branch
|
|
264
|
+
if value is not None: # pragma: no branch
|
|
265
|
+
return True
|
|
266
|
+
|
|
267
|
+
return any(value is not None for value in self._fixed_colors.values())
|
|
268
|
+
|
|
269
|
+
def _apply_colors(self, text: str, data: Data) -> str:
|
|
270
|
+
if self.uses_colors:
|
|
271
|
+
return terminal.apply_colors(
|
|
272
|
+
text,
|
|
273
|
+
data.get('percentage'),
|
|
274
|
+
**self._gradient_colors,
|
|
275
|
+
**self._fixed_colors,
|
|
276
|
+
)
|
|
277
|
+
else:
|
|
278
|
+
return text
|
|
279
|
+
|
|
280
|
+
def __init__(
|
|
281
|
+
self,
|
|
282
|
+
*args,
|
|
283
|
+
fixed_colors=None,
|
|
284
|
+
gradient_colors=None,
|
|
285
|
+
**kwargs,
|
|
286
|
+
):
|
|
287
|
+
if fixed_colors is not None:
|
|
288
|
+
self._fixed_colors.update(fixed_colors)
|
|
289
|
+
|
|
290
|
+
if gradient_colors is not None:
|
|
291
|
+
self._gradient_colors.update(gradient_colors)
|
|
292
|
+
|
|
293
|
+
if self.uses_colors:
|
|
294
|
+
self._len = utils.len_color
|
|
295
|
+
|
|
296
|
+
super().__init__(*args, **kwargs)
|
|
297
|
+
|
|
298
|
+
|
|
299
|
+
class AutoWidthWidgetBase(WidgetBase, metaclass=abc.ABCMeta):
|
|
300
|
+
'''The base class for all variable width widgets.
|
|
301
|
+
|
|
302
|
+
This widget is much like the \\hfill command in TeX, it will expand to
|
|
303
|
+
fill the line. You can use more than one in the same line, and they will
|
|
304
|
+
all have the same width, and together will fill the line.
|
|
305
|
+
'''
|
|
306
|
+
|
|
307
|
+
@abc.abstractmethod
|
|
308
|
+
def __call__(
|
|
309
|
+
self,
|
|
310
|
+
progress: ProgressBarMixinBase,
|
|
311
|
+
data: Data,
|
|
312
|
+
width: int = 0,
|
|
313
|
+
) -> str:
|
|
314
|
+
'''Updates the widget providing the total width the widget must fill.
|
|
315
|
+
|
|
316
|
+
progress - a reference to the calling ProgressBar
|
|
317
|
+
width - The total width the widget must fill
|
|
318
|
+
'''
|
|
319
|
+
|
|
320
|
+
|
|
321
|
+
class TimeSensitiveWidgetBase(WidgetBase, metaclass=abc.ABCMeta):
|
|
322
|
+
'''The base class for all time sensitive widgets.
|
|
323
|
+
|
|
324
|
+
Some widgets like timers would become out of date unless updated at least
|
|
325
|
+
every `INTERVAL`
|
|
326
|
+
'''
|
|
327
|
+
|
|
328
|
+
INTERVAL = datetime.timedelta(milliseconds=100)
|
|
329
|
+
|
|
330
|
+
|
|
331
|
+
class FormatLabel(FormatWidgetMixin, WidgetBase):
|
|
332
|
+
'''Displays a formatted label.
|
|
333
|
+
|
|
334
|
+
>>> label = FormatLabel('%(value)s', min_width=5, max_width=10)
|
|
335
|
+
>>> class Progress:
|
|
336
|
+
... pass
|
|
337
|
+
>>> label = FormatLabel('{value} :: {value:^6}', new_style=True)
|
|
338
|
+
>>> str(label(Progress, dict(value='test')))
|
|
339
|
+
'test :: test '
|
|
340
|
+
|
|
341
|
+
'''
|
|
342
|
+
|
|
343
|
+
mapping: ClassVar[types.Dict[str, types.Tuple[str, types.Any]]] = dict(
|
|
344
|
+
finished=('end_time', None),
|
|
345
|
+
last_update=('last_update_time', None),
|
|
346
|
+
max=('max_value', None),
|
|
347
|
+
seconds=('seconds_elapsed', None),
|
|
348
|
+
start=('start_time', None),
|
|
349
|
+
elapsed=('total_seconds_elapsed', utils.format_time),
|
|
350
|
+
value=('value', None),
|
|
351
|
+
)
|
|
352
|
+
|
|
353
|
+
def __init__(self, format: str, **kwargs):
|
|
354
|
+
FormatWidgetMixin.__init__(self, format=format, **kwargs)
|
|
355
|
+
WidgetBase.__init__(self, **kwargs)
|
|
356
|
+
|
|
357
|
+
def __call__(
|
|
358
|
+
self,
|
|
359
|
+
progress: ProgressBarMixinBase,
|
|
360
|
+
data: Data,
|
|
361
|
+
format: types.Optional[str] = None,
|
|
362
|
+
):
|
|
363
|
+
for name, (key, transform) in self.mapping.items():
|
|
364
|
+
with contextlib.suppress(KeyError, ValueError, IndexError):
|
|
365
|
+
if transform is None:
|
|
366
|
+
data[name] = data[key]
|
|
367
|
+
else:
|
|
368
|
+
data[name] = transform(data[key])
|
|
369
|
+
|
|
370
|
+
return FormatWidgetMixin.__call__(self, progress, data, format)
|
|
371
|
+
|
|
372
|
+
|
|
373
|
+
class Timer(FormatLabel, TimeSensitiveWidgetBase):
|
|
374
|
+
'''WidgetBase which displays the elapsed seconds.'''
|
|
375
|
+
|
|
376
|
+
def __init__(self, format='Elapsed Time: %(elapsed)s', **kwargs):
|
|
377
|
+
if '%s' in format and '%(elapsed)s' not in format:
|
|
378
|
+
format = format.replace('%s', '%(elapsed)s')
|
|
379
|
+
|
|
380
|
+
FormatLabel.__init__(self, format=format, **kwargs)
|
|
381
|
+
TimeSensitiveWidgetBase.__init__(self, **kwargs)
|
|
382
|
+
|
|
383
|
+
# This is exposed as a static method for backwards compatibility
|
|
384
|
+
format_time = staticmethod(utils.format_time)
|
|
385
|
+
|
|
386
|
+
|
|
387
|
+
class SamplesMixin(TimeSensitiveWidgetBase, metaclass=abc.ABCMeta):
|
|
388
|
+
'''
|
|
389
|
+
Mixing for widgets that average multiple measurements.
|
|
390
|
+
|
|
391
|
+
Note that samples can be either an integer or a timedelta to indicate a
|
|
392
|
+
certain amount of time
|
|
393
|
+
|
|
394
|
+
>>> class progress:
|
|
395
|
+
... last_update_time = datetime.datetime.now()
|
|
396
|
+
... value = 1
|
|
397
|
+
... extra = dict()
|
|
398
|
+
|
|
399
|
+
>>> samples = SamplesMixin(samples=2)
|
|
400
|
+
>>> samples(progress, None, True)
|
|
401
|
+
(None, None)
|
|
402
|
+
>>> progress.last_update_time += datetime.timedelta(seconds=1)
|
|
403
|
+
>>> samples(progress, None, True) == (datetime.timedelta(seconds=1), 0)
|
|
404
|
+
True
|
|
405
|
+
|
|
406
|
+
>>> progress.last_update_time += datetime.timedelta(seconds=1)
|
|
407
|
+
>>> samples(progress, None, True) == (datetime.timedelta(seconds=1), 0)
|
|
408
|
+
True
|
|
409
|
+
|
|
410
|
+
>>> samples = SamplesMixin(samples=datetime.timedelta(seconds=1))
|
|
411
|
+
>>> _, value = samples(progress, None)
|
|
412
|
+
>>> value
|
|
413
|
+
SliceableDeque([1, 1])
|
|
414
|
+
|
|
415
|
+
>>> samples(progress, None, True) == (datetime.timedelta(seconds=1), 0)
|
|
416
|
+
True
|
|
417
|
+
'''
|
|
418
|
+
|
|
419
|
+
def __init__(
|
|
420
|
+
self,
|
|
421
|
+
samples=datetime.timedelta(seconds=2),
|
|
422
|
+
key_prefix=None,
|
|
423
|
+
**kwargs,
|
|
424
|
+
):
|
|
425
|
+
self.samples = samples
|
|
426
|
+
self.key_prefix = (key_prefix or self.__class__.__name__) + '_'
|
|
427
|
+
TimeSensitiveWidgetBase.__init__(self, **kwargs)
|
|
428
|
+
|
|
429
|
+
def get_sample_times(self, progress: ProgressBarMixinBase, data: Data):
|
|
430
|
+
return progress.extra.setdefault(
|
|
431
|
+
f'{self.key_prefix}sample_times',
|
|
432
|
+
containers.SliceableDeque(),
|
|
433
|
+
)
|
|
434
|
+
|
|
435
|
+
def get_sample_values(self, progress: ProgressBarMixinBase, data: Data):
|
|
436
|
+
return progress.extra.setdefault(
|
|
437
|
+
f'{self.key_prefix}sample_values',
|
|
438
|
+
containers.SliceableDeque(),
|
|
439
|
+
)
|
|
440
|
+
|
|
441
|
+
def __call__(
|
|
442
|
+
self,
|
|
443
|
+
progress: ProgressBarMixinBase,
|
|
444
|
+
data: Data,
|
|
445
|
+
delta: bool = False,
|
|
446
|
+
):
|
|
447
|
+
sample_times = self.get_sample_times(progress, data)
|
|
448
|
+
sample_values = self.get_sample_values(progress, data)
|
|
449
|
+
|
|
450
|
+
if sample_times:
|
|
451
|
+
sample_time = sample_times[-1]
|
|
452
|
+
else:
|
|
453
|
+
sample_time = datetime.datetime.min
|
|
454
|
+
|
|
455
|
+
if progress.last_update_time - sample_time > self.INTERVAL:
|
|
456
|
+
# Add a sample but limit the size to `num_samples`
|
|
457
|
+
sample_times.append(progress.last_update_time)
|
|
458
|
+
sample_values.append(progress.value)
|
|
459
|
+
|
|
460
|
+
if isinstance(self.samples, datetime.timedelta):
|
|
461
|
+
minimum_time = progress.last_update_time - self.samples
|
|
462
|
+
minimum_value = sample_values[-1]
|
|
463
|
+
while (
|
|
464
|
+
sample_times[2:]
|
|
465
|
+
and minimum_time > sample_times[1]
|
|
466
|
+
and minimum_value > sample_values[1]
|
|
467
|
+
):
|
|
468
|
+
sample_times.pop(0)
|
|
469
|
+
sample_values.pop(0)
|
|
470
|
+
elif len(sample_times) > self.samples:
|
|
471
|
+
sample_times.pop(0)
|
|
472
|
+
sample_values.pop(0)
|
|
473
|
+
|
|
474
|
+
if delta:
|
|
475
|
+
if delta_time := sample_times[-1] - sample_times[0]:
|
|
476
|
+
delta_value = sample_values[-1] - sample_values[0]
|
|
477
|
+
return delta_time, delta_value
|
|
478
|
+
else:
|
|
479
|
+
return None, None
|
|
480
|
+
else:
|
|
481
|
+
return sample_times, sample_values
|
|
482
|
+
|
|
483
|
+
|
|
484
|
+
class ETA(Timer):
|
|
485
|
+
'''WidgetBase which attempts to estimate the time of arrival.'''
|
|
486
|
+
|
|
487
|
+
def __init__(
|
|
488
|
+
self,
|
|
489
|
+
format_not_started='ETA: --:--:--',
|
|
490
|
+
format_finished='Time: %(elapsed)8s',
|
|
491
|
+
format='ETA: %(eta)8s',
|
|
492
|
+
format_zero='ETA: 00:00:00',
|
|
493
|
+
format_na='ETA: N/A',
|
|
494
|
+
**kwargs,
|
|
495
|
+
):
|
|
496
|
+
if '%s' in format and '%(eta)s' not in format:
|
|
497
|
+
format = format.replace('%s', '%(eta)s')
|
|
498
|
+
|
|
499
|
+
Timer.__init__(self, **kwargs)
|
|
500
|
+
self.format_not_started = format_not_started
|
|
501
|
+
self.format_finished = format_finished
|
|
502
|
+
self.format = format
|
|
503
|
+
self.format_zero = format_zero
|
|
504
|
+
self.format_NA = format_na
|
|
505
|
+
|
|
506
|
+
def _calculate_eta(
|
|
507
|
+
self,
|
|
508
|
+
progress: ProgressBarMixinBase,
|
|
509
|
+
data: Data,
|
|
510
|
+
value,
|
|
511
|
+
elapsed,
|
|
512
|
+
):
|
|
513
|
+
'''Updates the widget to show the ETA or total time when finished.'''
|
|
514
|
+
if elapsed:
|
|
515
|
+
# The max() prevents zero division errors
|
|
516
|
+
per_item = elapsed.total_seconds() / max(value, 1e-6)
|
|
517
|
+
remaining = progress.max_value - data['value']
|
|
518
|
+
return remaining * per_item
|
|
519
|
+
else:
|
|
520
|
+
return 0
|
|
521
|
+
|
|
522
|
+
def __call__(
|
|
523
|
+
self,
|
|
524
|
+
progress: ProgressBarMixinBase,
|
|
525
|
+
data: Data,
|
|
526
|
+
value=None,
|
|
527
|
+
elapsed=None,
|
|
528
|
+
):
|
|
529
|
+
'''Updates the widget to show the ETA or total time when finished.'''
|
|
530
|
+
if value is None:
|
|
531
|
+
value = data['value']
|
|
532
|
+
|
|
533
|
+
if elapsed is None:
|
|
534
|
+
elapsed = data['time_elapsed']
|
|
535
|
+
|
|
536
|
+
eta_na = False
|
|
537
|
+
try:
|
|
538
|
+
data['eta_seconds'] = self._calculate_eta(
|
|
539
|
+
progress,
|
|
540
|
+
data,
|
|
541
|
+
value=value,
|
|
542
|
+
elapsed=elapsed,
|
|
543
|
+
)
|
|
544
|
+
except TypeError:
|
|
545
|
+
data['eta_seconds'] = None
|
|
546
|
+
eta_na = True
|
|
547
|
+
|
|
548
|
+
data['eta'] = None
|
|
549
|
+
if data['eta_seconds']:
|
|
550
|
+
with contextlib.suppress(ValueError, OverflowError):
|
|
551
|
+
data['eta'] = utils.format_time(data['eta_seconds'])
|
|
552
|
+
|
|
553
|
+
if data['value'] == progress.min_value:
|
|
554
|
+
fmt = self.format_not_started
|
|
555
|
+
elif progress.end_time:
|
|
556
|
+
fmt = self.format_finished
|
|
557
|
+
elif data['eta']:
|
|
558
|
+
fmt = self.format
|
|
559
|
+
elif eta_na:
|
|
560
|
+
fmt = self.format_NA
|
|
561
|
+
else:
|
|
562
|
+
fmt = self.format_zero
|
|
563
|
+
|
|
564
|
+
return Timer.__call__(self, progress, data, format=fmt)
|
|
565
|
+
|
|
566
|
+
|
|
567
|
+
class AbsoluteETA(ETA):
|
|
568
|
+
'''Widget which attempts to estimate the absolute time of arrival.'''
|
|
569
|
+
|
|
570
|
+
def _calculate_eta(
|
|
571
|
+
self,
|
|
572
|
+
progress: ProgressBarMixinBase,
|
|
573
|
+
data: Data,
|
|
574
|
+
value,
|
|
575
|
+
elapsed,
|
|
576
|
+
):
|
|
577
|
+
eta_seconds = ETA._calculate_eta(self, progress, data, value, elapsed)
|
|
578
|
+
now = datetime.datetime.now()
|
|
579
|
+
try:
|
|
580
|
+
return now + datetime.timedelta(seconds=eta_seconds)
|
|
581
|
+
except OverflowError: # pragma: no cover
|
|
582
|
+
return datetime.datetime.max
|
|
583
|
+
|
|
584
|
+
def __init__(
|
|
585
|
+
self,
|
|
586
|
+
format_not_started='Estimated finish time: ----/--/-- --:--:--',
|
|
587
|
+
format_finished='Finished at: %(elapsed)s',
|
|
588
|
+
format='Estimated finish time: %(eta)s',
|
|
589
|
+
**kwargs,
|
|
590
|
+
):
|
|
591
|
+
ETA.__init__(
|
|
592
|
+
self,
|
|
593
|
+
format_not_started=format_not_started,
|
|
594
|
+
format_finished=format_finished,
|
|
595
|
+
format=format,
|
|
596
|
+
**kwargs,
|
|
597
|
+
)
|
|
598
|
+
|
|
599
|
+
|
|
600
|
+
class AdaptiveETA(ETA, SamplesMixin):
|
|
601
|
+
'''WidgetBase which attempts to estimate the time of arrival.
|
|
602
|
+
|
|
603
|
+
Uses a sampled average of the speed based on the 10 last updates.
|
|
604
|
+
Very convenient for resuming the progress halfway.
|
|
605
|
+
'''
|
|
606
|
+
|
|
607
|
+
def __init__(self, **kwargs):
|
|
608
|
+
ETA.__init__(self, **kwargs)
|
|
609
|
+
SamplesMixin.__init__(self, **kwargs)
|
|
610
|
+
|
|
611
|
+
def __call__(
|
|
612
|
+
self,
|
|
613
|
+
progress: ProgressBarMixinBase,
|
|
614
|
+
data: Data,
|
|
615
|
+
value=None,
|
|
616
|
+
elapsed=None,
|
|
617
|
+
):
|
|
618
|
+
elapsed, value = SamplesMixin.__call__(
|
|
619
|
+
self,
|
|
620
|
+
progress,
|
|
621
|
+
data,
|
|
622
|
+
delta=True,
|
|
623
|
+
)
|
|
624
|
+
if not elapsed:
|
|
625
|
+
value = None
|
|
626
|
+
elapsed = 0
|
|
627
|
+
|
|
628
|
+
return ETA.__call__(self, progress, data, value=value, elapsed=elapsed)
|
|
629
|
+
|
|
630
|
+
|
|
631
|
+
class DataSize(FormatWidgetMixin, WidgetBase):
|
|
632
|
+
'''
|
|
633
|
+
Widget for showing an amount of data transferred/processed.
|
|
634
|
+
|
|
635
|
+
Automatically formats the value (assumed to be a count of bytes) with an
|
|
636
|
+
appropriate sized unit, based on the IEC binary prefixes (powers of 1024).
|
|
637
|
+
'''
|
|
638
|
+
|
|
639
|
+
def __init__(
|
|
640
|
+
self,
|
|
641
|
+
variable='value',
|
|
642
|
+
format='%(scaled)5.1f %(prefix)s%(unit)s',
|
|
643
|
+
unit='B',
|
|
644
|
+
prefixes=('', 'Ki', 'Mi', 'Gi', 'Ti', 'Pi', 'Ei', 'Zi', 'Yi'),
|
|
645
|
+
**kwargs,
|
|
646
|
+
):
|
|
647
|
+
self.variable = variable
|
|
648
|
+
self.unit = unit
|
|
649
|
+
self.prefixes = prefixes
|
|
650
|
+
FormatWidgetMixin.__init__(self, format=format, **kwargs)
|
|
651
|
+
WidgetBase.__init__(self, **kwargs)
|
|
652
|
+
|
|
653
|
+
def __call__(
|
|
654
|
+
self,
|
|
655
|
+
progress: ProgressBarMixinBase,
|
|
656
|
+
data: Data,
|
|
657
|
+
format: types.Optional[str] = None,
|
|
658
|
+
):
|
|
659
|
+
value = data[self.variable]
|
|
660
|
+
if value is not None:
|
|
661
|
+
scaled, power = utils.scale_1024(value, len(self.prefixes))
|
|
662
|
+
else:
|
|
663
|
+
scaled = power = 0
|
|
664
|
+
|
|
665
|
+
data['scaled'] = scaled
|
|
666
|
+
data['prefix'] = self.prefixes[power]
|
|
667
|
+
data['unit'] = self.unit
|
|
668
|
+
|
|
669
|
+
return FormatWidgetMixin.__call__(self, progress, data, format)
|
|
670
|
+
|
|
671
|
+
|
|
672
|
+
class FileTransferSpeed(FormatWidgetMixin, TimeSensitiveWidgetBase):
|
|
673
|
+
'''
|
|
674
|
+
Widget for showing the current transfer speed (useful for file transfers).
|
|
675
|
+
'''
|
|
676
|
+
|
|
677
|
+
def __init__(
|
|
678
|
+
self,
|
|
679
|
+
format='%(scaled)5.1f %(prefix)s%(unit)-s/s',
|
|
680
|
+
inverse_format='%(scaled)5.1f s/%(prefix)s%(unit)-s',
|
|
681
|
+
unit='B',
|
|
682
|
+
prefixes=('', 'Ki', 'Mi', 'Gi', 'Ti', 'Pi', 'Ei', 'Zi', 'Yi'),
|
|
683
|
+
**kwargs,
|
|
684
|
+
):
|
|
685
|
+
self.unit = unit
|
|
686
|
+
self.prefixes = prefixes
|
|
687
|
+
self.inverse_format = inverse_format
|
|
688
|
+
FormatWidgetMixin.__init__(self, format=format, **kwargs)
|
|
689
|
+
TimeSensitiveWidgetBase.__init__(self, **kwargs)
|
|
690
|
+
|
|
691
|
+
def _speed(self, value, elapsed):
|
|
692
|
+
speed = float(value) / elapsed
|
|
693
|
+
return utils.scale_1024(speed, len(self.prefixes))
|
|
694
|
+
|
|
695
|
+
def __call__(
|
|
696
|
+
self,
|
|
697
|
+
progress: ProgressBarMixinBase,
|
|
698
|
+
data,
|
|
699
|
+
value=None,
|
|
700
|
+
total_seconds_elapsed=None,
|
|
701
|
+
):
|
|
702
|
+
'''Updates the widget with the current SI prefixed speed.'''
|
|
703
|
+
if value is None:
|
|
704
|
+
value = data['value']
|
|
705
|
+
|
|
706
|
+
elapsed = utils.deltas_to_seconds(
|
|
707
|
+
total_seconds_elapsed,
|
|
708
|
+
data['total_seconds_elapsed'],
|
|
709
|
+
)
|
|
710
|
+
|
|
711
|
+
if (
|
|
712
|
+
value is not None
|
|
713
|
+
and elapsed is not None
|
|
714
|
+
and elapsed > 2e-6
|
|
715
|
+
and value > 2e-6
|
|
716
|
+
): # =~ 0
|
|
717
|
+
scaled, power = self._speed(value, elapsed)
|
|
718
|
+
else:
|
|
719
|
+
scaled = power = 0
|
|
720
|
+
|
|
721
|
+
data['unit'] = self.unit
|
|
722
|
+
if power == 0 and scaled < 0.1:
|
|
723
|
+
if scaled > 0:
|
|
724
|
+
scaled = 1 / scaled
|
|
725
|
+
data['scaled'] = scaled
|
|
726
|
+
data['prefix'] = self.prefixes[0]
|
|
727
|
+
return FormatWidgetMixin.__call__(
|
|
728
|
+
self,
|
|
729
|
+
progress,
|
|
730
|
+
data,
|
|
731
|
+
self.inverse_format,
|
|
732
|
+
)
|
|
733
|
+
else:
|
|
734
|
+
data['scaled'] = scaled
|
|
735
|
+
data['prefix'] = self.prefixes[power]
|
|
736
|
+
return FormatWidgetMixin.__call__(self, progress, data)
|
|
737
|
+
|
|
738
|
+
|
|
739
|
+
class AdaptiveTransferSpeed(FileTransferSpeed, SamplesMixin):
|
|
740
|
+
'''Widget for showing the transfer speed based on the last X samples.'''
|
|
741
|
+
|
|
742
|
+
def __init__(self, **kwargs):
|
|
743
|
+
FileTransferSpeed.__init__(self, **kwargs)
|
|
744
|
+
SamplesMixin.__init__(self, **kwargs)
|
|
745
|
+
|
|
746
|
+
def __call__(
|
|
747
|
+
self,
|
|
748
|
+
progress: ProgressBarMixinBase,
|
|
749
|
+
data,
|
|
750
|
+
value=None,
|
|
751
|
+
total_seconds_elapsed=None,
|
|
752
|
+
):
|
|
753
|
+
elapsed, value = SamplesMixin.__call__(
|
|
754
|
+
self,
|
|
755
|
+
progress,
|
|
756
|
+
data,
|
|
757
|
+
delta=True,
|
|
758
|
+
)
|
|
759
|
+
return FileTransferSpeed.__call__(self, progress, data, value, elapsed)
|
|
760
|
+
|
|
761
|
+
|
|
762
|
+
class AnimatedMarker(TimeSensitiveWidgetBase):
|
|
763
|
+
'''An animated marker for the progress bar which defaults to appear as if
|
|
764
|
+
it were rotating.
|
|
765
|
+
'''
|
|
766
|
+
|
|
767
|
+
def __init__(
|
|
768
|
+
self,
|
|
769
|
+
markers='|/-\\',
|
|
770
|
+
default=None,
|
|
771
|
+
fill='',
|
|
772
|
+
marker_wrap=None,
|
|
773
|
+
fill_wrap=None,
|
|
774
|
+
**kwargs,
|
|
775
|
+
):
|
|
776
|
+
self.markers = markers
|
|
777
|
+
self.marker_wrap = create_wrapper(marker_wrap)
|
|
778
|
+
self.default = default or markers[0]
|
|
779
|
+
self.fill_wrap = create_wrapper(fill_wrap)
|
|
780
|
+
self.fill = create_marker(fill, self.fill_wrap) if fill else None
|
|
781
|
+
WidgetBase.__init__(self, **kwargs)
|
|
782
|
+
|
|
783
|
+
def __call__(self, progress: ProgressBarMixinBase, data: Data, width=None):
|
|
784
|
+
'''Updates the widget to show the next marker or the first marker when
|
|
785
|
+
finished.
|
|
786
|
+
'''
|
|
787
|
+
if progress.end_time:
|
|
788
|
+
return self.default
|
|
789
|
+
|
|
790
|
+
marker = self.markers[data['updates'] % len(self.markers)]
|
|
791
|
+
if self.marker_wrap:
|
|
792
|
+
marker = self.marker_wrap.format(marker)
|
|
793
|
+
|
|
794
|
+
if self.fill:
|
|
795
|
+
# Cut the last character so we can replace it with our marker
|
|
796
|
+
fill = self.fill(
|
|
797
|
+
progress,
|
|
798
|
+
data,
|
|
799
|
+
width - progress.custom_len(marker), # type: ignore
|
|
800
|
+
)
|
|
801
|
+
else:
|
|
802
|
+
fill = ''
|
|
803
|
+
|
|
804
|
+
# Python 3 returns an int when indexing bytes
|
|
805
|
+
if isinstance(marker, int): # pragma: no cover
|
|
806
|
+
marker = bytes(marker)
|
|
807
|
+
fill = fill.encode()
|
|
808
|
+
else:
|
|
809
|
+
# cast fill to the same type as marker
|
|
810
|
+
fill = type(marker)(fill)
|
|
811
|
+
|
|
812
|
+
return fill + marker # type: ignore
|
|
813
|
+
|
|
814
|
+
|
|
815
|
+
# Alias for backwards compatibility
|
|
816
|
+
RotatingMarker = AnimatedMarker
|
|
817
|
+
|
|
818
|
+
|
|
819
|
+
class Counter(FormatWidgetMixin, WidgetBase):
|
|
820
|
+
'''Displays the current count.'''
|
|
821
|
+
|
|
822
|
+
def __init__(self, format='%(value)d', **kwargs):
|
|
823
|
+
FormatWidgetMixin.__init__(self, format=format, **kwargs)
|
|
824
|
+
WidgetBase.__init__(self, format=format, **kwargs)
|
|
825
|
+
|
|
826
|
+
def __call__(
|
|
827
|
+
self,
|
|
828
|
+
progress: ProgressBarMixinBase,
|
|
829
|
+
data: Data,
|
|
830
|
+
format=None,
|
|
831
|
+
):
|
|
832
|
+
return FormatWidgetMixin.__call__(self, progress, data, format)
|
|
833
|
+
|
|
834
|
+
|
|
835
|
+
class ColoredMixin:
|
|
836
|
+
_fixed_colors: ClassVar[TFixedColors] = TFixedColors(
|
|
837
|
+
fg_none=colors.yellow,
|
|
838
|
+
bg_none=None,
|
|
839
|
+
)
|
|
840
|
+
_gradient_colors: ClassVar[TGradientColors] = TGradientColors(
|
|
841
|
+
fg=colors.gradient,
|
|
842
|
+
bg=None,
|
|
843
|
+
)
|
|
844
|
+
# _fixed_colors: ClassVar[dict[str, terminal.Color | None]] = dict(
|
|
845
|
+
# fg_none=colors.yellow, bg_none=None)
|
|
846
|
+
# _gradient_colors: ClassVar[dict[str, terminal.OptionalColor |
|
|
847
|
+
# None]] = dict(fg=colors.gradient,
|
|
848
|
+
# bg=None)
|
|
849
|
+
|
|
850
|
+
|
|
851
|
+
class Percentage(FormatWidgetMixin, ColoredMixin, WidgetBase):
|
|
852
|
+
'''Displays the current percentage as a number with a percent sign.'''
|
|
853
|
+
|
|
854
|
+
def __init__(self, format='%(percentage)3d%%', na='N/A%%', **kwargs):
|
|
855
|
+
self.na = na
|
|
856
|
+
FormatWidgetMixin.__init__(self, format=format, **kwargs)
|
|
857
|
+
WidgetBase.__init__(self, format=format, **kwargs)
|
|
858
|
+
|
|
859
|
+
def get_format(
|
|
860
|
+
self,
|
|
861
|
+
progress: ProgressBarMixinBase,
|
|
862
|
+
data: Data,
|
|
863
|
+
format=None,
|
|
864
|
+
):
|
|
865
|
+
# If percentage is not available, display N/A%
|
|
866
|
+
percentage = data.get('percentage', base.Undefined)
|
|
867
|
+
if not percentage and percentage != 0:
|
|
868
|
+
output = self.na
|
|
869
|
+
else:
|
|
870
|
+
output = FormatWidgetMixin.get_format(self, progress, data, format)
|
|
871
|
+
|
|
872
|
+
return self._apply_colors(output, data)
|
|
873
|
+
|
|
874
|
+
|
|
875
|
+
class SimpleProgress(FormatWidgetMixin, ColoredMixin, WidgetBase):
|
|
876
|
+
'''Returns progress as a count of the total (e.g.: "5 of 47").'''
|
|
877
|
+
|
|
878
|
+
max_width_cache: dict[
|
|
879
|
+
types.Union[str, tuple[float, float | types.Type[base.UnknownLength]]],
|
|
880
|
+
types.Optional[int],
|
|
881
|
+
]
|
|
882
|
+
|
|
883
|
+
DEFAULT_FORMAT = '%(value_s)s of %(max_value_s)s'
|
|
884
|
+
|
|
885
|
+
def __init__(self, format=DEFAULT_FORMAT, **kwargs):
|
|
886
|
+
FormatWidgetMixin.__init__(self, format=format, **kwargs)
|
|
887
|
+
WidgetBase.__init__(self, format=format, **kwargs)
|
|
888
|
+
self.max_width_cache = dict(default=self.max_width or 0)
|
|
889
|
+
|
|
890
|
+
def __call__(
|
|
891
|
+
self,
|
|
892
|
+
progress: ProgressBarMixinBase,
|
|
893
|
+
data: Data,
|
|
894
|
+
format=None,
|
|
895
|
+
):
|
|
896
|
+
# If max_value is not available, display N/A
|
|
897
|
+
if data.get('max_value'):
|
|
898
|
+
data['max_value_s'] = data['max_value']
|
|
899
|
+
else:
|
|
900
|
+
data['max_value_s'] = 'N/A'
|
|
901
|
+
|
|
902
|
+
# if value is not available it's the zeroth iteration
|
|
903
|
+
if data.get('value'):
|
|
904
|
+
data['value_s'] = data['value']
|
|
905
|
+
else:
|
|
906
|
+
data['value_s'] = 0
|
|
907
|
+
|
|
908
|
+
formatted = FormatWidgetMixin.__call__(
|
|
909
|
+
self,
|
|
910
|
+
progress,
|
|
911
|
+
data,
|
|
912
|
+
format=format,
|
|
913
|
+
)
|
|
914
|
+
|
|
915
|
+
# Guess the maximum width from the min and max value
|
|
916
|
+
key = progress.min_value, progress.max_value
|
|
917
|
+
max_width: types.Optional[int] = self.max_width_cache.get(
|
|
918
|
+
key,
|
|
919
|
+
self.max_width,
|
|
920
|
+
)
|
|
921
|
+
if not max_width:
|
|
922
|
+
temporary_data = data.copy()
|
|
923
|
+
for value in key:
|
|
924
|
+
if value is None: # pragma: no cover
|
|
925
|
+
continue
|
|
926
|
+
|
|
927
|
+
temporary_data['value'] = value
|
|
928
|
+
if width := progress.custom_len( # pragma: no branch
|
|
929
|
+
FormatWidgetMixin.__call__(
|
|
930
|
+
self,
|
|
931
|
+
progress,
|
|
932
|
+
temporary_data,
|
|
933
|
+
format=format,
|
|
934
|
+
),
|
|
935
|
+
):
|
|
936
|
+
max_width = max(max_width or 0, width)
|
|
937
|
+
|
|
938
|
+
self.max_width_cache[key] = max_width
|
|
939
|
+
|
|
940
|
+
# Adjust the output to have a consistent size in all cases
|
|
941
|
+
if max_width: # pragma: no branch
|
|
942
|
+
formatted = formatted.rjust(max_width)
|
|
943
|
+
|
|
944
|
+
return self._apply_colors(formatted, data)
|
|
945
|
+
|
|
946
|
+
|
|
947
|
+
class Bar(AutoWidthWidgetBase):
|
|
948
|
+
'''A progress bar which stretches to fill the line.'''
|
|
949
|
+
|
|
950
|
+
fg: terminal.OptionalColor | None = colors.gradient
|
|
951
|
+
bg: terminal.OptionalColor | None = None
|
|
952
|
+
|
|
953
|
+
def __init__(
|
|
954
|
+
self,
|
|
955
|
+
marker='#',
|
|
956
|
+
left='|',
|
|
957
|
+
right='|',
|
|
958
|
+
fill=' ',
|
|
959
|
+
fill_left=True,
|
|
960
|
+
marker_wrap=None,
|
|
961
|
+
**kwargs,
|
|
962
|
+
):
|
|
963
|
+
'''Creates a customizable progress bar.
|
|
964
|
+
|
|
965
|
+
The callable takes the same parameters as the `__call__` method
|
|
966
|
+
|
|
967
|
+
marker - string or callable object to use as a marker
|
|
968
|
+
left - string or callable object to use as a left border
|
|
969
|
+
right - string or callable object to use as a right border
|
|
970
|
+
fill - character to use for the empty part of the progress bar
|
|
971
|
+
fill_left - whether to fill from the left or the right
|
|
972
|
+
'''
|
|
973
|
+
self.marker = create_marker(marker, marker_wrap)
|
|
974
|
+
self.left = string_or_lambda(left)
|
|
975
|
+
self.right = string_or_lambda(right)
|
|
976
|
+
self.fill = string_or_lambda(fill)
|
|
977
|
+
self.fill_left = fill_left
|
|
978
|
+
|
|
979
|
+
AutoWidthWidgetBase.__init__(self, **kwargs)
|
|
980
|
+
|
|
981
|
+
def __call__(
|
|
982
|
+
self,
|
|
983
|
+
progress: ProgressBarMixinBase,
|
|
984
|
+
data: Data,
|
|
985
|
+
width: int = 0,
|
|
986
|
+
color=True,
|
|
987
|
+
):
|
|
988
|
+
'''Updates the progress bar and its subcomponents.'''
|
|
989
|
+
left = converters.to_unicode(self.left(progress, data, width))
|
|
990
|
+
right = converters.to_unicode(self.right(progress, data, width))
|
|
991
|
+
width -= progress.custom_len(left) + progress.custom_len(right)
|
|
992
|
+
marker = converters.to_unicode(self.marker(progress, data, width))
|
|
993
|
+
fill = converters.to_unicode(self.fill(progress, data, width))
|
|
994
|
+
|
|
995
|
+
# Make sure we ignore invisible characters when filling
|
|
996
|
+
width += len(marker) - progress.custom_len(marker)
|
|
997
|
+
|
|
998
|
+
if self.fill_left:
|
|
999
|
+
marker = marker.ljust(width, fill)
|
|
1000
|
+
else:
|
|
1001
|
+
marker = marker.rjust(width, fill)
|
|
1002
|
+
|
|
1003
|
+
if color:
|
|
1004
|
+
marker = self._apply_colors(marker, data)
|
|
1005
|
+
|
|
1006
|
+
return left + marker + right
|
|
1007
|
+
|
|
1008
|
+
|
|
1009
|
+
class ReverseBar(Bar):
|
|
1010
|
+
'''A bar which has a marker that goes from right to left.'''
|
|
1011
|
+
|
|
1012
|
+
def __init__(
|
|
1013
|
+
self,
|
|
1014
|
+
marker='#',
|
|
1015
|
+
left='|',
|
|
1016
|
+
right='|',
|
|
1017
|
+
fill=' ',
|
|
1018
|
+
fill_left=False,
|
|
1019
|
+
**kwargs,
|
|
1020
|
+
):
|
|
1021
|
+
'''Creates a customizable progress bar.
|
|
1022
|
+
|
|
1023
|
+
marker - string or updatable object to use as a marker
|
|
1024
|
+
left - string or updatable object to use as a left border
|
|
1025
|
+
right - string or updatable object to use as a right border
|
|
1026
|
+
fill - character to use for the empty part of the progress bar
|
|
1027
|
+
fill_left - whether to fill from the left or the right
|
|
1028
|
+
'''
|
|
1029
|
+
Bar.__init__(
|
|
1030
|
+
self,
|
|
1031
|
+
marker=marker,
|
|
1032
|
+
left=left,
|
|
1033
|
+
right=right,
|
|
1034
|
+
fill=fill,
|
|
1035
|
+
fill_left=fill_left,
|
|
1036
|
+
**kwargs,
|
|
1037
|
+
)
|
|
1038
|
+
|
|
1039
|
+
|
|
1040
|
+
class BouncingBar(Bar, TimeSensitiveWidgetBase):
|
|
1041
|
+
'''A bar which has a marker which bounces from side to side.'''
|
|
1042
|
+
|
|
1043
|
+
INTERVAL = datetime.timedelta(milliseconds=100)
|
|
1044
|
+
|
|
1045
|
+
def __call__(
|
|
1046
|
+
self,
|
|
1047
|
+
progress: ProgressBarMixinBase,
|
|
1048
|
+
data: Data,
|
|
1049
|
+
width: int = 0,
|
|
1050
|
+
color=True,
|
|
1051
|
+
):
|
|
1052
|
+
'''Updates the progress bar and its subcomponents.'''
|
|
1053
|
+
left = converters.to_unicode(self.left(progress, data, width))
|
|
1054
|
+
right = converters.to_unicode(self.right(progress, data, width))
|
|
1055
|
+
width -= progress.custom_len(left) + progress.custom_len(right)
|
|
1056
|
+
marker = converters.to_unicode(self.marker(progress, data, width))
|
|
1057
|
+
|
|
1058
|
+
fill = converters.to_unicode(self.fill(progress, data, width))
|
|
1059
|
+
|
|
1060
|
+
if width: # pragma: no branch
|
|
1061
|
+
value = int(
|
|
1062
|
+
data['total_seconds_elapsed'] / self.INTERVAL.total_seconds(),
|
|
1063
|
+
)
|
|
1064
|
+
|
|
1065
|
+
a = value % width
|
|
1066
|
+
b = width - a - 1
|
|
1067
|
+
if value % (width * 2) >= width:
|
|
1068
|
+
a, b = b, a
|
|
1069
|
+
|
|
1070
|
+
if self.fill_left:
|
|
1071
|
+
marker = a * fill + marker + b * fill
|
|
1072
|
+
else:
|
|
1073
|
+
marker = b * fill + marker + a * fill
|
|
1074
|
+
|
|
1075
|
+
return left + marker + right
|
|
1076
|
+
|
|
1077
|
+
|
|
1078
|
+
class FormatCustomText(FormatWidgetMixin, WidgetBase):
|
|
1079
|
+
mapping: types.Dict[str, types.Any] = dict() # noqa: RUF012
|
|
1080
|
+
copy = False
|
|
1081
|
+
|
|
1082
|
+
def __init__(
|
|
1083
|
+
self,
|
|
1084
|
+
format: str,
|
|
1085
|
+
mapping: types.Optional[types.Dict[str, types.Any]] = None,
|
|
1086
|
+
**kwargs,
|
|
1087
|
+
):
|
|
1088
|
+
self.format = format
|
|
1089
|
+
self.mapping = mapping or self.mapping
|
|
1090
|
+
FormatWidgetMixin.__init__(self, format=format, **kwargs)
|
|
1091
|
+
WidgetBase.__init__(self, **kwargs)
|
|
1092
|
+
|
|
1093
|
+
def update_mapping(self, **mapping: types.Dict[str, types.Any]):
|
|
1094
|
+
self.mapping.update(mapping)
|
|
1095
|
+
|
|
1096
|
+
def __call__(
|
|
1097
|
+
self,
|
|
1098
|
+
progress: ProgressBarMixinBase,
|
|
1099
|
+
data: Data,
|
|
1100
|
+
format: types.Optional[str] = None,
|
|
1101
|
+
):
|
|
1102
|
+
return FormatWidgetMixin.__call__(
|
|
1103
|
+
self,
|
|
1104
|
+
progress,
|
|
1105
|
+
self.mapping,
|
|
1106
|
+
format or self.format,
|
|
1107
|
+
)
|
|
1108
|
+
|
|
1109
|
+
|
|
1110
|
+
class VariableMixin:
|
|
1111
|
+
'''Mixin to display a custom user variable.'''
|
|
1112
|
+
|
|
1113
|
+
def __init__(self, name, **kwargs):
|
|
1114
|
+
if not isinstance(name, str):
|
|
1115
|
+
raise TypeError('Variable(): argument must be a string')
|
|
1116
|
+
if len(name.split()) > 1:
|
|
1117
|
+
raise ValueError('Variable(): argument must be single word')
|
|
1118
|
+
self.name = name
|
|
1119
|
+
|
|
1120
|
+
|
|
1121
|
+
class MultiRangeBar(Bar, VariableMixin):
|
|
1122
|
+
'''
|
|
1123
|
+
A bar with multiple sub-ranges, each represented by a different symbol.
|
|
1124
|
+
|
|
1125
|
+
The various ranges are represented on a user-defined variable, formatted as
|
|
1126
|
+
|
|
1127
|
+
.. code-block:: python
|
|
1128
|
+
|
|
1129
|
+
[
|
|
1130
|
+
['Symbol1', amount1],
|
|
1131
|
+
['Symbol2', amount2],
|
|
1132
|
+
...
|
|
1133
|
+
]
|
|
1134
|
+
'''
|
|
1135
|
+
|
|
1136
|
+
def __init__(self, name, markers, **kwargs):
|
|
1137
|
+
VariableMixin.__init__(self, name)
|
|
1138
|
+
Bar.__init__(self, **kwargs)
|
|
1139
|
+
self.markers = [string_or_lambda(marker) for marker in markers]
|
|
1140
|
+
|
|
1141
|
+
def get_values(self, progress: ProgressBarMixinBase, data: Data):
|
|
1142
|
+
return data['variables'][self.name] or []
|
|
1143
|
+
|
|
1144
|
+
def __call__(
|
|
1145
|
+
self,
|
|
1146
|
+
progress: ProgressBarMixinBase,
|
|
1147
|
+
data: Data,
|
|
1148
|
+
width: int = 0,
|
|
1149
|
+
color=True,
|
|
1150
|
+
):
|
|
1151
|
+
'''Updates the progress bar and its subcomponents.'''
|
|
1152
|
+
left = converters.to_unicode(self.left(progress, data, width))
|
|
1153
|
+
right = converters.to_unicode(self.right(progress, data, width))
|
|
1154
|
+
width -= progress.custom_len(left) + progress.custom_len(right)
|
|
1155
|
+
values = self.get_values(progress, data)
|
|
1156
|
+
|
|
1157
|
+
values_sum = sum(values)
|
|
1158
|
+
if width and values_sum:
|
|
1159
|
+
middle = ''
|
|
1160
|
+
values_accumulated = 0
|
|
1161
|
+
width_accumulated = 0
|
|
1162
|
+
for marker, value in zip(self.markers, values):
|
|
1163
|
+
marker = converters.to_unicode(marker(progress, data, width))
|
|
1164
|
+
assert progress.custom_len(marker) == 1
|
|
1165
|
+
|
|
1166
|
+
values_accumulated += value
|
|
1167
|
+
item_width = int(values_accumulated / values_sum * width)
|
|
1168
|
+
item_width -= width_accumulated
|
|
1169
|
+
width_accumulated += item_width
|
|
1170
|
+
middle += item_width * marker
|
|
1171
|
+
else:
|
|
1172
|
+
fill = converters.to_unicode(self.fill(progress, data, width))
|
|
1173
|
+
assert progress.custom_len(fill) == 1
|
|
1174
|
+
middle = fill * width
|
|
1175
|
+
|
|
1176
|
+
return left + middle + right
|
|
1177
|
+
|
|
1178
|
+
|
|
1179
|
+
class MultiProgressBar(MultiRangeBar):
|
|
1180
|
+
def __init__(
|
|
1181
|
+
self,
|
|
1182
|
+
name,
|
|
1183
|
+
# NOTE: the markers are not whitespace even though some
|
|
1184
|
+
# terminals don't show the characters correctly!
|
|
1185
|
+
markers=' ▁▂▃▄▅▆▇█',
|
|
1186
|
+
**kwargs,
|
|
1187
|
+
):
|
|
1188
|
+
MultiRangeBar.__init__(
|
|
1189
|
+
self,
|
|
1190
|
+
name=name,
|
|
1191
|
+
markers=list(reversed(markers)),
|
|
1192
|
+
**kwargs,
|
|
1193
|
+
)
|
|
1194
|
+
|
|
1195
|
+
def get_values(self, progress: ProgressBarMixinBase, data: Data):
|
|
1196
|
+
ranges = [0.0] * len(self.markers)
|
|
1197
|
+
for value in data['variables'][self.name] or []:
|
|
1198
|
+
if not isinstance(value, (int, float)):
|
|
1199
|
+
# Progress is (value, max)
|
|
1200
|
+
progress_value, progress_max = value
|
|
1201
|
+
value = float(progress_value) / float(progress_max)
|
|
1202
|
+
|
|
1203
|
+
if not 0 <= value <= 1:
|
|
1204
|
+
raise ValueError(
|
|
1205
|
+
f'Range value needs to be in the range [0..1], got {value}',
|
|
1206
|
+
)
|
|
1207
|
+
|
|
1208
|
+
range_ = value * (len(ranges) - 1)
|
|
1209
|
+
pos = int(range_)
|
|
1210
|
+
frac = range_ % 1
|
|
1211
|
+
ranges[pos] += 1 - frac
|
|
1212
|
+
if frac:
|
|
1213
|
+
ranges[pos + 1] += frac
|
|
1214
|
+
|
|
1215
|
+
if self.fill_left: # pragma: no branch
|
|
1216
|
+
ranges = list(reversed(ranges))
|
|
1217
|
+
|
|
1218
|
+
return ranges
|
|
1219
|
+
|
|
1220
|
+
|
|
1221
|
+
class GranularMarkers:
|
|
1222
|
+
smooth = ' ▏▎▍▌▋▊▉█'
|
|
1223
|
+
bar = ' ▁▂▃▄▅▆▇█'
|
|
1224
|
+
snake = ' ▖▌▛█'
|
|
1225
|
+
fade_in = ' ░▒▓█'
|
|
1226
|
+
dots = ' ⡀⡄⡆⡇⣇⣧⣷⣿'
|
|
1227
|
+
growing_circles = ' .oO'
|
|
1228
|
+
|
|
1229
|
+
|
|
1230
|
+
class GranularBar(AutoWidthWidgetBase):
|
|
1231
|
+
'''A progressbar that can display progress at a sub-character granularity
|
|
1232
|
+
by using multiple marker characters.
|
|
1233
|
+
|
|
1234
|
+
Examples of markers:
|
|
1235
|
+
- Smooth: ` ▏▎▍▌▋▊▉█` (default)
|
|
1236
|
+
- Bar: ` ▁▂▃▄▅▆▇█`
|
|
1237
|
+
- Snake: ` ▖▌▛█`
|
|
1238
|
+
- Fade in: ` ░▒▓█`
|
|
1239
|
+
- Dots: ` ⡀⡄⡆⡇⣇⣧⣷⣿`
|
|
1240
|
+
- Growing circles: ` .oO`
|
|
1241
|
+
|
|
1242
|
+
The markers can be accessed through GranularMarkers. GranularMarkers.dots
|
|
1243
|
+
for example
|
|
1244
|
+
'''
|
|
1245
|
+
|
|
1246
|
+
def __init__(
|
|
1247
|
+
self,
|
|
1248
|
+
markers=GranularMarkers.smooth,
|
|
1249
|
+
left='|',
|
|
1250
|
+
right='|',
|
|
1251
|
+
**kwargs,
|
|
1252
|
+
):
|
|
1253
|
+
'''Creates a customizable progress bar.
|
|
1254
|
+
|
|
1255
|
+
markers - string of characters to use as granular progress markers. The
|
|
1256
|
+
first character should represent 0% and the last 100%.
|
|
1257
|
+
Ex: ` .oO`.
|
|
1258
|
+
left - string or callable object to use as a left border
|
|
1259
|
+
right - string or callable object to use as a right border
|
|
1260
|
+
'''
|
|
1261
|
+
self.markers = markers
|
|
1262
|
+
self.left = string_or_lambda(left)
|
|
1263
|
+
self.right = string_or_lambda(right)
|
|
1264
|
+
|
|
1265
|
+
AutoWidthWidgetBase.__init__(self, **kwargs)
|
|
1266
|
+
|
|
1267
|
+
def __call__(
|
|
1268
|
+
self,
|
|
1269
|
+
progress: ProgressBarMixinBase,
|
|
1270
|
+
data: Data,
|
|
1271
|
+
width: int = 0,
|
|
1272
|
+
):
|
|
1273
|
+
left = converters.to_unicode(self.left(progress, data, width))
|
|
1274
|
+
right = converters.to_unicode(self.right(progress, data, width))
|
|
1275
|
+
width -= progress.custom_len(left) + progress.custom_len(right)
|
|
1276
|
+
|
|
1277
|
+
max_value = progress.max_value
|
|
1278
|
+
# mypy doesn't get that the first part of the if statement makes sure
|
|
1279
|
+
# we get the correct type
|
|
1280
|
+
if (
|
|
1281
|
+
max_value is not base.UnknownLength
|
|
1282
|
+
and max_value > 0 # type: ignore
|
|
1283
|
+
):
|
|
1284
|
+
percent = progress.value / max_value # type: ignore
|
|
1285
|
+
else:
|
|
1286
|
+
percent = 0
|
|
1287
|
+
|
|
1288
|
+
num_chars = percent * width
|
|
1289
|
+
|
|
1290
|
+
marker = self.markers[-1] * int(num_chars)
|
|
1291
|
+
|
|
1292
|
+
if marker_idx := int((num_chars % 1) * (len(self.markers) - 1)):
|
|
1293
|
+
marker += self.markers[marker_idx]
|
|
1294
|
+
|
|
1295
|
+
marker = converters.to_unicode(marker)
|
|
1296
|
+
|
|
1297
|
+
# Make sure we ignore invisible characters when filling
|
|
1298
|
+
width += len(marker) - progress.custom_len(marker)
|
|
1299
|
+
marker = marker.ljust(width, self.markers[0])
|
|
1300
|
+
|
|
1301
|
+
return left + marker + right
|
|
1302
|
+
|
|
1303
|
+
|
|
1304
|
+
class FormatLabelBar(FormatLabel, Bar):
|
|
1305
|
+
'''A bar which has a formatted label in the center.'''
|
|
1306
|
+
|
|
1307
|
+
def __init__(self, format, **kwargs):
|
|
1308
|
+
FormatLabel.__init__(self, format, **kwargs)
|
|
1309
|
+
Bar.__init__(self, **kwargs)
|
|
1310
|
+
|
|
1311
|
+
def __call__( # type: ignore
|
|
1312
|
+
self,
|
|
1313
|
+
progress: ProgressBarMixinBase,
|
|
1314
|
+
data: Data,
|
|
1315
|
+
width: int = 0,
|
|
1316
|
+
format: FormatString = None,
|
|
1317
|
+
):
|
|
1318
|
+
center = FormatLabel.__call__(self, progress, data, format=format)
|
|
1319
|
+
bar = Bar.__call__(self, progress, data, width, color=False)
|
|
1320
|
+
|
|
1321
|
+
# Aligns the center of the label to the center of the bar
|
|
1322
|
+
center_len = progress.custom_len(center)
|
|
1323
|
+
center_left = int((width - center_len) / 2)
|
|
1324
|
+
center_right = center_left + center_len
|
|
1325
|
+
|
|
1326
|
+
return (
|
|
1327
|
+
self._apply_colors(
|
|
1328
|
+
bar[:center_left],
|
|
1329
|
+
data,
|
|
1330
|
+
)
|
|
1331
|
+
+ self._apply_colors(
|
|
1332
|
+
center,
|
|
1333
|
+
data,
|
|
1334
|
+
)
|
|
1335
|
+
+ self._apply_colors(
|
|
1336
|
+
bar[center_right:],
|
|
1337
|
+
data,
|
|
1338
|
+
)
|
|
1339
|
+
)
|
|
1340
|
+
|
|
1341
|
+
|
|
1342
|
+
class PercentageLabelBar(Percentage, FormatLabelBar):
|
|
1343
|
+
'''A bar which displays the current percentage in the center.'''
|
|
1344
|
+
|
|
1345
|
+
# %3d adds an extra space that makes it look off-center
|
|
1346
|
+
# %2d keeps the label somewhat consistently in-place
|
|
1347
|
+
def __init__(self, format='%(percentage)2d%%', na='N/A%%', **kwargs):
|
|
1348
|
+
Percentage.__init__(self, format, na=na, **kwargs)
|
|
1349
|
+
FormatLabelBar.__init__(self, format, **kwargs)
|
|
1350
|
+
|
|
1351
|
+
def __call__( # type: ignore
|
|
1352
|
+
self,
|
|
1353
|
+
progress: ProgressBarMixinBase,
|
|
1354
|
+
data: Data,
|
|
1355
|
+
width: int = 0,
|
|
1356
|
+
format: FormatString = None,
|
|
1357
|
+
):
|
|
1358
|
+
return super().__call__(progress, data, width, format=format)
|
|
1359
|
+
|
|
1360
|
+
|
|
1361
|
+
class Variable(FormatWidgetMixin, VariableMixin, WidgetBase):
|
|
1362
|
+
'''Displays a custom variable.'''
|
|
1363
|
+
|
|
1364
|
+
def __init__(
|
|
1365
|
+
self,
|
|
1366
|
+
name,
|
|
1367
|
+
format='{name}: {formatted_value}',
|
|
1368
|
+
width=6,
|
|
1369
|
+
precision=3,
|
|
1370
|
+
**kwargs,
|
|
1371
|
+
):
|
|
1372
|
+
'''Creates a Variable associated with the given name.'''
|
|
1373
|
+
self.format = format
|
|
1374
|
+
self.width = width
|
|
1375
|
+
self.precision = precision
|
|
1376
|
+
VariableMixin.__init__(self, name=name)
|
|
1377
|
+
WidgetBase.__init__(self, **kwargs)
|
|
1378
|
+
|
|
1379
|
+
def __call__(
|
|
1380
|
+
self,
|
|
1381
|
+
progress: ProgressBarMixinBase,
|
|
1382
|
+
data: Data,
|
|
1383
|
+
format: types.Optional[str] = None,
|
|
1384
|
+
):
|
|
1385
|
+
value = data['variables'][self.name]
|
|
1386
|
+
context = data.copy()
|
|
1387
|
+
context['value'] = value
|
|
1388
|
+
context['name'] = self.name
|
|
1389
|
+
context['width'] = self.width
|
|
1390
|
+
context['precision'] = self.precision
|
|
1391
|
+
|
|
1392
|
+
try:
|
|
1393
|
+
# Make sure to try and cast the value first, otherwise the
|
|
1394
|
+
# formatting will generate warnings/errors on newer Python releases
|
|
1395
|
+
value = float(value)
|
|
1396
|
+
fmt = '{value:{width}.{precision}}'
|
|
1397
|
+
context['formatted_value'] = fmt.format(**context)
|
|
1398
|
+
except (TypeError, ValueError):
|
|
1399
|
+
if value:
|
|
1400
|
+
context['formatted_value'] = '{value:{width}}'.format(
|
|
1401
|
+
**context,
|
|
1402
|
+
)
|
|
1403
|
+
else:
|
|
1404
|
+
context['formatted_value'] = '-' * self.width
|
|
1405
|
+
|
|
1406
|
+
return self.format.format(**context)
|
|
1407
|
+
|
|
1408
|
+
|
|
1409
|
+
class DynamicMessage(Variable):
|
|
1410
|
+
'''Kept for backwards compatibility, please use `Variable` instead.'''
|
|
1411
|
+
|
|
1412
|
+
|
|
1413
|
+
class CurrentTime(FormatWidgetMixin, TimeSensitiveWidgetBase):
|
|
1414
|
+
'''Widget which displays the current (date)time with seconds resolution.'''
|
|
1415
|
+
|
|
1416
|
+
INTERVAL = datetime.timedelta(seconds=1)
|
|
1417
|
+
|
|
1418
|
+
def __init__(
|
|
1419
|
+
self,
|
|
1420
|
+
format='Current Time: %(current_time)s',
|
|
1421
|
+
microseconds=False,
|
|
1422
|
+
**kwargs,
|
|
1423
|
+
):
|
|
1424
|
+
self.microseconds = microseconds
|
|
1425
|
+
FormatWidgetMixin.__init__(self, format=format, **kwargs)
|
|
1426
|
+
TimeSensitiveWidgetBase.__init__(self, **kwargs)
|
|
1427
|
+
|
|
1428
|
+
def __call__(
|
|
1429
|
+
self,
|
|
1430
|
+
progress: ProgressBarMixinBase,
|
|
1431
|
+
data: Data,
|
|
1432
|
+
format: types.Optional[str] = None,
|
|
1433
|
+
):
|
|
1434
|
+
data['current_time'] = self.current_time()
|
|
1435
|
+
data['current_datetime'] = self.current_datetime()
|
|
1436
|
+
|
|
1437
|
+
return FormatWidgetMixin.__call__(self, progress, data, format=format)
|
|
1438
|
+
|
|
1439
|
+
def current_datetime(self):
|
|
1440
|
+
now = datetime.datetime.now()
|
|
1441
|
+
if not self.microseconds:
|
|
1442
|
+
now = now.replace(microsecond=0)
|
|
1443
|
+
|
|
1444
|
+
return now
|
|
1445
|
+
|
|
1446
|
+
def current_time(self):
|
|
1447
|
+
return self.current_datetime().time()
|
|
1448
|
+
|
|
1449
|
+
|
|
1450
|
+
class JobStatusBar(Bar, VariableMixin):
|
|
1451
|
+
'''
|
|
1452
|
+
Widget which displays the job status as markers on the bar.
|
|
1453
|
+
|
|
1454
|
+
The status updates can be given either as a boolean or as a string. If it's
|
|
1455
|
+
a string, it will be displayed as-is. If it's a boolean, it will be
|
|
1456
|
+
displayed as a marker (default: '█' for success, 'X' for failure)
|
|
1457
|
+
configurable through the `success_marker` and `failure_marker` parameters.
|
|
1458
|
+
|
|
1459
|
+
Args:
|
|
1460
|
+
name: The name of the variable to use for the status updates.
|
|
1461
|
+
left: The left border of the bar.
|
|
1462
|
+
right: The right border of the bar.
|
|
1463
|
+
fill: The fill character of the bar.
|
|
1464
|
+
fill_left: Whether to fill the bar from the left or the right.
|
|
1465
|
+
success_fg_color: The foreground color to use for successful jobs.
|
|
1466
|
+
success_bg_color: The background color to use for successful jobs.
|
|
1467
|
+
success_marker: The marker to use for successful jobs.
|
|
1468
|
+
failure_fg_color: The foreground color to use for failed jobs.
|
|
1469
|
+
failure_bg_color: The background color to use for failed jobs.
|
|
1470
|
+
failure_marker: The marker to use for failed jobs.
|
|
1471
|
+
'''
|
|
1472
|
+
|
|
1473
|
+
success_fg_color: terminal.Color | None = colors.green
|
|
1474
|
+
success_bg_color: terminal.Color | None = None
|
|
1475
|
+
success_marker: str = '█'
|
|
1476
|
+
failure_fg_color: terminal.Color | None = colors.red
|
|
1477
|
+
failure_bg_color: terminal.Color | None = None
|
|
1478
|
+
failure_marker: str = 'X'
|
|
1479
|
+
job_markers: list[str]
|
|
1480
|
+
|
|
1481
|
+
def __init__(
|
|
1482
|
+
self,
|
|
1483
|
+
name: str,
|
|
1484
|
+
left='|',
|
|
1485
|
+
right='|',
|
|
1486
|
+
fill=' ',
|
|
1487
|
+
fill_left=True,
|
|
1488
|
+
success_fg_color=colors.green,
|
|
1489
|
+
success_bg_color=None,
|
|
1490
|
+
success_marker='█',
|
|
1491
|
+
failure_fg_color=colors.red,
|
|
1492
|
+
failure_bg_color=None,
|
|
1493
|
+
failure_marker='X',
|
|
1494
|
+
**kwargs,
|
|
1495
|
+
):
|
|
1496
|
+
VariableMixin.__init__(self, name)
|
|
1497
|
+
self.name = name
|
|
1498
|
+
self.job_markers = []
|
|
1499
|
+
self.left = string_or_lambda(left)
|
|
1500
|
+
self.right = string_or_lambda(right)
|
|
1501
|
+
self.fill = string_or_lambda(fill)
|
|
1502
|
+
self.success_fg_color = success_fg_color
|
|
1503
|
+
self.success_bg_color = success_bg_color
|
|
1504
|
+
self.success_marker = success_marker
|
|
1505
|
+
self.failure_fg_color = failure_fg_color
|
|
1506
|
+
self.failure_bg_color = failure_bg_color
|
|
1507
|
+
self.failure_marker = failure_marker
|
|
1508
|
+
|
|
1509
|
+
Bar.__init__(
|
|
1510
|
+
self,
|
|
1511
|
+
left=left,
|
|
1512
|
+
right=right,
|
|
1513
|
+
fill=fill,
|
|
1514
|
+
fill_left=fill_left,
|
|
1515
|
+
**kwargs,
|
|
1516
|
+
)
|
|
1517
|
+
|
|
1518
|
+
def __call__(
|
|
1519
|
+
self,
|
|
1520
|
+
progress: ProgressBarMixinBase,
|
|
1521
|
+
data: Data,
|
|
1522
|
+
width: int = 0,
|
|
1523
|
+
color=True,
|
|
1524
|
+
):
|
|
1525
|
+
left = converters.to_unicode(self.left(progress, data, width))
|
|
1526
|
+
right = converters.to_unicode(self.right(progress, data, width))
|
|
1527
|
+
width -= progress.custom_len(left) + progress.custom_len(right)
|
|
1528
|
+
|
|
1529
|
+
status: str | bool | None = data['variables'].get(self.name)
|
|
1530
|
+
|
|
1531
|
+
if width and status is not None:
|
|
1532
|
+
if status is True:
|
|
1533
|
+
marker = self.success_marker
|
|
1534
|
+
fg_color = self.success_fg_color
|
|
1535
|
+
bg_color = self.success_bg_color
|
|
1536
|
+
elif status is False: # pragma: no branch
|
|
1537
|
+
marker = self.failure_marker
|
|
1538
|
+
fg_color = self.failure_fg_color
|
|
1539
|
+
bg_color = self.failure_bg_color
|
|
1540
|
+
else: # pragma: no cover
|
|
1541
|
+
marker = status
|
|
1542
|
+
fg_color = bg_color = None
|
|
1543
|
+
|
|
1544
|
+
marker = converters.to_unicode(marker)
|
|
1545
|
+
if fg_color: # pragma: no branch
|
|
1546
|
+
marker = fg_color.fg(marker)
|
|
1547
|
+
if bg_color: # pragma: no cover
|
|
1548
|
+
marker = bg_color.bg(marker)
|
|
1549
|
+
|
|
1550
|
+
self.job_markers.append(marker)
|
|
1551
|
+
marker = ''.join(self.job_markers)
|
|
1552
|
+
width -= progress.custom_len(marker)
|
|
1553
|
+
|
|
1554
|
+
fill = converters.to_unicode(self.fill(progress, data, width))
|
|
1555
|
+
fill = self._apply_colors(fill * width, data)
|
|
1556
|
+
|
|
1557
|
+
if self.fill_left: # pragma: no branch
|
|
1558
|
+
marker += fill
|
|
1559
|
+
else: # pragma: no cover
|
|
1560
|
+
marker = fill + marker
|
|
1561
|
+
else:
|
|
1562
|
+
marker = ''
|
|
1563
|
+
|
|
1564
|
+
return left + marker + right
|