summaryrefslogtreecommitdiff
path: root/src/windows/input_placeholders.py
blob: 796cf0ad314c7a143e10d27d9760c66ef6707184 (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
"""
Classes used to replace the input in some tabs or special situations,
but which are not inputs.
"""

import logging
log = logging.getLogger(__name__)


from . import Win, g_lock
from theming import get_theme, to_curses_attr


class HelpText(Win):
    """
    A Window just displaying a read-only message.
    Usually used to replace an Input when the tab is in
    command mode.
    """
    def __init__(self, text=''):
        Win.__init__(self)
        self.txt = text

    def refresh(self, txt=None):
        log.debug('Refresh: %s', self.__class__.__name__)
        if txt:
            self.txt = txt
        with g_lock:
            self._win.erase()
            self.addstr(0, 0, self.txt[:self.width-1], to_curses_attr(get_theme().COLOR_INFORMATION_BAR))
            self.finish_line(get_theme().COLOR_INFORMATION_BAR)
            self._refresh()

    def do_command(self, key, raw=False):
        return False

class YesNoInput(Win):
    """
    A Window just displaying a Yes/No input
    Used to ask a confirmation
    """
    def __init__(self, text=''):
        Win.__init__(self)
        self.key_func = {
                'y' : self.on_yes,
                'n' : self.on_no,
        }
        self.txt = text
        self.value = None

    def on_yes(self):
        self.value = True

    def on_no(self):
        self.value = False

    def refresh(self, txt=None):
        log.debug('Refresh: %s', self.__class__.__name__)
        if txt:
            self.txt = txt
        with g_lock:
            self._win.erase()
            self.addstr(0, 0, self.txt[:self.width-1], to_curses_attr(get_theme().COLOR_WARNING_PROMPT))
            self.finish_line(get_theme().COLOR_WARNING_PROMPT)
            self._refresh()

    def do_command(self, key, raw=False):
        if key.lower() in self.key_func:
            self.key_func[key]()

    def prompt(self):
        """Monopolizes the input while waiting for a recognized keypress"""
        cl = []
        while self.value is None:
            if len(cl) == 1 and cl[0] in self.key_func:
                self.key_func[cl[0]]()
            cl = self.core.read_keyboard()