OLD | NEW |
1 # | 1 # |
2 # Copyright (C) 2009 The Android Open Source Project | 2 # Copyright (C) 2009 The Android Open Source Project |
3 # | 3 # |
4 # Licensed under the Apache License, Version 2.0 (the "License"); | 4 # Licensed under the Apache License, Version 2.0 (the "License"); |
5 # you may not use this file except in compliance with the License. | 5 # you may not use this file except in compliance with the License. |
6 # You may obtain a copy of the License at | 6 # You may obtain a copy of the License at |
7 # | 7 # |
8 # http://www.apache.org/licenses/LICENSE-2.0 | 8 # http://www.apache.org/licenses/LICENSE-2.0 |
9 # | 9 # |
10 # Unless required by applicable law or agreed to in writing, software | 10 # Unless required by applicable law or agreed to in writing, software |
11 # distributed under the License is distributed on an "AS IS" BASIS, | 11 # distributed under the License is distributed on an "AS IS" BASIS, |
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 # See the License for the specific language governing permissions and | 13 # See the License for the specific language governing permissions and |
14 # limitations under the License. | 14 # limitations under the License. |
15 | 15 |
16 import sys | 16 import sys |
17 from time import time | 17 from time import time |
18 | 18 |
19 class Progress(object): | 19 class Progress(object): |
20 def __init__(self, title, total=0): | 20 def __init__(self, title, total=0): |
21 self._title = title | 21 self._title = title |
22 self._total = total | 22 self._total = total |
23 self._done = 0 | 23 self._done = 0 |
24 self._lastp = -1 | 24 self._lastp = -1 |
25 self._start = time() | 25 self._start = time() |
26 self._show = False | 26 self._show = False |
| 27 self._width = 0 |
27 | 28 |
28 def update(self, inc=1): | 29 def update(self, inc=1, extra=''): |
29 self._done += inc | 30 self._done += inc |
30 | 31 |
31 if not self._show: | 32 if not self._show: |
32 if 0.5 <= time() - self._start: | 33 if 0.5 <= time() - self._start: |
33 self._show = True | 34 self._show = True |
34 else: | 35 else: |
35 return | 36 return |
36 | 37 |
| 38 text = None |
| 39 |
37 if self._total <= 0: | 40 if self._total <= 0: |
38 sys.stdout.write('\r%s: %d, ' % ( | 41 text = '%s: %3d' % (self._title, self._done) |
39 self._title, | |
40 self._done)) | |
41 sys.stdout.flush() | |
42 else: | 42 else: |
43 p = (100 * self._done) / self._total | 43 p = (100 * self._done) / self._total |
44 | 44 |
45 if self._lastp != p: | 45 if self._lastp != p: |
46 self._lastp = p | 46 self._lastp = p |
47 sys.stdout.write('\r%s: %3d%% (%d/%d) ' % ( | 47 text = '%s: %3d%% (%2d/%2d)' % (self._title, p, |
48 self._title, | 48 self._done, self._total) |
49 p, | 49 |
50 self._done, | 50 if text: |
51 self._total)) | 51 text += ' ' + extra |
52 sys.stdout.flush() | 52 spaces = max(self._width - len(text), 0) |
| 53 sys.stdout.write('%s%*s\r' % (text, spaces, '')) |
| 54 sys.stdout.flush() |
| 55 self._width = len(text) |
53 | 56 |
54 def end(self): | 57 def end(self): |
55 if not self._show: | 58 if not self._show: |
56 return | 59 return |
57 | 60 |
58 if self._total <= 0: | 61 if self._total <= 0: |
59 sys.stdout.write('\r%s: %d, done. \n' % ( | 62 sys.stdout.write('%s: %d, done.\n' % ( |
60 self._title, | 63 self._title, |
61 self._done)) | 64 self._done)) |
62 sys.stdout.flush() | 65 sys.stdout.flush() |
63 else: | 66 else: |
64 p = (100 * self._done) / self._total | 67 p = (100 * self._done) / self._total |
65 sys.stdout.write('\r%s: %3d%% (%d/%d), done. \n' % ( | 68 sys.stdout.write('%s: %3d%% (%d/%d), done.\n' % ( |
66 self._title, | 69 self._title, |
67 p, | 70 p, |
68 self._done, | 71 self._done, |
69 self._total)) | 72 self._total)) |
70 sys.stdout.flush() | 73 sys.stdout.flush() |
OLD | NEW |