25
25
widgets ,
26
26
widgets as widgets_module , # Avoid name collision
27
27
)
28
+ from .terminal import os_specific
28
29
29
30
logger = logging .getLogger (__name__ )
30
31
@@ -187,13 +188,13 @@ class DefaultFdMixin(ProgressBarMixinBase):
187
188
enable_colors : progressbar .env .ColorSupport = progressbar .env .COLOR_SUPPORT
188
189
189
190
def __init__ (
190
- self ,
191
- fd : base .TextIO = sys .stderr ,
192
- is_terminal : bool | None = None ,
193
- line_breaks : bool | None = None ,
194
- enable_colors : progressbar .env .ColorSupport | None = None ,
195
- line_offset : int = 0 ,
196
- ** kwargs ,
191
+ self ,
192
+ fd : base .TextIO = sys .stderr ,
193
+ is_terminal : bool | None = None ,
194
+ line_breaks : bool | None = None ,
195
+ enable_colors : progressbar .env .ColorSupport | None = None ,
196
+ line_offset : int = 0 ,
197
+ ** kwargs ,
197
198
):
198
199
if fd is sys .stdout :
199
200
fd = utils .streams .original_stdout
@@ -210,9 +211,9 @@ def __init__(
210
211
super ().__init__ (** kwargs )
211
212
212
213
def _apply_line_offset (
213
- self ,
214
- fd : base .TextIO ,
215
- line_offset : int ,
214
+ self ,
215
+ fd : base .TextIO ,
216
+ line_offset : int ,
216
217
) -> base .TextIO :
217
218
if line_offset :
218
219
return progressbar .terminal .stream .LineOffsetStreamWrapper (
@@ -232,8 +233,8 @@ def _determine_line_breaks(self, line_breaks: bool | None) -> bool | None:
232
233
return line_breaks
233
234
234
235
def _determine_enable_colors (
235
- self ,
236
- enable_colors : progressbar .env .ColorSupport | None ,
236
+ self ,
237
+ enable_colors : progressbar .env .ColorSupport | None ,
237
238
) -> progressbar .env .ColorSupport :
238
239
'''
239
240
Determines the color support for the progress bar.
@@ -294,6 +295,10 @@ def _determine_enable_colors(
294
295
def print (self , * args : types .Any , ** kwargs : types .Any ) -> None :
295
296
print (* args , file = self .fd , ** kwargs )
296
297
298
+ def start (self , ** kwargs ):
299
+ os_specific .set_console_mode ()
300
+ super ().start ()
301
+
297
302
def update (self , * args : types .Any , ** kwargs : types .Any ) -> None :
298
303
ProgressBarMixinBase .update (self , * args , ** kwargs )
299
304
@@ -309,10 +314,12 @@ def update(self, *args: types.Any, **kwargs: types.Any) -> None:
309
314
self .fd .write (types .cast (str , line .encode ('ascii' , 'replace' )))
310
315
311
316
def finish (
312
- self ,
313
- * args : types .Any ,
314
- ** kwargs : types .Any ,
317
+ self ,
318
+ * args : types .Any ,
319
+ ** kwargs : types .Any ,
315
320
) -> None : # pragma: no cover
321
+ os_specific .reset_console_mode ()
322
+
316
323
if self ._finished :
317
324
return
318
325
@@ -341,8 +348,8 @@ def _format_widgets(self):
341
348
342
349
for index , widget in enumerate (self .widgets ):
343
350
if isinstance (
344
- widget ,
345
- widgets .WidgetBase ,
351
+ widget ,
352
+ widgets .WidgetBase ,
346
353
) and not widget .check_size (self ):
347
354
continue
348
355
elif isinstance (widget , widgets .AutoWidthWidgetBase ):
@@ -420,10 +427,10 @@ class StdRedirectMixin(DefaultFdMixin):
420
427
_stderr : base .IO
421
428
422
429
def __init__ (
423
- self ,
424
- redirect_stderr : bool = False ,
425
- redirect_stdout : bool = False ,
426
- ** kwargs ,
430
+ self ,
431
+ redirect_stderr : bool = False ,
432
+ redirect_stdout : bool = False ,
433
+ ** kwargs ,
427
434
):
428
435
DefaultFdMixin .__init__ (self , ** kwargs )
429
436
self .redirect_stderr = redirect_stderr
@@ -551,23 +558,23 @@ class ProgressBar(
551
558
paused : bool = False
552
559
553
560
def __init__ (
554
- self ,
555
- min_value : NumberT = 0 ,
556
- max_value : NumberT | types .Type [base .UnknownLength ] | None = None ,
557
- widgets : types .Optional [
558
- types .Sequence [widgets_module .WidgetBase | str ]
559
- ] = None ,
560
- left_justify : bool = True ,
561
- initial_value : NumberT = 0 ,
562
- poll_interval : types .Optional [float ] = None ,
563
- widget_kwargs : types .Optional [types .Dict [str , types .Any ]] = None ,
564
- custom_len : types .Callable [[str ], int ] = utils .len_color ,
565
- max_error = True ,
566
- prefix = None ,
567
- suffix = None ,
568
- variables = None ,
569
- min_poll_interval = None ,
570
- ** kwargs ,
561
+ self ,
562
+ min_value : NumberT = 0 ,
563
+ max_value : NumberT | types .Type [base .UnknownLength ] | None = None ,
564
+ widgets : types .Optional [
565
+ types .Sequence [widgets_module .WidgetBase | str ]
566
+ ] = None ,
567
+ left_justify : bool = True ,
568
+ initial_value : NumberT = 0 ,
569
+ poll_interval : types .Optional [float ] = None ,
570
+ widget_kwargs : types .Optional [types .Dict [str , types .Any ]] = None ,
571
+ custom_len : types .Callable [[str ], int ] = utils .len_color ,
572
+ max_error = True ,
573
+ prefix = None ,
574
+ suffix = None ,
575
+ variables = None ,
576
+ min_poll_interval = None ,
577
+ ** kwargs ,
571
578
): # sourcery skip: low-code-quality
572
579
'''Initializes a progress bar with sane defaults.'''
573
580
StdRedirectMixin .__init__ (self , ** kwargs )
@@ -632,8 +639,8 @@ def __init__(
632
639
default = None ,
633
640
)
634
641
self ._MINIMUM_UPDATE_INTERVAL = (
635
- utils .deltas_to_seconds (self ._MINIMUM_UPDATE_INTERVAL )
636
- or self ._MINIMUM_UPDATE_INTERVAL
642
+ utils .deltas_to_seconds (self ._MINIMUM_UPDATE_INTERVAL )
643
+ or self ._MINIMUM_UPDATE_INTERVAL
637
644
)
638
645
639
646
# Note that the _MINIMUM_UPDATE_INTERVAL sets the minimum in case of
@@ -649,8 +656,8 @@ def __init__(
649
656
self .variables = utils .AttributeDict (variables or {})
650
657
for widget in self .widgets :
651
658
if (
652
- isinstance (widget , widgets_module .VariableMixin )
653
- and widget .name not in self .variables
659
+ isinstance (widget , widgets_module .VariableMixin )
660
+ and widget .name not in self .variables
654
661
):
655
662
self .variables [widget .name ] = None
656
663
@@ -771,7 +778,7 @@ def data(self) -> types.Dict[str, types.Any]:
771
778
total_seconds_elapsed = total_seconds_elapsed ,
772
779
# The seconds since the bar started modulo 60
773
780
seconds_elapsed = (elapsed .seconds % 60 )
774
- + (elapsed .microseconds / 1000000.0 ),
781
+ + (elapsed .microseconds / 1000000.0 ),
775
782
# The minutes since the bar started modulo 60
776
783
minutes_elapsed = (elapsed .seconds / 60 ) % 60 ,
777
784
# The hours since the bar started modulo 24
@@ -901,9 +908,9 @@ def update(self, value=None, force=False, **kwargs):
901
908
self .start ()
902
909
903
910
if (
904
- value is not None
905
- and value is not base .UnknownLength
906
- and isinstance (value , (int , float ))
911
+ value is not None
912
+ and value is not base .UnknownLength
913
+ and isinstance (value , (int , float ))
907
914
):
908
915
if self .max_value is base .UnknownLength :
909
916
# Can't compare against unknown lengths so just update
@@ -1026,9 +1033,9 @@ def _init_prefix(self):
1026
1033
1027
1034
def _verify_max_value (self ):
1028
1035
if (
1029
- self .max_value is not base .UnknownLength
1030
- and self .max_value is not None
1031
- and self .max_value < 0 # type: ignore
1036
+ self .max_value is not base .UnknownLength
1037
+ and self .max_value is not None
1038
+ and self .max_value < 0 # type: ignore
1032
1039
):
1033
1040
raise ValueError ('max_value out of range, got %r' % self .max_value )
1034
1041
0 commit comments