blob: 3764b9e2e7d6b5a32d4bb0760b7dc01a63ec087f [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):
Mike Frysinger4e05f652021-02-23 16:57:56 -050029 def __init__(self, title, total=0, units='', print_newline=False):
Shawn O. Pearce68194f42009-04-10 16:48:52 -070030 self._title = title
31 self._total = total
32 self._done = 0
Shawn O. Pearce2810cbc2009-04-18 10:09:16 -070033 self._start = time()
34 self._show = False
Shawn O. Pearce490d09a2011-09-19 08:56:47 -070035 self._units = units
Tim Schumacher913327f2017-06-05 15:01:41 +020036 self._print_newline = print_newline
Shawn O. Pearce68194f42009-04-10 16:48:52 -070037
Mike Frysinger3538dd22019-08-26 15:32:06 -040038 def update(self, inc=1, msg=''):
Shawn O. Pearce68194f42009-04-10 16:48:52 -070039 self._done += inc
Shawn O. Pearce68194f42009-04-10 16:48:52 -070040
Shawn O. Pearcef4f04d92010-05-27 16:48:36 -070041 if _NOT_TTY or IsTrace():
Shawn O. Pearce6ed4e282009-04-18 09:59:18 -070042 return
43
Shawn O. Pearce2810cbc2009-04-18 10:09:16 -070044 if not self._show:
45 if 0.5 <= time() - self._start:
46 self._show = True
47 else:
48 return
49
Shawn O. Pearceb1168ff2009-04-16 08:00:42 -070050 if self._total <= 0:
Mike Frysinger70d861f2019-08-26 15:22:36 -040051 sys.stderr.write('%s\r%s: %d,' % (
David Pursehouseabdf7502020-02-12 14:58:39 +090052 CSI_ERASE_LINE,
53 self._title,
54 self._done))
Shawn O. Pearceb1168ff2009-04-16 08:00:42 -070055 sys.stderr.flush()
56 else:
57 p = (100 * self._done) / self._total
Mike Frysinger4e05f652021-02-23 16:57:56 -050058 sys.stderr.write('%s\r%s: %3d%% (%d%s/%d%s)%s%s%s' % (
59 CSI_ERASE_LINE,
60 self._title,
61 p,
62 self._done, self._units,
63 self._total, self._units,
64 ' ' if msg else '', msg,
65 '\n' if self._print_newline else ''))
66 sys.stderr.flush()
Shawn O. Pearceb1168ff2009-04-16 08:00:42 -070067
68 def end(self):
Shawn O. Pearcef4f04d92010-05-27 16:48:36 -070069 if _NOT_TTY or IsTrace() or not self._show:
Shawn O. Pearce6ed4e282009-04-18 09:59:18 -070070 return
71
Shawn O. Pearceb1168ff2009-04-16 08:00:42 -070072 if self._total <= 0:
Mike Frysinger70d861f2019-08-26 15:22:36 -040073 sys.stderr.write('%s\r%s: %d, done.\n' % (
David Pursehouseabdf7502020-02-12 14:58:39 +090074 CSI_ERASE_LINE,
75 self._title,
76 self._done))
Shawn O. Pearceb1168ff2009-04-16 08:00:42 -070077 sys.stderr.flush()
78 else:
79 p = (100 * self._done) / self._total
Mike Frysinger70d861f2019-08-26 15:22:36 -040080 sys.stderr.write('%s\r%s: %3d%% (%d%s/%d%s), done.\n' % (
David Pursehouseabdf7502020-02-12 14:58:39 +090081 CSI_ERASE_LINE,
82 self._title,
83 p,
84 self._done, self._units,
85 self._total, self._units))
Shawn O. Pearce68194f42009-04-10 16:48:52 -070086 sys.stderr.flush()