blob: 9222fcfc207ce6dff5f923b353c85edbf0d457f9 [file] [log] [blame]
Shawn O. Pearce68194f42009-04-10 16:48:52 -07001# Copyright (C) 2009 The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
Shawn O. Pearcef4f04d92010-05-27 16:48:36 -070015import os
Shawn O. Pearce68194f42009-04-10 16:48:52 -070016import sys
Shawn O. Pearce2810cbc2009-04-18 10:09:16 -070017from time import time
Mike Frysinger8a11f6f2019-08-27 00:26:15 -040018from repo_trace import IsTrace
Shawn O. Pearce68194f42009-04-10 16:48:52 -070019
Shawn O. Pearcef4f04d92010-05-27 16:48:36 -070020_NOT_TTY = not os.isatty(2)
21
Mike Frysinger70d861f2019-08-26 15:22:36 -040022# This will erase all content in the current line (wherever the cursor is).
23# It does not move the cursor, so this is usually followed by \r to move to
24# column 0.
25CSI_ERASE_LINE = '\x1b[2K'
26
David Pursehouse819827a2020-02-12 15:20:19 +090027
Shawn O. Pearce68194f42009-04-10 16:48:52 -070028class Progress(object):
Tim Schumacher7be072e2017-06-28 18:29:23 +020029 def __init__(self, title, total=0, units='', print_newline=False,
30 always_print_percentage=False):
Shawn O. Pearce68194f42009-04-10 16:48:52 -070031 self._title = title
32 self._total = total
33 self._done = 0
34 self._lastp = -1
Shawn O. Pearce2810cbc2009-04-18 10:09:16 -070035 self._start = time()
36 self._show = False
Shawn O. Pearce490d09a2011-09-19 08:56:47 -070037 self._units = units
Tim Schumacher913327f2017-06-05 15:01:41 +020038 self._print_newline = print_newline
Tim Schumacher7be072e2017-06-28 18:29:23 +020039 self._always_print_percentage = always_print_percentage
Shawn O. Pearce68194f42009-04-10 16:48:52 -070040
Mike Frysinger3538dd22019-08-26 15:32:06 -040041 def update(self, inc=1, msg=''):
Shawn O. Pearce68194f42009-04-10 16:48:52 -070042 self._done += inc
Shawn O. Pearce68194f42009-04-10 16:48:52 -070043
Shawn O. Pearcef4f04d92010-05-27 16:48:36 -070044 if _NOT_TTY or IsTrace():
Shawn O. Pearce6ed4e282009-04-18 09:59:18 -070045 return
46
Shawn O. Pearce2810cbc2009-04-18 10:09:16 -070047 if not self._show:
48 if 0.5 <= time() - self._start:
49 self._show = True
50 else:
51 return
52
Shawn O. Pearceb1168ff2009-04-16 08:00:42 -070053 if self._total <= 0:
Mike Frysinger70d861f2019-08-26 15:22:36 -040054 sys.stderr.write('%s\r%s: %d,' % (
David Pursehouseabdf7502020-02-12 14:58:39 +090055 CSI_ERASE_LINE,
56 self._title,
57 self._done))
Shawn O. Pearceb1168ff2009-04-16 08:00:42 -070058 sys.stderr.flush()
59 else:
60 p = (100 * self._done) / self._total
61
Tim Schumacher7be072e2017-06-28 18:29:23 +020062 if self._lastp != p or self._always_print_percentage:
Shawn O. Pearceb1168ff2009-04-16 08:00:42 -070063 self._lastp = p
Mike Frysinger3538dd22019-08-26 15:32:06 -040064 sys.stderr.write('%s\r%s: %3d%% (%d%s/%d%s)%s%s%s' % (
David Pursehouseabdf7502020-02-12 14:58:39 +090065 CSI_ERASE_LINE,
66 self._title,
67 p,
68 self._done, self._units,
69 self._total, self._units,
70 ' ' if msg else '', msg,
71 "\n" if self._print_newline else ""))
Shawn O. Pearceb1168ff2009-04-16 08:00:42 -070072 sys.stderr.flush()
73
74 def end(self):
Shawn O. Pearcef4f04d92010-05-27 16:48:36 -070075 if _NOT_TTY or IsTrace() or not self._show:
Shawn O. Pearce6ed4e282009-04-18 09:59:18 -070076 return
77
Shawn O. Pearceb1168ff2009-04-16 08:00:42 -070078 if self._total <= 0:
Mike Frysinger70d861f2019-08-26 15:22:36 -040079 sys.stderr.write('%s\r%s: %d, done.\n' % (
David Pursehouseabdf7502020-02-12 14:58:39 +090080 CSI_ERASE_LINE,
81 self._title,
82 self._done))
Shawn O. Pearceb1168ff2009-04-16 08:00:42 -070083 sys.stderr.flush()
84 else:
85 p = (100 * self._done) / self._total
Mike Frysinger70d861f2019-08-26 15:22:36 -040086 sys.stderr.write('%s\r%s: %3d%% (%d%s/%d%s), done.\n' % (
David Pursehouseabdf7502020-02-12 14:58:39 +090087 CSI_ERASE_LINE,
88 self._title,
89 p,
90 self._done, self._units,
91 self._total, self._units))
Shawn O. Pearce68194f42009-04-10 16:48:52 -070092 sys.stderr.flush()