blc2 - Python library and frontend for running theatrical lighting and SFX. Written for the English 2041F fall 2019 theatre production of "The Cenci".

git clone https://benconnors.ca/git-repos/blc2

Log | Files | Refs

dialog.py (1608B) - raw


      1 import curses
      2 
      3 from .globals import CURSES_LOCK
      4 
      5 def split_words(s, l):
      6     lines = []
      7     while len(s) > l:
      8         work = s[:l]
      9         for i in reversed(range(l)):
     10             if work[i] in ' -_':
     11                 lines.append(s[:i+1])
     12                 s = s[i+1:]
     13                 break
     14         else:
     15             lines.append(work)
     16             s = s[l:]
     17     if s:
     18         lines.append(s)
     19 
     20     return lines
     21 
     22 def askyesnocancel(stdscr, msg, title="Confirm", resize=None):
     23     height, width = stdscr.getmaxyx()
     24 
     25     thiswidth = min(width, 40)
     26 
     27     posx = (width // 2) - (thiswidth // 2)
     28     lines = split_words(msg, thiswidth-2)
     29     thisheight = len(lines) + 3
     30     posy = (height // 2) - (thisheight // 2)
     31 
     32     if thisheight > height:
     33         raise ValueError("Not enough room")
     34 
     35     win = curses.newwin(thisheight, thiswidth, posy, posx)
     36     win.leaveok(True)
     37     win.border()
     38     win.addstr(0, (thiswidth // 2) - (len(title) // 2), title)
     39     win.keypad(True)
     40     
     41     for n, l in enumerate(lines, 1):
     42         win.addstr(n, 1, l)
     43 
     44     win.addstr(thisheight-2, 1, "[Y]es, [N]o, [C]ancel")
     45 
     46     win.refresh()
     47 
     48     curses.curs_set(0)
     49 
     50     while True:
     51         l = win.getch()
     52 
     53         if l == curses.KEY_RESIZE:
     54             if resize is not None:
     55                 resize()
     56             return askyesnocancel(stdscr, msg, title=title, resize=resize)
     57         elif l in (ord('y'), ord('Y')):
     58             win.erase()
     59             return True
     60         elif l in (ord('n'), ord('N')):
     61             win.erase()
     62             return False
     63         elif l in (ord('c'), ord('C')):
     64             win.erase()
     65             return None