summaryrefslogtreecommitdiff
path: root/ttystatus/status.py
blob: a5c40d15a1a58fadb57bbe51b07cf1354c1e70fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# Copyright 2010, 2011  Lars Wirzenius
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


import sys

import ttystatus


class TerminalStatus(object):

    '''Show status and progress information on a terminal.

    All output is provided via widgets of various kinds. Many widgets
    format data that TerminalStatus stores. TerminalStatus provides a
    dict interface for setting and retrieving data items. Unlike a real
    dict, getting a value for a key that has not been set does not
    result in a KeyError exception, but in the empty string being
    returned.

    '''

    def __init__(self, period=None, messager=None, _terminal=None):
        self._m = messager or ttystatus.Messager(
            period=period, _terminal=_terminal)
        self.clear()

    def get_terminal_size(self):  # pragma: no cover
        '''Return terminal width, height.'''
        return self._m.get_terminal_size()

    def add(self, widget):
        '''Add a new widget to the status display.'''
        if not self._widget_rows:
            self._widget_rows = [[]]
        self._widget_rows[-1].append(widget)
        self._register_interests(widget)
        self.flush()

    def _register_interests(self, widget):
        if getattr(widget, 'interested_in', None) is None:
            self._unknown_interest.append(widget)
        else:
            for key in widget.interested_in:
                widgets = self._interested_in.get(key, [])
                widgets.append(widget)
                self._interested_in[key] = widgets

    def start_new_line(self):  # pragma: no cover
        '''Start a new line of widgets.'''
        if not self._widget_rows:
            self._widget_rows = [[]]
        self._widget_rows.append([])
        self.flush()

    def format(self, format_string):
        '''Add new widgets based on format string.

        The format string is taken literally, except that ``%%`` is a
        literal percent character, and ``%Foo(a,b,c)`` is a widget
        of type ``Foo`` with parameters a, b, and c. For example:
        ``format("hello, %String(name)")``.

        '''

        for i, line in enumerate(format_string.split('\n')):
            if i > 0:  # pragma: no cover
                self.start_new_line()
            for widget in ttystatus.parse(line):
                self.add(widget)
        self.flush()

    @property
    def widgets(self):
        result = []
        for row in self._widget_rows:
            result += row
        return result

    def clear(self):
        '''Remove all widgets.'''
        self._widget_rows = []
        self._values = {}
        self._interested_in = {}
        self._unknown_interest = []
        self._m.clear()

    def __getitem__(self, key):
        '''Return value for key, or the empty string.'''
        return self._values.get(key, '')

    def get(self, key, default=None):
        '''Like dict.get.'''
        return self._values.get(key, default)

    def __setitem__(self, key, value):
        '''Set value for key.'''
        self._values[key] = value
        widget_lists = [
            self._interested_in.get(key, []),
            self._unknown_interest,
        ]
        for widgets in widget_lists:
            for w in widgets:
                w.update(self)
        if self._m.enabled and self._m.time_to_write():
            self._write()

    def hide(self):  # pragma: no cover
        '''Hide current progress report.

        Use .flush() to make it visible again. Hiding is useful if you
        want to write things to stdout/stderr that might get mixed
        with progress output. The .notify() and .error() methods get
        disabled if progress reporting gets disabled, but .hide()
        doesn't.

        '''

        self._m.clear()

    def flush(self):

        '''Force an update of current state to the screen.

        This happens even if it is not yet time to output the screen.

        '''

        self._write(force=True)

    def _render(self):
        '''Render current state of all widgets.'''

        return '\n'.join(self._render_row(row) for row in self._widget_rows)

    def _render_row(self, widget_row):
        max_chars = self._m.get_max_line_length()
        remaining = max_chars

        texts = [None] * len(widget_row)

        for i, w in enumerate(widget_row):
            if w.static_width:
                texts[i] = self._make_safe(w.render(0))
                remaining -= len(texts[i])

        for i, w in enumerate(widget_row):
            if not w.static_width:
                texts[i] = self._make_safe(w.render(remaining))
                remaining -= len(texts[i])

        return (''.join(texts))[:max_chars]

    def _make_safe(self, line):
        '''Expand TABs, remove all other ASCII control characters.'''
        ASCII_SPACE = 32
        return ''.join(
            c if ord(c) >= ASCII_SPACE else ''
            for c in line.expandtabs())

    def _write(self, force=False):
        '''Render and output current state of all widgets.'''
        self._m.write(self._render(), force=force)

    def increase(self, key, delta):
        '''Increase value for a key by a given amount.'''
        self[key] = (self[key] or 0) + delta

    def notify(self, msg):
        '''Show a message.'''
        self._m.notify(msg, sys.stdout)

    def error(self, msg):
        '''Write an error message.'''
        self._m.notify(msg, sys.stderr, force=True)

    def finish(self):
        '''Finish status display.'''
        self._write()
        self._m.finish()

    def disable(self):
        '''Disable all output.'''
        self._m.disable()

    def enable(self):
        '''Enable output if it has been disabled.'''
        self._m.enable()