summaryrefslogtreecommitdiff
path: root/interface/dialog.py
blob: 68de590ec620072b6d7ae3f414be5f3f872f389d (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
import curses

from .globals import CURSES_LOCK

def split_words(s, l):
    lines = []
    while len(s) > l:
        work = s[:l]
        for i in reversed(range(l)):
            if work[i] in ' -_':
                lines.append(s[:i+1])
                s = s[i+1:]
                break
        else:
            lines.append(work)
            s = s[l:]
    if s:
        lines.append(s)

    return lines

def askyesnocancel(stdscr, msg, title="Confirm", resize=None):
    height, width = stdscr.getmaxyx()

    thiswidth = min(width, 40)

    posx = (width // 2) - (thiswidth // 2)
    lines = split_words(msg, thiswidth-2)
    thisheight = len(lines) + 3
    posy = (height // 2) - (thisheight // 2)

    if thisheight > height:
        raise ValueError("Not enough room")

    win = curses.newwin(thisheight, thiswidth, posy, posx)
    win.leaveok(True)
    win.border()
    win.addstr(0, (thiswidth // 2) - (len(title) // 2), title)
    win.keypad(True)
    
    for n, l in enumerate(lines, 1):
        win.addstr(n, 1, l)

    win.addstr(thisheight-2, 1, "[Y]es, [N]o, [C]ancel")

    win.refresh()

    curses.curs_set(0)

    while True:
        l = win.getch()

        if l == curses.KEY_RESIZE:
            if resize is not None:
                resize()
            return askyesnocancel(stdscr, msg, title=title, resize=resize)
        elif l in (ord('y'), ord('Y')):
            win.erase()
            return True
        elif l in (ord('n'), ord('N')):
            win.erase()
            return False
        elif l in (ord('c'), ord('C')):
            win.erase()
            return None