Skip to content

Commit

Permalink
Fix performance issue with ProgressBar formatters
Browse files Browse the repository at this point in the history
Signed-off-by: Alessandro Salvatori <[email protected]>
  • Loading branch information
ale-dd authored and jonathanslenders committed May 15, 2024
1 parent 465ab02 commit f8cf7ca
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions src/prompt_toolkit/shortcuts/progress_bar/formatters.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,15 +130,15 @@ class Percentage(Formatter):
Display the progress as a percentage.
"""

template = "<percentage>{percentage:>5}%</percentage>"
template = HTML("<percentage>{percentage:>5}%</percentage>")

def format(
self,
progress_bar: ProgressBar,
progress: ProgressBarCounter[object],
width: int,
) -> AnyFormattedText:
return HTML(self.template).format(percentage=round(progress.percentage, 1))
return self.template.format(percentage=round(progress.percentage, 1))

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

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

def __init__(
self,
Expand Down Expand Up @@ -202,7 +202,7 @@ def format(
bar_b = sym_b
bar_c = sym_c * (width - pb_a)

return HTML(self.template).format(
return self.template.format(
start=self.start, end=self.end, bar_a=bar_a, bar_b=bar_b, bar_c=bar_c
)

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

template = "<current>{current:>3}</current>/<total>{total:>3}</total>"
template = HTML("<current>{current:>3}</current>/<total>{total:>3}</total>")

def format(
self,
progress_bar: ProgressBar,
progress: ProgressBarCounter[object],
width: int,
) -> AnyFormattedText:
return HTML(self.template).format(
return self.template.format(
current=progress.items_completed, total=progress.total or "?"
)

Expand All @@ -250,14 +250,16 @@ class TimeElapsed(Formatter):
Display the elapsed time.
"""

template = HTML("<time-elapsed>{time_elapsed}</time-elapsed>")

def format(
self,
progress_bar: ProgressBar,
progress: ProgressBarCounter[object],
width: int,
) -> AnyFormattedText:
text = _format_timedelta(progress.time_elapsed).rjust(width)
return HTML("<time-elapsed>{time_elapsed}</time-elapsed>").format(
return self.template.format(
time_elapsed=text
)

Expand All @@ -275,7 +277,7 @@ class TimeLeft(Formatter):
Display the time left.
"""

template = "<time-left>{time_left}</time-left>"
template = HTML("<time-left>{time_left}</time-left>")
unknown = "?:??:??"

def format(
Expand All @@ -290,7 +292,7 @@ def format(
else:
formatted_time_left = self.unknown

return HTML(self.template).format(time_left=formatted_time_left.rjust(width))
return self.template.format(time_left=formatted_time_left.rjust(width))

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

template = (
template = HTML(
"<iterations-per-second>{iterations_per_second:.2f}</iterations-per-second>"
)

Expand All @@ -318,7 +320,7 @@ def format(
width: int,
) -> AnyFormattedText:
value = progress.items_completed / progress.time_elapsed.total_seconds()
return HTML(self.template.format(iterations_per_second=value))
return self.template.format(iterations_per_second=value)

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

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

def format(
Expand All @@ -344,7 +347,7 @@ def format(
width: int,
) -> AnyFormattedText:
index = int(time.time() * 3) % len(self.characters)
return HTML("<spinning-wheel>{0}</spinning-wheel>").format(
return self.template.format(
self.characters[index]
)

Expand Down

0 comments on commit f8cf7ca

Please sign in to comment.