summaryrefslogtreecommitdiff
path: root/ttystatus/status.py
blob: 012014a24bae90aa06560f4c64ce2f465e43adaa (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
# Copyright 2010  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 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, output=None, period=None, messager=None):
        self._m = messager or ttystatus.Messager(output=output, period=period)
        self.clear()
        
    def add(self, widget):
        '''Add a new widget to the status display.'''
        self._widgets.append(widget)
        
    def clear(self):
        '''Remove all widgets.'''
        self._widgets = []
        self._values = dict()
        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
        width = self._m.width
        for w in self._widgets:
            if w.interested_in(key):
                w.update(self, width)
                width -= len(str(w))
        self._m.write(''.join(str(w) for w in self._widgets))
    
    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)
    
    def finish(self):
        '''Finish status display.'''
        self._m.finish()