Skip to content

Commit d5a2f99

Browse files
authored
fix progress bar to print in one line
1 parent 30b80d2 commit d5a2f99

File tree

1 file changed

+8
-10
lines changed

1 file changed

+8
-10
lines changed

esptool/logger.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ class EsptoolLogger(TemplateLogger):
7373
ansi_clear: str = ""
7474
ansi_line_up: str = ""
7575
ansi_line_clear: str = ""
76+
ansi_line_up_pos1: str = ""
7677

7778
_stage_active: bool = False
7879
_newline_count: int = 0
@@ -115,7 +116,7 @@ def _set_smart_features(cls, override: bool | None = None):
115116

116117
# Determine if colors should be enabled
117118
cls.instance._smart_features = (
118-
is_tty and term_supports_color and not no_color
119+
is_tty or term_supports_color and not no_color
119120
)
120121
# Handle Windows specifically
121122
if sys.platform == "win32" and cls.instance._smart_features:
@@ -134,6 +135,7 @@ def _set_smart_features(cls, override: bool | None = None):
134135
cls.instance.ansi_clear = "\033[K"
135136
cls.instance.ansi_line_up = "\033[1A"
136137
cls.instance.ansi_line_clear = "\x1b[2K"
138+
cls.instance.ansi_line_up_pos1 = "\033[F"
137139
else:
138140
cls.instance.ansi_red = ""
139141
cls.instance.ansi_yellow = ""
@@ -142,6 +144,7 @@ def _set_smart_features(cls, override: bool | None = None):
142144
cls.instance.ansi_clear = ""
143145
cls.instance.ansi_line_up = ""
144146
cls.instance.ansi_line_clear = ""
147+
cls.instance.ansi_line_up_pos1 = ""
145148

146149
def print(self, *args, **kwargs):
147150
"""
@@ -228,18 +231,13 @@ def progress_bar(
228231
Call in a loop to print a progress bar overwriting itself in place.
229232
If terminal doesn't support ANSI escape codes, no overwriting happens.
230233
"""
231-
filled = int(bar_length * cur_iter // total_iters)
232-
if filled == bar_length:
233-
bar = "=" * bar_length
234-
elif filled == 0:
235-
bar = " " * bar_length
236-
else:
237-
bar = f"{'=' * (filled - 1)}>{' ' * (bar_length - filled)}"
238-
239234
percent = f"{100 * (cur_iter / float(total_iters)):.1f}"
235+
filled_length = int(bar_length * cur_iter // total_iters)
236+
bar = '█' * filled_length + '░' * (bar_length - filled_length)
237+
240238
self.print(
241239
f"\r{self.ansi_clear}{prefix}[{bar}] {percent:>5}%{suffix} ",
242-
end="\n" if not self._smart_features or cur_iter == total_iters else "",
240+
end="\n" if not self._smart_features or cur_iter == total_iters else f"{self.ansi_line_up_pos1}",
243241
flush=True,
244242
)
245243

0 commit comments

Comments
 (0)