diff options
author | Mike Frysinger <vapier@google.com> | 2022-04-19 02:30:09 -0400 |
---|---|---|
committer | Mike Frysinger <vapier@google.com> | 2022-04-19 23:50:48 +0000 |
commit | 4c11aebeb934817407266787d58247e1ca802057 (patch) | |
tree | 206022ca4322d0eaf9b89f9c3acc560e2cb5db02 /progress.py | |
parent | b90a422ab692657709ed49783212f7febe445ac7 (diff) | |
download | git-repo-4c11aebeb934817407266787d58247e1ca802057.tar.gz |
progress: optimize progress bar updates a bitv2.24
Rather than erase the entire line first then print out the new content,
print out the new content on top of the old and then erase anything we
didn't update. This should result in a lot less flashing with faster
terminals.
Bug: https://crbug.com/gerrit/11293
Change-Id: Ie2920b0bf3d5e6f920b8631a1c406444b23cd12d
Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/335214
Reviewed-by: LaMont Jones <lamontjones@google.com>
Tested-by: Mike Frysinger <vapier@google.com>
Diffstat (limited to 'progress.py')
-rw-r--r-- | progress.py | 27 |
1 files changed, 16 insertions, 11 deletions
diff --git a/progress.py b/progress.py index 43c7ad21..f9ac53a1 100644 --- a/progress.py +++ b/progress.py | |||
@@ -24,6 +24,11 @@ _NOT_TTY = not os.isatty(2) | |||
24 | # column 0. | 24 | # column 0. |
25 | CSI_ERASE_LINE = '\x1b[2K' | 25 | CSI_ERASE_LINE = '\x1b[2K' |
26 | 26 | ||
27 | # This will erase all content in the current line after the cursor. This is | ||
28 | # useful for partial updates & progress messages as the terminal can display | ||
29 | # it better. | ||
30 | CSI_ERASE_LINE_AFTER = '\x1b[K' | ||
31 | |||
27 | 32 | ||
28 | def duration_str(total): | 33 | def duration_str(total): |
29 | """A less noisy timedelta.__str__. | 34 | """A less noisy timedelta.__str__. |
@@ -85,10 +90,10 @@ class Progress(object): | |||
85 | return | 90 | return |
86 | 91 | ||
87 | if self._total <= 0: | 92 | if self._total <= 0: |
88 | sys.stderr.write('%s\r%s: %d,' % ( | 93 | sys.stderr.write('\r%s: %d,%s' % ( |
89 | CSI_ERASE_LINE, | ||
90 | self._title, | 94 | self._title, |
91 | self._done)) | 95 | self._done, |
96 | CSI_ERASE_LINE_AFTER)) | ||
92 | sys.stderr.flush() | 97 | sys.stderr.flush() |
93 | else: | 98 | else: |
94 | p = (100 * self._done) / self._total | 99 | p = (100 * self._done) / self._total |
@@ -96,14 +101,14 @@ class Progress(object): | |||
96 | jobs = '[%d job%s] ' % (self._active, 's' if self._active > 1 else '') | 101 | jobs = '[%d job%s] ' % (self._active, 's' if self._active > 1 else '') |
97 | else: | 102 | else: |
98 | jobs = '' | 103 | jobs = '' |
99 | sys.stderr.write('%s\r%s: %2d%% %s(%d%s/%d%s)%s%s%s' % ( | 104 | sys.stderr.write('\r%s: %2d%% %s(%d%s/%d%s)%s%s%s%s' % ( |
100 | CSI_ERASE_LINE, | ||
101 | self._title, | 105 | self._title, |
102 | p, | 106 | p, |
103 | jobs, | 107 | jobs, |
104 | self._done, self._units, | 108 | self._done, self._units, |
105 | self._total, self._units, | 109 | self._total, self._units, |
106 | ' ' if msg else '', msg, | 110 | ' ' if msg else '', msg, |
111 | CSI_ERASE_LINE_AFTER, | ||
107 | '\n' if self._print_newline else '')) | 112 | '\n' if self._print_newline else '')) |
108 | sys.stderr.flush() | 113 | sys.stderr.flush() |
109 | 114 | ||
@@ -113,19 +118,19 @@ class Progress(object): | |||
113 | 118 | ||
114 | duration = duration_str(time() - self._start) | 119 | duration = duration_str(time() - self._start) |
115 | if self._total <= 0: | 120 | if self._total <= 0: |
116 | sys.stderr.write('%s\r%s: %d, done in %s\n' % ( | 121 | sys.stderr.write('\r%s: %d, done in %s%s\n' % ( |
117 | CSI_ERASE_LINE, | ||
118 | self._title, | 122 | self._title, |
119 | self._done, | 123 | self._done, |
120 | duration)) | 124 | duration, |
125 | CSI_ERASE_LINE_AFTER)) | ||
121 | sys.stderr.flush() | 126 | sys.stderr.flush() |
122 | else: | 127 | else: |
123 | p = (100 * self._done) / self._total | 128 | p = (100 * self._done) / self._total |
124 | sys.stderr.write('%s\r%s: %3d%% (%d%s/%d%s), done in %s\n' % ( | 129 | sys.stderr.write('\r%s: %3d%% (%d%s/%d%s), done in %s%s\n' % ( |
125 | CSI_ERASE_LINE, | ||
126 | self._title, | 130 | self._title, |
127 | p, | 131 | p, |
128 | self._done, self._units, | 132 | self._done, self._units, |
129 | self._total, self._units, | 133 | self._total, self._units, |
130 | duration)) | 134 | duration, |
135 | CSI_ERASE_LINE_AFTER)) | ||
131 | sys.stderr.flush() | 136 | sys.stderr.flush() |