~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/progress.py

Merge in nested progress bars

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# Copyright (C) 2005 Aaron Bentley <aaron.bentley@utoronto.ca>
2
 
# Copyright (C) 2005 Canonical <canonical.com>
 
2
# Copyright (C) 2005, 2006 Canonical <canonical.com>
3
3
#
4
4
#    This program is free software; you can redistribute it and/or modify
5
5
#    it under the terms of the GNU General Public License as published by
42
42
from collections import deque
43
43
 
44
44
 
 
45
import bzrlib.errors as errors
 
46
 
 
47
 
45
48
def _supports_progress(f):
46
49
    if not hasattr(f, 'isatty'):
47
50
        return False
61
64
    else:
62
65
        return DotsProgressBar(to_file=to_file, **kwargs)
63
66
    
64
 
    
 
67
 
 
68
class ProgressBarStack(object):
 
69
    """A stack of progress bars."""
 
70
 
 
71
    def __init__(self,
 
72
                 to_file=sys.stderr,
 
73
                 show_pct=False,
 
74
                 show_spinner=False,
 
75
                 show_eta=True,
 
76
                 show_bar=True,
 
77
                 show_count=True,
 
78
                 to_messages_file=sys.stdout,
 
79
                 klass=None):
 
80
        """Setup the stack with the parameters the progress bars should have."""
 
81
        self._to_file = to_file
 
82
        self._show_pct = show_pct
 
83
        self._show_spinner = show_spinner
 
84
        self._show_eta = show_eta
 
85
        self._show_bar = show_bar
 
86
        self._show_count = show_count
 
87
        self._to_messages_file = to_messages_file
 
88
        self._stack = []
 
89
        self._klass = klass or TTYProgressBar
 
90
 
 
91
    def get_nested(self):
 
92
        """Return a nested progress bar."""
 
93
        # initial implementation - return a new bar each time.
 
94
        new_bar = self._klass(to_file=self._to_file,
 
95
                              show_pct=self._show_pct,
 
96
                              show_spinner=self._show_spinner,
 
97
                              show_eta=self._show_eta,
 
98
                              show_bar=self._show_bar,
 
99
                              show_count=self._show_count,
 
100
                              to_messages_file=self._to_messages_file,
 
101
                              _stack=self)
 
102
        self._stack.append(new_bar)
 
103
        return new_bar
 
104
 
 
105
    def return_pb(self, bar):
 
106
        """Return bar after its been used."""
 
107
        if bar != self._stack[-1]:
 
108
            raise errors.MissingProgressBarFinish()
 
109
        self._stack.pop()
 
110
 
 
111
 
65
112
class _BaseProgressBar(object):
 
113
 
66
114
    def __init__(self,
67
115
                 to_file=sys.stderr,
68
116
                 show_pct=False,
70
118
                 show_eta=True,
71
119
                 show_bar=True,
72
120
                 show_count=True,
73
 
                 to_messages_file=sys.stdout):
 
121
                 to_messages_file=sys.stdout,
 
122
                 _stack=None):
74
123
        object.__init__(self)
75
124
        self.to_file = to_file
76
125
        self.to_messages_file = to_messages_file
82
131
        self.show_eta = show_eta
83
132
        self.show_bar = show_bar
84
133
        self.show_count = show_count
 
134
        self._stack = _stack
 
135
 
 
136
    def finished(self):
 
137
        """Return this bar to its progress stack."""
 
138
        self.clear()
 
139
        assert self._stack is not None
 
140
        self._stack.return_pb(self)
85
141
 
86
142
    def note(self, fmt_string, *args, **kwargs):
87
143
        """Record a note without disrupting the progress bar."""
109
165
 
110
166
 
111
167
class DotsProgressBar(_BaseProgressBar):
 
168
 
112
169
    def __init__(self, **kwargs):
113
170
        _BaseProgressBar.__init__(self, **kwargs)
114
171
        self.last_msg = None