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