~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/progress.py

Introduce new bzr progress bar api. ui_factory.nested_progress_bar.

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
        """Setup the stack with the parameters the progress bars should have."""
 
80
        self._to_file = to_file
 
81
        self._show_pct = show_pct
 
82
        self._show_spinner = show_spinner
 
83
        self._show_eta = show_eta
 
84
        self._show_bar = show_bar
 
85
        self._show_count = show_count
 
86
        self._to_messages_file = to_messages_file
 
87
        self._stack = []
 
88
 
 
89
    def get_nested(self):
 
90
        """Return a nested progress bar."""
 
91
        # initial implementation - return a new bar each time.
 
92
        new_bar = TTYProgressBar(to_file=self._to_file,
 
93
                                 show_pct=self._show_pct,
 
94
                                 show_spinner=self._show_spinner,
 
95
                                 show_eta=self._show_eta,
 
96
                                 show_bar=self._show_bar,
 
97
                                 show_count=self._show_count,
 
98
                                 to_messages_file=self._to_messages_file,
 
99
                                 _stack=self)
 
100
        self._stack.append(new_bar)
 
101
        return new_bar
 
102
 
 
103
    def return_pb(self, bar):
 
104
        """Return bar after its been used."""
 
105
        if bar != self._stack[-1]:
 
106
            raise errors.MissingProgressBarFinish()
 
107
        self._stack.pop()
 
108
 
 
109
 
65
110
class _BaseProgressBar(object):
 
111
 
66
112
    def __init__(self,
67
113
                 to_file=sys.stderr,
68
114
                 show_pct=False,
70
116
                 show_eta=True,
71
117
                 show_bar=True,
72
118
                 show_count=True,
73
 
                 to_messages_file=sys.stdout):
 
119
                 to_messages_file=sys.stdout,
 
120
                 _stack=None):
74
121
        object.__init__(self)
75
122
        self.to_file = to_file
76
123
        self.to_messages_file = to_messages_file
82
129
        self.show_eta = show_eta
83
130
        self.show_bar = show_bar
84
131
        self.show_count = show_count
 
132
        self._stack = _stack
 
133
 
 
134
    def finished(self):
 
135
        """Return this bar to its progress stack."""
 
136
        self.clear()
 
137
        assert self._stack
 
138
        self._stack.return_pb(self)
85
139
 
86
140
    def note(self, fmt_string, *args, **kwargs):
87
141
        """Record a note without disrupting the progress bar."""