Skip to content

Commit f8cf7ca

Browse files
ale-ddjonathanslenders
authored andcommitted
Fix performance issue with ProgressBar formatters
Signed-off-by: Alessandro Salvatori <[email protected]>
1 parent 465ab02 commit f8cf7ca

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

Diff for: src/prompt_toolkit/shortcuts/progress_bar/formatters.py

+15-12
Original file line numberDiff line numberDiff line change
@@ -130,15 +130,15 @@ class Percentage(Formatter):
130130
Display the progress as a percentage.
131131
"""
132132

133-
template = "<percentage>{percentage:>5}%</percentage>"
133+
template = HTML("<percentage>{percentage:>5}%</percentage>")
134134

135135
def format(
136136
self,
137137
progress_bar: ProgressBar,
138138
progress: ProgressBarCounter[object],
139139
width: int,
140140
) -> AnyFormattedText:
141-
return HTML(self.template).format(percentage=round(progress.percentage, 1))
141+
return self.template.format(percentage=round(progress.percentage, 1))
142142

143143
def get_width(self, progress_bar: ProgressBar) -> AnyDimension:
144144
return D.exact(6)
@@ -149,7 +149,7 @@ class Bar(Formatter):
149149
Display the progress bar itself.
150150
"""
151151

152-
template = "<bar>{start}<bar-a>{bar_a}</bar-a><bar-b>{bar_b}</bar-b><bar-c>{bar_c}</bar-c>{end}</bar>"
152+
template = HTML("<bar>{start}<bar-a>{bar_a}</bar-a><bar-b>{bar_b}</bar-b><bar-c>{bar_c}</bar-c>{end}</bar>")
153153

154154
def __init__(
155155
self,
@@ -202,7 +202,7 @@ def format(
202202
bar_b = sym_b
203203
bar_c = sym_c * (width - pb_a)
204204

205-
return HTML(self.template).format(
205+
return self.template.format(
206206
start=self.start, end=self.end, bar_a=bar_a, bar_b=bar_b, bar_c=bar_c
207207
)
208208

@@ -215,15 +215,15 @@ class Progress(Formatter):
215215
Display the progress as text. E.g. "8/20"
216216
"""
217217

218-
template = "<current>{current:>3}</current>/<total>{total:>3}</total>"
218+
template = HTML("<current>{current:>3}</current>/<total>{total:>3}</total>")
219219

220220
def format(
221221
self,
222222
progress_bar: ProgressBar,
223223
progress: ProgressBarCounter[object],
224224
width: int,
225225
) -> AnyFormattedText:
226-
return HTML(self.template).format(
226+
return self.template.format(
227227
current=progress.items_completed, total=progress.total or "?"
228228
)
229229

@@ -250,14 +250,16 @@ class TimeElapsed(Formatter):
250250
Display the elapsed time.
251251
"""
252252

253+
template = HTML("<time-elapsed>{time_elapsed}</time-elapsed>")
254+
253255
def format(
254256
self,
255257
progress_bar: ProgressBar,
256258
progress: ProgressBarCounter[object],
257259
width: int,
258260
) -> AnyFormattedText:
259261
text = _format_timedelta(progress.time_elapsed).rjust(width)
260-
return HTML("<time-elapsed>{time_elapsed}</time-elapsed>").format(
262+
return self.template.format(
261263
time_elapsed=text
262264
)
263265

@@ -275,7 +277,7 @@ class TimeLeft(Formatter):
275277
Display the time left.
276278
"""
277279

278-
template = "<time-left>{time_left}</time-left>"
280+
template = HTML("<time-left>{time_left}</time-left>")
279281
unknown = "?:??:??"
280282

281283
def format(
@@ -290,7 +292,7 @@ def format(
290292
else:
291293
formatted_time_left = self.unknown
292294

293-
return HTML(self.template).format(time_left=formatted_time_left.rjust(width))
295+
return self.template.format(time_left=formatted_time_left.rjust(width))
294296

295297
def get_width(self, progress_bar: ProgressBar) -> AnyDimension:
296298
all_values = [
@@ -307,7 +309,7 @@ class IterationsPerSecond(Formatter):
307309
Display the iterations per second.
308310
"""
309311

310-
template = (
312+
template = HTML(
311313
"<iterations-per-second>{iterations_per_second:.2f}</iterations-per-second>"
312314
)
313315

@@ -318,7 +320,7 @@ def format(
318320
width: int,
319321
) -> AnyFormattedText:
320322
value = progress.items_completed / progress.time_elapsed.total_seconds()
321-
return HTML(self.template.format(iterations_per_second=value))
323+
return self.template.format(iterations_per_second=value)
322324

323325
def get_width(self, progress_bar: ProgressBar) -> AnyDimension:
324326
all_values = [
@@ -335,6 +337,7 @@ class SpinningWheel(Formatter):
335337
Display a spinning wheel.
336338
"""
337339

340+
template = HTML("<spinning-wheel>{0}</spinning-wheel>")
338341
characters = r"/-\|"
339342

340343
def format(
@@ -344,7 +347,7 @@ def format(
344347
width: int,
345348
) -> AnyFormattedText:
346349
index = int(time.time() * 3) % len(self.characters)
347-
return HTML("<spinning-wheel>{0}</spinning-wheel>").format(
350+
return self.template.format(
348351
self.characters[index]
349352
)
350353

0 commit comments

Comments
 (0)