diff options
Diffstat (limited to 'interface/dialog.py')
-rw-r--r-- | interface/dialog.py | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/interface/dialog.py b/interface/dialog.py new file mode 100644 index 0000000..18ecb34 --- /dev/null +++ b/interface/dialog.py @@ -0,0 +1,64 @@ +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.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 |