Optimize progressbar renderin. This fixes #418
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Armin Ronacher · committed by Armin Ronacher · parent da080dd · Browse files · 2 files·+40 added−26 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+40 added−26 removed
- other:+40 added−26 removed
Provenance
- Commit
c4c9eb2- Tree
1858112- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
da080dd
2 files changed+40 added−26 removed
modified:click/_termui_impl.py+32 added-26 removed
85 unmodified lines86 self.max_width = None87 self.entered = False88 self.current_item = None89 self.is_hidden = not isatty(self.file)90+ self._last_line = None91 92 def __enter__(self):93 self.entered = True94 self.render_progress()86 unmodified lines181 def render_progress(self):182 from .termui import get_terminal_size183 184 if self.is_hidden:184- echo(self.label, file=self.file, color=self.color)185- self.file.flush()186- return185+ buf = [self.label]186+ else:187+ buf = []188+ # Update width in case the terminal has been resized189+ if self.autowidth:190+ old_width = self.width191+ self.width = 0192+ clutter_length = term_len(self.format_progress_line())193+ new_width = max(0, get_terminal_size()[0] - clutter_length)194+ if new_width < old_width:195+ buf.append(BEFORE_BAR)196+ buf.append(' ' * self.max_width)197+ self.max_width = new_width198+ self.width = new_width199 188- # Update width in case the terminal has been resized189- if self.autowidth:190- old_width = self.width191- self.width = 0192- clutter_length = term_len(self.format_progress_line())193- new_width = max(0, get_terminal_size()[0] - clutter_length)194- if new_width < old_width:195- self.file.write(BEFORE_BAR)196- self.file.write(' ' * self.max_width)197- self.max_width = new_width198- self.width = new_width200+ clear_width = self.width201+ if self.max_width is not None:202+ clear_width = self.max_width203 200- clear_width = self.width201- if self.max_width is not None:202- clear_width = self.max_width204+ buf.append(BEFORE_BAR)205+ line = self.format_progress_line()206+ line_len = term_len(line)207+ if self.max_width is None or self.max_width < line_len:208+ self.max_width = line_len209+ buf.append(line)210 204- self.file.write(BEFORE_BAR)205- line = self.format_progress_line()206- line_len = term_len(line)207- if self.max_width is None or self.max_width < line_len:208- self.max_width = line_len209- # Use echo here so that we get colorama support.210- echo(line, file=self.file, nl=False, color=self.color)211- self.file.write(' ' * (clear_width - line_len))212- self.file.flush()211+ buf.append(' ' * (clear_width - line_len))212+ line = ''.join(buf)213 214+ # Render the line only if it changed.215+ if line != self._last_line:216+ self._last_line = line217+ echo(line, file=self.file, color=self.color, nl='')218+ self.file.flush()219+220 def make_step(self, n_steps):221 self.pos += n_steps222 if self.length_known and self.pos >= self.length:223 self.finished = True311 unmodified linesmodified:CHANGES+8 added
1 unmodified line2 ===============3 4 This contains all major version changes between Click releases.5 6+Version 6.07+-----------8+9+(codename and release date to be decided)10+11+- Optimized the progressbar rendering to not render when it did not12+ actually change.13+14 Version 5.115 -----------16 17 (bugfix release, released on 17th August 2015)250 unmodified lines