blob: d2ed4baeb081a1431afa2b19eb2cf115e859b050 [file] [log] [blame]
Mike Frysingerf6013762019-06-13 02:30:51 -04001# -*- coding:utf-8 -*-
Shawn O. Pearce68194f42009-04-10 16:48:52 -07002#
3# Copyright (C) 2009 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
Shawn O. Pearcef4f04d92010-05-27 16:48:36 -070017import os
Shawn O. Pearce68194f42009-04-10 16:48:52 -070018import sys
Shawn O. Pearce2810cbc2009-04-18 10:09:16 -070019from time import time
Mike Frysinger8a11f6f2019-08-27 00:26:15 -040020from repo_trace import IsTrace
Shawn O. Pearce68194f42009-04-10 16:48:52 -070021
Shawn O. Pearcef4f04d92010-05-27 16:48:36 -070022_NOT_TTY = not os.isatty(2)
23
Mike Frysinger70d861f2019-08-26 15:22:36 -040024# This will erase all content in the current line (wherever the cursor is).
25# It does not move the cursor, so this is usually followed by \r to move to
26# column 0.
27CSI_ERASE_LINE = '\x1b[2K'
28
Shawn O. Pearce68194f42009-04-10 16:48:52 -070029class Progress(object):
Tim Schumacher7be072e2017-06-28 18:29:23 +020030 def __init__(self, title, total=0, units='', print_newline=False,
31 always_print_percentage=False):
Shawn O. Pearce68194f42009-04-10 16:48:52 -070032 self._title = title
33 self._total = total
34 self._done = 0
35 self._lastp = -1
Shawn O. Pearce2810cbc2009-04-18 10:09:16 -070036 self._start = time()
37 self._show = False
Shawn O. Pearce490d09a2011-09-19 08:56:47 -070038 self._units = units
Tim Schumacher913327f2017-06-05 15:01:41 +020039 self._print_newline = print_newline
Tim Schumacher7be072e2017-06-28 18:29:23 +020040 self._always_print_percentage = always_print_percentage
Shawn O. Pearce68194f42009-04-10 16:48:52 -070041
Mike Frysinger3538dd22019-08-26 15:32:06 -040042 def update(self, inc=1, msg=''):
Shawn O. Pearce68194f42009-04-10 16:48:52 -070043 self._done += inc
Shawn O. Pearce68194f42009-04-10 16:48:52 -070044
Shawn O. Pearcef4f04d92010-05-27 16:48:36 -070045 if _NOT_TTY or IsTrace():
Shawn O. Pearce6ed4e282009-04-18 09:59:18 -070046 return
47
Shawn O. Pearce2810cbc2009-04-18 10:09:16 -070048 if not self._show:
49 if 0.5 <= time() - self._start:
50 self._show = True
51 else:
52 return
53
Shawn O. Pearceb1168ff2009-04-16 08:00:42 -070054 if self._total <= 0:
Mike Frysinger70d861f2019-08-26 15:22:36 -040055 sys.stderr.write('%s\r%s: %d,' % (
56 CSI_ERASE_LINE,
Shawn O. Pearceb1168ff2009-04-16 08:00:42 -070057 self._title,
58 self._done))
59 sys.stderr.flush()
60 else:
61 p = (100 * self._done) / self._total
62
Tim Schumacher7be072e2017-06-28 18:29:23 +020063 if self._lastp != p or self._always_print_percentage:
Shawn O. Pearceb1168ff2009-04-16 08:00:42 -070064 self._lastp = p
Mike Frysinger3538dd22019-08-26 15:32:06 -040065 sys.stderr.write('%s\r%s: %3d%% (%d%s/%d%s)%s%s%s' % (
Mike Frysinger70d861f2019-08-26 15:22:36 -040066 CSI_ERASE_LINE,
Shawn O. Pearceb1168ff2009-04-16 08:00:42 -070067 self._title,
68 p,
Shawn O. Pearce490d09a2011-09-19 08:56:47 -070069 self._done, self._units,
Tim Schumacher913327f2017-06-05 15:01:41 +020070 self._total, self._units,
Mike Frysinger3538dd22019-08-26 15:32:06 -040071 ' ' if msg else '', msg,
Tim Schumacher913327f2017-06-05 15:01:41 +020072 "\n" if self._print_newline else ""))
Shawn O. Pearceb1168ff2009-04-16 08:00:42 -070073 sys.stderr.flush()
74
75 def end(self):
Shawn O. Pearcef4f04d92010-05-27 16:48:36 -070076 if _NOT_TTY or IsTrace() or not self._show:
Shawn O. Pearce6ed4e282009-04-18 09:59:18 -070077 return
78
Shawn O. Pearceb1168ff2009-04-16 08:00:42 -070079 if self._total <= 0:
Mike Frysinger70d861f2019-08-26 15:22:36 -040080 sys.stderr.write('%s\r%s: %d, done.\n' % (
81 CSI_ERASE_LINE,
Shawn O. Pearceb1168ff2009-04-16 08:00:42 -070082 self._title,
83 self._done))
84 sys.stderr.flush()
85 else:
86 p = (100 * self._done) / self._total
Mike Frysinger70d861f2019-08-26 15:22:36 -040087 sys.stderr.write('%s\r%s: %3d%% (%d%s/%d%s), done.\n' % (
88 CSI_ERASE_LINE,
Shawn O. Pearce68194f42009-04-10 16:48:52 -070089 self._title,
90 p,
Shawn O. Pearce490d09a2011-09-19 08:56:47 -070091 self._done, self._units,
92 self._total, self._units))
Shawn O. Pearce68194f42009-04-10 16:48:52 -070093 sys.stderr.flush()