mercurial/progress.py
author Jun Wu <quark@fb.com>
Fri, 29 Sep 2017 11:41:24 -0700
changeset 34316 12b355964de8
parent 34314 a667f0ca1d5f
child 34486 a57c938e7ac8
permissions -rw-r--r--
test-patchbomb: use mocktime The test was using system time for displaying ETAs, which could be flaky if the sysload is high. This patch extracts mocktime.py from test-progress.t to make sure test-patchbomb.t is unaffected by system time. Differential Revision: https://phab.mercurial-scm.org/D844
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
25497
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
     1
# progress.py progress bars related code
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
     2
#
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
     3
# Copyright (C) 2010 Augie Fackler <durin42@gmail.com>
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
     4
#
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
     5
# This software may be used and distributed according to the terms of the
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
     6
# GNU General Public License version 2 or any later version.
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
     7
25968
1139d7cf9405 progress: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25847
diff changeset
     8
from __future__ import absolute_import
1139d7cf9405 progress: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25847
diff changeset
     9
32049
ed42e00a5c4e progress: retry ferr.flush() and .write() on EINTR (issue5532)
Yuya Nishihara <yuya@tcha.org>
parents: 32048
diff changeset
    10
import errno
25497
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    11
import threading
25968
1139d7cf9405 progress: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25847
diff changeset
    12
import time
25497
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    13
25968
1139d7cf9405 progress: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25847
diff changeset
    14
from .i18n import _
1139d7cf9405 progress: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25847
diff changeset
    15
from . import encoding
25497
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    16
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    17
def spacejoin(*args):
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    18
    return ' '.join(s for s in args if s)
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    19
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    20
def shouldprint(ui):
28171
2d20d1d2ea76 progress: display progress bar when HGPLAINEXCEPT contains "progress"
Matt Anderson <andersonmat@fb.com>
parents: 26781
diff changeset
    21
    return not (ui.quiet or ui.plain('progress')) and (
30324
cb1ea3cc44b5 progress: obtain stderr from ui
Yuya Nishihara <yuya@tcha.org>
parents: 29089
diff changeset
    22
        ui._isatty(ui.ferr) or ui.configbool('progress', 'assume-tty'))
25497
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    23
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    24
def fmtremaining(seconds):
26781
1aee2ab0f902 spelling: trivial spell checking
Mads Kiilerich <madski@unity3d.com>
parents: 26407
diff changeset
    25
    """format a number of remaining seconds in human readable way
25497
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    26
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    27
    This will properly display seconds, minutes, hours, days if needed"""
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    28
    if seconds < 60:
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    29
        # i18n: format XX seconds as "XXs"
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    30
        return _("%02ds") % (seconds)
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    31
    minutes = seconds // 60
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    32
    if minutes < 60:
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    33
        seconds -= minutes * 60
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    34
        # i18n: format X minutes and YY seconds as "XmYYs"
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    35
        return _("%dm%02ds") % (minutes, seconds)
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    36
    # we're going to ignore seconds in this case
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    37
    minutes += 1
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    38
    hours = minutes // 60
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    39
    minutes -= hours * 60
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    40
    if hours < 30:
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    41
        # i18n: format X hours and YY minutes as "XhYYm"
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    42
        return _("%dh%02dm") % (hours, minutes)
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    43
    # we're going to ignore minutes in this case
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    44
    hours += 1
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    45
    days = hours // 24
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    46
    hours -= days * 24
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    47
    if days < 15:
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    48
        # i18n: format X days and YY hours as "XdYYh"
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    49
        return _("%dd%02dh") % (days, hours)
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    50
    # we're going to ignore hours in this case
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    51
    days += 1
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    52
    weeks = days // 7
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    53
    days -= weeks * 7
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    54
    if weeks < 55:
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    55
        # i18n: format X weeks and YY days as "XwYYd"
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    56
        return _("%dw%02dd") % (weeks, days)
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    57
    # we're going to ignore days and treat a year as 52 weeks
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    58
    weeks += 1
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    59
    years = weeks // 52
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    60
    weeks -= years * 52
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    61
    # i18n: format X years and YY weeks as "XyYYw"
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    62
    return _("%dy%02dw") % (years, weeks)
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    63
32049
ed42e00a5c4e progress: retry ferr.flush() and .write() on EINTR (issue5532)
Yuya Nishihara <yuya@tcha.org>
parents: 32048
diff changeset
    64
# file_write() and file_flush() of Python 2 do not restart on EINTR if
ed42e00a5c4e progress: retry ferr.flush() and .write() on EINTR (issue5532)
Yuya Nishihara <yuya@tcha.org>
parents: 32048
diff changeset
    65
# the file is attached to a "slow" device (e.g. a terminal) and raise
ed42e00a5c4e progress: retry ferr.flush() and .write() on EINTR (issue5532)
Yuya Nishihara <yuya@tcha.org>
parents: 32048
diff changeset
    66
# IOError. We cannot know how many bytes would be written by file_write(),
ed42e00a5c4e progress: retry ferr.flush() and .write() on EINTR (issue5532)
Yuya Nishihara <yuya@tcha.org>
parents: 32048
diff changeset
    67
# but a progress text is known to be short enough to be written by a
ed42e00a5c4e progress: retry ferr.flush() and .write() on EINTR (issue5532)
Yuya Nishihara <yuya@tcha.org>
parents: 32048
diff changeset
    68
# single write() syscall, so we can just retry file_write() with the whole
ed42e00a5c4e progress: retry ferr.flush() and .write() on EINTR (issue5532)
Yuya Nishihara <yuya@tcha.org>
parents: 32048
diff changeset
    69
# text. (issue5532)
ed42e00a5c4e progress: retry ferr.flush() and .write() on EINTR (issue5532)
Yuya Nishihara <yuya@tcha.org>
parents: 32048
diff changeset
    70
#
ed42e00a5c4e progress: retry ferr.flush() and .write() on EINTR (issue5532)
Yuya Nishihara <yuya@tcha.org>
parents: 32048
diff changeset
    71
# This should be a short-term workaround. We'll need to fix every occurrence
ed42e00a5c4e progress: retry ferr.flush() and .write() on EINTR (issue5532)
Yuya Nishihara <yuya@tcha.org>
parents: 32048
diff changeset
    72
# of write() to a terminal or pipe.
ed42e00a5c4e progress: retry ferr.flush() and .write() on EINTR (issue5532)
Yuya Nishihara <yuya@tcha.org>
parents: 32048
diff changeset
    73
def _eintrretry(func, *args):
ed42e00a5c4e progress: retry ferr.flush() and .write() on EINTR (issue5532)
Yuya Nishihara <yuya@tcha.org>
parents: 32048
diff changeset
    74
    while True:
ed42e00a5c4e progress: retry ferr.flush() and .write() on EINTR (issue5532)
Yuya Nishihara <yuya@tcha.org>
parents: 32048
diff changeset
    75
        try:
ed42e00a5c4e progress: retry ferr.flush() and .write() on EINTR (issue5532)
Yuya Nishihara <yuya@tcha.org>
parents: 32048
diff changeset
    76
            return func(*args)
ed42e00a5c4e progress: retry ferr.flush() and .write() on EINTR (issue5532)
Yuya Nishihara <yuya@tcha.org>
parents: 32048
diff changeset
    77
        except IOError as err:
ed42e00a5c4e progress: retry ferr.flush() and .write() on EINTR (issue5532)
Yuya Nishihara <yuya@tcha.org>
parents: 32048
diff changeset
    78
            if err.errno == errno.EINTR:
ed42e00a5c4e progress: retry ferr.flush() and .write() on EINTR (issue5532)
Yuya Nishihara <yuya@tcha.org>
parents: 32048
diff changeset
    79
                continue
ed42e00a5c4e progress: retry ferr.flush() and .write() on EINTR (issue5532)
Yuya Nishihara <yuya@tcha.org>
parents: 32048
diff changeset
    80
            raise
ed42e00a5c4e progress: retry ferr.flush() and .write() on EINTR (issue5532)
Yuya Nishihara <yuya@tcha.org>
parents: 32048
diff changeset
    81
25497
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    82
class progbar(object):
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    83
    def __init__(self, ui):
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    84
        self.ui = ui
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    85
        self._refreshlock = threading.Lock()
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    86
        self.resetstate()
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    87
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    88
    def resetstate(self):
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    89
        self.topics = []
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    90
        self.topicstates = {}
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    91
        self.starttimes = {}
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    92
        self.startvals = {}
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    93
        self.printed = False
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    94
        self.lastprint = time.time() + float(self.ui.config(
33499
0407a51b9d8c codemod: register core configitems using a script
Jun Wu <quark@fb.com>
parents: 33250
diff changeset
    95
            'progress', 'delay'))
25497
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    96
        self.curtopic = None
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    97
        self.lasttopic = None
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    98
        self.indetcount = 0
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    99
        self.refresh = float(self.ui.config(
33499
0407a51b9d8c codemod: register core configitems using a script
Jun Wu <quark@fb.com>
parents: 33250
diff changeset
   100
            'progress', 'refresh'))
25497
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   101
        self.changedelay = max(3 * self.refresh,
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   102
                               float(self.ui.config(
33499
0407a51b9d8c codemod: register core configitems using a script
Jun Wu <quark@fb.com>
parents: 33250
diff changeset
   103
                                   'progress', 'changedelay')))
25497
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   104
        self.order = self.ui.configlist(
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   105
            'progress', 'format',
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   106
            default=['topic', 'bar', 'number', 'estimate'])
34314
a667f0ca1d5f progress: make ETA only consider progress made in the last minute
Jun Wu <quark@fb.com>
parents: 34313
diff changeset
   107
        self.estimateinterval = self.ui.configwith(
a667f0ca1d5f progress: make ETA only consider progress made in the last minute
Jun Wu <quark@fb.com>
parents: 34313
diff changeset
   108
            float, 'progress', 'estimateinterval')
25497
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   109
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   110
    def show(self, now, topic, pos, item, unit, total):
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   111
        if not shouldprint(self.ui):
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   112
            return
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   113
        termwidth = self.width()
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   114
        self.printed = True
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   115
        head = ''
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   116
        needprogress = False
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   117
        tail = ''
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   118
        for indicator in self.order:
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   119
            add = ''
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   120
            if indicator == 'topic':
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   121
                add = topic
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   122
            elif indicator == 'number':
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   123
                if total:
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   124
                    add = ('% ' + str(len(str(total))) +
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   125
                           's/%s') % (pos, total)
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   126
                else:
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   127
                    add = str(pos)
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   128
            elif indicator.startswith('item') and item:
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   129
                slice = 'end'
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   130
                if '-' in indicator:
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   131
                    wid = int(indicator.split('-')[1])
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   132
                elif '+' in indicator:
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   133
                    slice = 'beginning'
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   134
                    wid = int(indicator.split('+')[1])
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   135
                else:
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   136
                    wid = 20
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   137
                if slice == 'end':
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   138
                    add = encoding.trim(item, wid, leftside=True)
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   139
                else:
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   140
                    add = encoding.trim(item, wid)
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   141
                add += (wid - encoding.colwidth(add)) * ' '
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   142
            elif indicator == 'bar':
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   143
                add = ''
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   144
                needprogress = True
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   145
            elif indicator == 'unit' and unit:
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   146
                add = unit
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   147
            elif indicator == 'estimate':
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   148
                add = self.estimate(topic, pos, total, now)
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   149
            elif indicator == 'speed':
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   150
                add = self.speed(topic, pos, unit, now)
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   151
            if not needprogress:
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   152
                head = spacejoin(head, add)
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   153
            else:
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   154
                tail = spacejoin(tail, add)
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   155
        if needprogress:
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   156
            used = 0
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   157
            if head:
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   158
                used += encoding.colwidth(head) + 1
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   159
            if tail:
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   160
                used += encoding.colwidth(tail) + 1
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   161
            progwidth = termwidth - used - 3
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   162
            if total and pos <= total:
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   163
                amt = pos * progwidth // total
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   164
                bar = '=' * (amt - 1)
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   165
                if amt > 0:
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   166
                    bar += '>'
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   167
                bar += ' ' * (progwidth - amt)
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   168
            else:
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   169
                progwidth -= 3
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   170
                self.indetcount += 1
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   171
                # mod the count by twice the width so we can make the
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   172
                # cursor bounce between the right and left sides
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   173
                amt = self.indetcount % (2 * progwidth)
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   174
                amt -= progwidth
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   175
                bar = (' ' * int(progwidth - abs(amt)) + '<=>' +
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   176
                       ' ' * int(abs(amt)))
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   177
            prog = ''.join(('[', bar , ']'))
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   178
            out = spacejoin(head, prog, tail)
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   179
        else:
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   180
            out = spacejoin(head, tail)
32048
c3ef33fd0058 progress: extract stubs to restart ferr.flush() and .write() on EINTR
Yuya Nishihara <yuya@tcha.org>
parents: 30324
diff changeset
   181
        self._writeerr('\r' + encoding.trim(out, termwidth))
25497
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   182
        self.lasttopic = topic
32048
c3ef33fd0058 progress: extract stubs to restart ferr.flush() and .write() on EINTR
Yuya Nishihara <yuya@tcha.org>
parents: 30324
diff changeset
   183
        self._flusherr()
25497
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   184
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   185
    def clear(self):
29089
222b8170d69e progress: stop excessive clearing (issue4801)
Matt Mackall <mpm@selenic.com>
parents: 28171
diff changeset
   186
        if not self.printed or not self.lastprint or not shouldprint(self.ui):
25497
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   187
            return
32048
c3ef33fd0058 progress: extract stubs to restart ferr.flush() and .write() on EINTR
Yuya Nishihara <yuya@tcha.org>
parents: 30324
diff changeset
   188
        self._writeerr('\r%s\r' % (' ' * self.width()))
26407
72bccc1f26b1 progress: force a repaint of a printed progress bar after a clear()
Augie Fackler <augie@google.com>
parents: 25968
diff changeset
   189
        if self.printed:
72bccc1f26b1 progress: force a repaint of a printed progress bar after a clear()
Augie Fackler <augie@google.com>
parents: 25968
diff changeset
   190
            # force immediate re-paint of progress bar
72bccc1f26b1 progress: force a repaint of a printed progress bar after a clear()
Augie Fackler <augie@google.com>
parents: 25968
diff changeset
   191
            self.lastprint = 0
25497
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   192
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   193
    def complete(self):
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   194
        if not shouldprint(self.ui):
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   195
            return
33249
391da1416038 configitems: register the 'progress.clear-complete' config
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 32049
diff changeset
   196
        if self.ui.configbool('progress', 'clear-complete'):
25497
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   197
            self.clear()
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   198
        else:
32048
c3ef33fd0058 progress: extract stubs to restart ferr.flush() and .write() on EINTR
Yuya Nishihara <yuya@tcha.org>
parents: 30324
diff changeset
   199
            self._writeerr('\n')
c3ef33fd0058 progress: extract stubs to restart ferr.flush() and .write() on EINTR
Yuya Nishihara <yuya@tcha.org>
parents: 30324
diff changeset
   200
        self._flusherr()
c3ef33fd0058 progress: extract stubs to restart ferr.flush() and .write() on EINTR
Yuya Nishihara <yuya@tcha.org>
parents: 30324
diff changeset
   201
c3ef33fd0058 progress: extract stubs to restart ferr.flush() and .write() on EINTR
Yuya Nishihara <yuya@tcha.org>
parents: 30324
diff changeset
   202
    def _flusherr(self):
32049
ed42e00a5c4e progress: retry ferr.flush() and .write() on EINTR (issue5532)
Yuya Nishihara <yuya@tcha.org>
parents: 32048
diff changeset
   203
        _eintrretry(self.ui.ferr.flush)
25497
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   204
32048
c3ef33fd0058 progress: extract stubs to restart ferr.flush() and .write() on EINTR
Yuya Nishihara <yuya@tcha.org>
parents: 30324
diff changeset
   205
    def _writeerr(self, msg):
32049
ed42e00a5c4e progress: retry ferr.flush() and .write() on EINTR (issue5532)
Yuya Nishihara <yuya@tcha.org>
parents: 32048
diff changeset
   206
        _eintrretry(self.ui.ferr.write, msg)
32048
c3ef33fd0058 progress: extract stubs to restart ferr.flush() and .write() on EINTR
Yuya Nishihara <yuya@tcha.org>
parents: 30324
diff changeset
   207
25497
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   208
    def width(self):
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   209
        tw = self.ui.termwidth()
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   210
        return min(int(self.ui.config('progress', 'width', default=tw)), tw)
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   211
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   212
    def estimate(self, topic, pos, total, now):
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   213
        if total is None:
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   214
            return ''
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   215
        initialpos = self.startvals[topic]
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   216
        target = total - initialpos
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   217
        delta = pos - initialpos
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   218
        if delta > 0:
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   219
            elapsed = now - self.starttimes[topic]
34313
f428c347d32b progress: remove progress.estimate config
Jun Wu <quark@fb.com>
parents: 33499
diff changeset
   220
            seconds = (elapsed * (target - delta)) // delta + 1
f428c347d32b progress: remove progress.estimate config
Jun Wu <quark@fb.com>
parents: 33499
diff changeset
   221
            return fmtremaining(seconds)
25497
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   222
        return ''
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   223
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   224
    def speed(self, topic, pos, unit, now):
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   225
        initialpos = self.startvals[topic]
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   226
        delta = pos - initialpos
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   227
        elapsed = now - self.starttimes[topic]
34313
f428c347d32b progress: remove progress.estimate config
Jun Wu <quark@fb.com>
parents: 33499
diff changeset
   228
        if elapsed > 0:
25497
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   229
            return _('%d %s/sec') % (delta / elapsed, unit)
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   230
        return ''
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   231
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   232
    def _oktoprint(self, now):
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   233
        '''Check if conditions are met to print - e.g. changedelay elapsed'''
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   234
        if (self.lasttopic is None # first time we printed
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   235
            # not a topic change
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   236
            or self.curtopic == self.lasttopic
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   237
            # it's been long enough we should print anyway
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   238
            or now - self.lastprint >= self.changedelay):
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   239
            return True
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   240
        else:
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   241
            return False
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   242
34314
a667f0ca1d5f progress: make ETA only consider progress made in the last minute
Jun Wu <quark@fb.com>
parents: 34313
diff changeset
   243
    def _calibrateestimate(self, topic, now, pos):
a667f0ca1d5f progress: make ETA only consider progress made in the last minute
Jun Wu <quark@fb.com>
parents: 34313
diff changeset
   244
        '''Adjust starttimes and startvals for topic so ETA works better
a667f0ca1d5f progress: make ETA only consider progress made in the last minute
Jun Wu <quark@fb.com>
parents: 34313
diff changeset
   245
a667f0ca1d5f progress: make ETA only consider progress made in the last minute
Jun Wu <quark@fb.com>
parents: 34313
diff changeset
   246
        If progress is non-linear (ex. get much slower in the last minute),
a667f0ca1d5f progress: make ETA only consider progress made in the last minute
Jun Wu <quark@fb.com>
parents: 34313
diff changeset
   247
        it's more friendly to only use a recent time span for ETA and speed
a667f0ca1d5f progress: make ETA only consider progress made in the last minute
Jun Wu <quark@fb.com>
parents: 34313
diff changeset
   248
        calculation.
a667f0ca1d5f progress: make ETA only consider progress made in the last minute
Jun Wu <quark@fb.com>
parents: 34313
diff changeset
   249
a667f0ca1d5f progress: make ETA only consider progress made in the last minute
Jun Wu <quark@fb.com>
parents: 34313
diff changeset
   250
            [======================================>       ]
a667f0ca1d5f progress: make ETA only consider progress made in the last minute
Jun Wu <quark@fb.com>
parents: 34313
diff changeset
   251
                                             ^^^^^^^
a667f0ca1d5f progress: make ETA only consider progress made in the last minute
Jun Wu <quark@fb.com>
parents: 34313
diff changeset
   252
                           estimateinterval, only use this for estimation
a667f0ca1d5f progress: make ETA only consider progress made in the last minute
Jun Wu <quark@fb.com>
parents: 34313
diff changeset
   253
        '''
a667f0ca1d5f progress: make ETA only consider progress made in the last minute
Jun Wu <quark@fb.com>
parents: 34313
diff changeset
   254
        interval = self.estimateinterval
a667f0ca1d5f progress: make ETA only consider progress made in the last minute
Jun Wu <quark@fb.com>
parents: 34313
diff changeset
   255
        if interval <= 0:
a667f0ca1d5f progress: make ETA only consider progress made in the last minute
Jun Wu <quark@fb.com>
parents: 34313
diff changeset
   256
            return
a667f0ca1d5f progress: make ETA only consider progress made in the last minute
Jun Wu <quark@fb.com>
parents: 34313
diff changeset
   257
        elapsed = now - self.starttimes[topic]
a667f0ca1d5f progress: make ETA only consider progress made in the last minute
Jun Wu <quark@fb.com>
parents: 34313
diff changeset
   258
        if elapsed > interval:
a667f0ca1d5f progress: make ETA only consider progress made in the last minute
Jun Wu <quark@fb.com>
parents: 34313
diff changeset
   259
            delta = pos - self.startvals[topic]
a667f0ca1d5f progress: make ETA only consider progress made in the last minute
Jun Wu <quark@fb.com>
parents: 34313
diff changeset
   260
            newdelta = delta * interval / elapsed
a667f0ca1d5f progress: make ETA only consider progress made in the last minute
Jun Wu <quark@fb.com>
parents: 34313
diff changeset
   261
            # If a stall happens temporarily, ETA could change dramatically
a667f0ca1d5f progress: make ETA only consider progress made in the last minute
Jun Wu <quark@fb.com>
parents: 34313
diff changeset
   262
            # frequently. This is to avoid such dramatical change and make ETA
a667f0ca1d5f progress: make ETA only consider progress made in the last minute
Jun Wu <quark@fb.com>
parents: 34313
diff changeset
   263
            # smoother.
a667f0ca1d5f progress: make ETA only consider progress made in the last minute
Jun Wu <quark@fb.com>
parents: 34313
diff changeset
   264
            if newdelta < 0.1:
a667f0ca1d5f progress: make ETA only consider progress made in the last minute
Jun Wu <quark@fb.com>
parents: 34313
diff changeset
   265
                return
a667f0ca1d5f progress: make ETA only consider progress made in the last minute
Jun Wu <quark@fb.com>
parents: 34313
diff changeset
   266
            self.startvals[topic] = pos - newdelta
a667f0ca1d5f progress: make ETA only consider progress made in the last minute
Jun Wu <quark@fb.com>
parents: 34313
diff changeset
   267
            self.starttimes[topic] = now - interval
a667f0ca1d5f progress: make ETA only consider progress made in the last minute
Jun Wu <quark@fb.com>
parents: 34313
diff changeset
   268
25497
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   269
    def progress(self, topic, pos, item='', unit='', total=None):
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   270
        now = time.time()
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   271
        self._refreshlock.acquire()
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   272
        try:
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   273
            if pos is None:
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   274
                self.starttimes.pop(topic, None)
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   275
                self.startvals.pop(topic, None)
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   276
                self.topicstates.pop(topic, None)
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   277
                # reset the progress bar if this is the outermost topic
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   278
                if self.topics and self.topics[0] == topic and self.printed:
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   279
                    self.complete()
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   280
                    self.resetstate()
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   281
                # truncate the list of topics assuming all topics within
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   282
                # this one are also closed
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   283
                if topic in self.topics:
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   284
                    self.topics = self.topics[:self.topics.index(topic)]
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   285
                    # reset the last topic to the one we just unwound to,
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   286
                    # so that higher-level topics will be stickier than
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   287
                    # lower-level topics
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   288
                    if self.topics:
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   289
                        self.lasttopic = self.topics[-1]
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   290
                    else:
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   291
                        self.lasttopic = None
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   292
            else:
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   293
                if topic not in self.topics:
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   294
                    self.starttimes[topic] = now
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   295
                    self.startvals[topic] = pos
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   296
                    self.topics.append(topic)
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   297
                self.topicstates[topic] = pos, item, unit, total
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   298
                self.curtopic = topic
34314
a667f0ca1d5f progress: make ETA only consider progress made in the last minute
Jun Wu <quark@fb.com>
parents: 34313
diff changeset
   299
                self._calibrateestimate(topic, now, pos)
25497
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   300
                if now - self.lastprint >= self.refresh and self.topics:
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   301
                    if self._oktoprint(now):
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   302
                        self.lastprint = now
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   303
                        self.show(now, topic, *self.topicstates[topic])
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   304
        finally:
93b8b0049932 progress: move most extension code into a 'mercurial.progress' module
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   305
            self._refreshlock.release()