<s> """<STR_LIT>""" <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> import sys , os <EOL> from curses import * <EOL> from pycopia import environ <EOL> from pycopia . fsm import FSM , ANY <EOL> from pycopia import tty <EOL> from pycopia import UI <EOL> from pycopia . aid import IF <EOL> import curses . textpad <EOL> import curses . panel <EOL> class BasicCursesIO ( object ) : <EOL> def __init__ ( self ) : <EOL> self . stdin = sys . stdin <EOL> self . stdout = sys . stdout <EOL> self . stderr = sys . stderr <EOL> self . mode = "<STR_LIT>" <EOL> self . closed = <NUM_LIT:0> <EOL> self . softspace = <NUM_LIT:0> <EOL> self . read = self . stdin . read <EOL> self . readline = self . stdin . readline <EOL> self . readlines = self . stdin . readlines <EOL> self . write = self . stdout . write <EOL> self . flush = self . stdout . flush <EOL> self . writelines = self . stdout . writelines <EOL> self . set_size ( inputsize ) <EOL> def set_win ( self , win , inputsize = <NUM_LIT:6> ) : <EOL> pass <EOL> def raw_input ( self , prompt = "<STR_LIT>" ) : <EOL> return raw_input ( prompt ) <EOL> def __del__ ( self ) : <EOL> if not self . closed : <EOL> self . close ( ) <EOL> def close ( self ) : <EOL> if not self . closed : <EOL> self . stdout = None <EOL> self . stdin = None <EOL> self . closed = <NUM_LIT:1> <EOL> del self . read , self . readlines , self . write <EOL> del self . flush , self . writelines <EOL> def fileno ( self ) : <EOL> return self . stdout . fileno ( ) <EOL> def isatty ( self ) : <EOL> return self . stdin . isatty ( ) and self . stdout . isatty ( ) <EOL> def errlog ( self , text ) : <EOL> self . stderr . write ( "<STR_LIT>" % ( text , ) ) <EOL> self . stderr . flush ( ) <EOL> def set_size ( self , insize = <NUM_LIT:6> ) : <EOL> self . rows , self . cols , x , y = tty . get_winsize ( self . stdout . fileno ( ) ) <EOL> class CursesIO ( object ) : <EOL> def __init__ ( self , errfile = sys . stderr ) : <EOL> self . mode = "<STR_LIT>" <EOL> self . closed = <NUM_LIT:0> <EOL> self . softspace = <NUM_LIT:0> <EOL> self . stderr = errfile <EOL> self . _win = None <EOL> def set_win ( self , win , inputsize = <NUM_LIT:7> ) : <EOL> self . _win = win <EOL> self . set_size ( inputsize ) <EOL> def _init_outwin ( self , insize ) : <EOL> self . _outwin = self . _win . subwin ( self . rows - insize , self . cols , <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> self . _outwin . scrollok ( <NUM_LIT:1> ) <EOL> self . _outwin . idlok ( <NUM_LIT:1> ) <EOL> def _init_inwin ( self , insize ) : <EOL> self . _inwinborder = self . _win . subwin ( insize , self . cols , self . rows - insize , <NUM_LIT:0> ) <EOL> self . _inwinborder . box ( ) <EOL> self . _inwin = self . _win . subwin ( insize - <NUM_LIT:2> , self . cols - <NUM_LIT:2> , self . rows - insize + <NUM_LIT:2> , <NUM_LIT:2> ) <EOL> self . _inwin . scrollok ( <NUM_LIT:0> ) <EOL> self . _inwin . idlok ( <NUM_LIT:0> ) <EOL> def set_size ( self , insize = <NUM_LIT:7> ) : <EOL> self . rows , self . cols = self . _win . getmaxyx ( ) <EOL> self . _win . setscrreg ( <NUM_LIT:0> , self . rows - insize ) <EOL> self . _init_outwin ( insize ) <EOL> self . _init_inwin ( insize ) <EOL> outputwin = property ( lambda s : s . _outwin ) <EOL> inputwin = property ( lambda s : s . _inwin ) <EOL> root = property ( lambda s : curses . panel . new_panel ( s . _outwin ) ) <EOL> def __del__ ( self ) : <EOL> self . close ( ) <EOL> def __getattr__ ( self , key ) : <EOL> return getattr ( self . _outwin , key ) <EOL> def close ( self ) : <EOL> if not self . closed : <EOL> self . closed = <NUM_LIT:1> <EOL> self . _outwin = None <EOL> self . _inwin = None <EOL> self . _win . attrset ( <NUM_LIT:0> ) <EOL> self . _win . clrtobot ( ) <EOL> self . _win . refresh ( ) <EOL> self . _win = None <EOL> reset_shell_mode ( ) <EOL> def read ( self , n ) : <EOL> if self . closed : <EOL> raise IOError , "<STR_LIT>" <EOL> return self . _win . getstr ( ) <EOL> def write ( self , text , attr = <NUM_LIT:0> ) : <EOL> if self . closed : <EOL> raise IOError , "<STR_LIT>" <EOL> self . _outwin . addstr ( text , attr ) <EOL> self . _outwin . refresh ( ) <EOL> def readline ( self , hint = - <NUM_LIT:1> ) : <EOL> return self . raw_input ( ) + "<STR_LIT:\n>" <EOL> def readlines ( self ) : <EOL> if self . closed : <EOL> raise IOError , "<STR_LIT>" <EOL> rv = [ ] <EOL> t = self . readline ( ) <EOL> while t : <EOL> rv . append ( t ) <EOL> t = self . readline ( ) <EOL> return rv <EOL> def flush ( self ) : <EOL> self . _win . refresh ( ) <EOL> def writelines ( self , lines ) : <EOL> for line in lines : <EOL> self . writeline ( line ) <EOL> def writeline ( self , line , attr = <NUM_LIT:0> ) : <EOL> if self . closed : <EOL> raise IOError , "<STR_LIT>" <EOL> self . _outwin . addstr ( line , attr ) <EOL> self . _outwin . addstr ( "<STR_LIT:\n>" , <NUM_LIT:0> ) <EOL> self . _outwin . refresh ( ) <EOL> def fileno ( self ) : <EOL> return sys . stdout . fileno ( ) <EOL> def isatty ( self ) : <EOL> return sys . stdout . isatty ( ) <EOL> def errlog ( self , text ) : <EOL> self . stderr . write ( "<STR_LIT>" % ( text , ) ) <EOL> self . stderr . flush ( ) <EOL> def raw_input ( self , prompt = "<STR_LIT>" , attr = <NUM_LIT:0> ) : <EOL> self . _inwin . clear ( ) <EOL> self . _inwinborder . box ( ) <EOL> self . _inwin . addstr ( <NUM_LIT:0> , <NUM_LIT:0> , prompt , attr ) <EOL> self . _inwin . refresh ( ) <EOL> return self . _inwin . getstr ( ) <EOL> user_input = raw_input <EOL> def getkey ( self ) : <EOL> return self . _inwin . getkey ( ) <EOL> class CursesTheme ( UI . Theme ) : <EOL> "<STR_LIT>" <EOL> def _setcolors ( cls ) : <EOL> afstr = tigetstr ( "<STR_LIT>" ) <EOL> cls . NORMAL = cls . RESET = tigetstr ( "<STR_LIT>" ) <EOL> cls . BOLD = cls . BRIGHT = tigetstr ( "<STR_LIT>" ) <EOL> cls . BLACK = tparm ( afstr , COLOR_BLACK ) <EOL> cls . RED = tparm ( afstr , COLOR_RED ) <EOL> cls . GREEN = tparm ( afstr , COLOR_GREEN ) <EOL> cls . YELLOW = tparm ( afstr , COLOR_YELLOW ) <EOL> cls . BLUE = tparm ( afstr , COLOR_BLUE ) <EOL> cls . MAGENTA = tparm ( afstr , COLOR_MAGENTA ) <EOL> cls . CYAN = tparm ( afstr , COLOR_CYAN ) <EOL> cls . WHITE = tparm ( afstr , COLOR_WHITE ) <EOL> cls . DEFAULT = tigetstr ( "<STR_LIT>" ) <EOL> cls . GREY = cls . BLACK + cls . BOLD <EOL> cls . BRIGHTRED = cls . RED + cls . BOLD <EOL> cls . BRIGHTGREEN = cls . GREEN + cls . BOLD <EOL> cls . BRIGHTYELLOW = cls . YELLOW + cls . BOLD <EOL> cls . BRIGHTBLUE = cls . BLUE + cls . BOLD <EOL> cls . BRIGHTMAGENTA = cls . MAGENTA + cls . BOLD <EOL> cls . BRIGHTCYAN = cls . CYAN + cls . BOLD <EOL> cls . BRIGHTWHITE = cls . WHITE + cls . BOLD <EOL> cls . UNDERSCORE = tigetstr ( "<STR_LIT>" ) <EOL> cls . BLINK = tigetstr ( "<STR_LIT>" ) <EOL> cls . help_local = COLOR_WHITE <EOL> cls . help_inherited = COLOR_YELLOW <EOL> cls . help_created = COLOR_GREEN <EOL> _setcolors = classmethod ( _setcolors ) <EOL> class UserInterface ( UI . UserInterface ) : <EOL> """<STR_LIT>""" <EOL> def printf ( self , text ) : <EOL> "<STR_LIT>" <EOL> self . Print ( self . format ( text ) ) <EOL> def error ( self , text ) : <EOL> self . _io . errlog ( text ) <EOL> def add_heading ( self , text , level = <NUM_LIT:1> ) : <EOL> s = [ "<STR_LIT:\n>" ] <EOL> s . append ( "<STR_LIT>" % ( "<STR_LIT:U+0020>" * ( level - <NUM_LIT:1> ) , text ) ) <EOL> s . append ( "<STR_LIT>" % ( "<STR_LIT:U+0020>" * ( level - <NUM_LIT:1> ) , "<STR_LIT:->" * len ( text ) ) ) <EOL> s . append ( "<STR_LIT:\n>" ) <EOL> self . Print ( "<STR_LIT:\n>" . join ( s ) ) <EOL> def add_title ( self , title ) : <EOL> self . add_heading ( title , <NUM_LIT:0> ) <EOL> def logfile ( self , filename ) : <EOL> self . _io . write ( "<STR_LIT>" % ( filename , ) ) <EOL> def add_message ( self , msgtype , msg , level = <NUM_LIT:1> , attr = <NUM_LIT:0> ) : <EOL> self . _io . addstr ( "<STR_LIT>" % ( "<STR_LIT:U+0020>" * ( level - <NUM_LIT:1> ) , msgtype ) , attr ) <EOL> self . _io . addstr ( "<STR_LIT>" % msg ) <EOL> self . _io . refresh ( ) <EOL> def add_summary ( self , text ) : <EOL> self . _io . write ( text ) <EOL> def add_text ( self , text ) : <EOL> self . _io . write ( text ) <EOL> def add_url ( self , text , url ) : <EOL> self . _io . write ( "<STR_LIT>" % ( text , url ) ) <EOL> def passed ( self , msg = "<STR_LIT>" , level = <NUM_LIT:1> ) : <EOL> return self . add_message ( self . format ( "<STR_LIT>" ) , msg , level , color_pair ( COLOR_GREEN ) | A_BOLD ) <EOL> def failed ( self , msg = "<STR_LIT>" , level = <NUM_LIT:1> ) : <EOL> return self . add_message ( self . format ( "<STR_LIT>" ) , msg , level , color_pair ( COLOR_RED ) | A_BOLD ) <EOL> def incomplete ( self , msg = "<STR_LIT>" , level = <NUM_LIT:1> ) : <EOL> return self . add_message ( self . format ( "<STR_LIT>" ) , msg , level , color_pair ( COLOR_YELLOW ) ) <EOL> def abort ( self , msg = "<STR_LIT>" , level = <NUM_LIT:1> ) : <EOL> return self . add_message ( self . format ( "<STR_LIT>" ) , msg , level , color_pair ( COLOR_YELLOW ) | A_BOLD ) <EOL> def info ( self , msg , level = <NUM_LIT:1> ) : <EOL> return self . add_message ( "<STR_LIT>" , msg , level ) <EOL> def diagnostic ( self , msg , level = <NUM_LIT:1> ) : <EOL> return self . add_message ( self . format ( "<STR_LIT>" ) , msg , level , color_pair ( COLOR_YELLOW ) ) <EOL> def newpage ( self ) : <EOL> self . _io . write ( "<STR_LIT>" ) <EOL> def newsection ( self ) : <EOL> self . _io . write ( "<STR_LIT>" ) <EOL> def _get_prompt ( self , name , prompt = None ) : <EOL> return prompt or self . _env [ name ] <EOL> def user_input ( self , prompt = None ) : <EOL> return self . _io . raw_input ( self . _get_prompt ( "<STR_LIT>" , prompt ) ) <EOL> def more_user_input ( self ) : <EOL> return self . _io . raw_input ( self . _get_prompt ( "<STR_LIT>" ) ) <EOL> def choose ( self , somelist , defidx = <NUM_LIT:0> , prompt = None ) : <EOL> return choose ( self . _io , somelist , defidx , self . _get_prompt ( "<STR_LIT>" , prompt ) ) <EOL> def get_text ( self , msg = None ) : <EOL> return get_text ( self . _io , self . _get_prompt ( "<STR_LIT>" ) , msg ) <EOL> def get_value ( self , prompt , default = None ) : <EOL> return get_input ( self . _io , prompt , default ) <EOL> def yes_no ( self , prompt , default = True ) : <EOL> return yes_no ( self . _io , prompt , default ) <EOL> def get_key ( prompt = "<STR_LIT>" ) : <EOL> if prompt : <EOL> self . _io . write ( prompt ) <EOL> return self . _io . getkey ( ) <EOL> def _print_doc ( self , s , color ) : <EOL> i = s . find ( "<STR_LIT:\n>" ) <EOL> if i > <NUM_LIT:0> : <EOL> self . _io . addstr ( s [ : i ] , color_pair ( color ) ) <EOL> self . _io . addstr ( s [ i : ] ) <EOL> self . _io . addstr ( "<STR_LIT>" ) <EOL> else : <EOL> self . _io . addstr ( s ) <EOL> self . _io . addstr ( "<STR_LIT>" ) <EOL> self . _io . refresh ( ) <EOL> def help_local ( self , text ) : <EOL> return self . _print_doc ( text , self . _theme . help_local ) <EOL> def help_inherited ( self , text ) : <EOL> return self . _print_doc ( text , self . _theme . help_inherited ) <EOL> def help_created ( self , text ) : <EOL> return self . _print_doc ( text , self . _theme . help_created ) <EOL> def get_text ( io , prompt = "<STR_LIT>" , msg = None ) : <EOL> """<STR_LIT>""" <EOL> py , px = io . getmaxyx ( ) <EOL> outwin = io . derwin ( py - <NUM_LIT:3> , px - <NUM_LIT:20> , <NUM_LIT:3> , <NUM_LIT:10> ) <EOL> outwin . box ( ) <EOL> outwin . addstr ( <NUM_LIT:0> , <NUM_LIT:5> , ( msg or "<STR_LIT>" ) + "<STR_LIT>" ) <EOL> outwin . refresh ( ) <EOL> win = outwin . derwin ( py - <NUM_LIT:5> , px - <NUM_LIT> , <NUM_LIT:2> , <NUM_LIT:2> ) <EOL> tb = curses . textpad . Textbox ( win ) <EOL> text = tb . edit ( ) <EOL> win . erase ( ) <EOL> outwin . erase ( ) <EOL> return text <EOL> def get_input ( io , prompt = "<STR_LIT>" , default = None ) : <EOL> """<STR_LIT>""" <EOL> if default : <EOL> ri = io . raw_input ( "<STR_LIT>" % ( prompt , default ) ) <EOL> if not ri : <EOL> return default <EOL> else : <EOL> return ri <EOL> else : <EOL> return io . raw_input ( "<STR_LIT>" % ( prompt , ) ) <EOL> def choose ( io , somelist , defidx = <NUM_LIT:0> , prompt = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> if len ( somelist ) == <NUM_LIT:0> : <EOL> return None <EOL> root = io . root <EOL> root . bottom ( ) <EOL> py , px = io . getmaxyx ( ) <EOL> outwin = io . derwin ( py - <NUM_LIT:3> , px - <NUM_LIT:10> , <NUM_LIT:3> , <NUM_LIT:5> ) <EOL> outwin . box ( ) <EOL> outwin . refresh ( ) <EOL> win = outwin . derwin ( py - <NUM_LIT:5> , px - <NUM_LIT:12> , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> panel = curses . panel . new_panel ( outwin ) <EOL> print_menu_list ( somelist , win , py ) <EOL> panel . top ( ) <EOL> panel . show ( ) <EOL> defidx = int ( defidx ) <EOL> if defidx < <NUM_LIT:0> : <EOL> defidx = <NUM_LIT:0> <EOL> if defidx >= len ( somelist ) : <EOL> defidx = len ( somelist ) - <NUM_LIT:1> <EOL> try : <EOL> try : <EOL> ri = get_input ( io , prompt , defidx + <NUM_LIT:1> ) <EOL> except EOFError : <EOL> return None <EOL> if ri : <EOL> try : <EOL> idx = int ( ri ) - <NUM_LIT:1> <EOL> except ValueError : <EOL> io . errlog ( "<STR_LIT>" ) <EOL> return None <EOL> else : <EOL> try : <EOL> return somelist [ idx ] <EOL> except IndexError : <EOL> io . errlog ( "<STR_LIT>" ) <EOL> return None <EOL> else : <EOL> return None <EOL> finally : <EOL> panel . hide ( ) <EOL> win . erase ( ) ; outwin . clear ( ) <EOL> root . show ( ) <EOL> def yes_no ( io , prompt , default = True ) : <EOL> yesno = get_input ( io , prompt , IF ( default , "<STR_LIT:Y>" , "<STR_LIT:N>" ) ) <EOL> return yesno . upper ( ) . startswith ( "<STR_LIT:Y>" ) <EOL> def print_menu_list ( clist , win , lines = <NUM_LIT:20> ) : <EOL> """<STR_LIT>""" <EOL> y , x = win . getbegyx ( ) <EOL> h = max ( ( len ( clist ) / <NUM_LIT:2> ) + <NUM_LIT:1> , lines ) <EOL> i1 , i2 = <NUM_LIT:1> , h + <NUM_LIT:1> <EOL> for c1 , c2 in map ( None , clist [ : h ] , clist [ h : ] ) : <EOL> if c2 : <EOL> win . addstr ( y + i1 , <NUM_LIT:1> , "<STR_LIT>" % ( i1 , c1 , i2 , c2 ) ) <EOL> else : <EOL> win . addstr ( y + i1 , <NUM_LIT:1> , "<STR_LIT>" % ( i1 , c1 ) ) <EOL> i1 += <NUM_LIT:1> <EOL> i2 += <NUM_LIT:1> <EOL> win . refresh ( ) <EOL> def get_curses_ui ( ioc = CursesIO , uic = UserInterface , inputsize = <NUM_LIT:7> , env = None ) : <EOL> """<STR_LIT>""" <EOL> tty . save_state ( sys . stdout . fileno ( ) ) <EOL> win = initscr ( ) <EOL> def_shell_mode ( ) <EOL> try : <EOL> start_color ( ) <EOL> except : <EOL> pass <EOL> else : <EOL> init_pair ( COLOR_RED , COLOR_RED , COLOR_BLACK ) <EOL> init_pair ( COLOR_BLUE , COLOR_BLUE , COLOR_BLACK ) <EOL> init_pair ( COLOR_CYAN , COLOR_CYAN , COLOR_BLACK ) <EOL> init_pair ( COLOR_GREEN , COLOR_GREEN , COLOR_BLACK ) <EOL> init_pair ( COLOR_MAGENTA , COLOR_MAGENTA , COLOR_BLACK ) <EOL> init_pair ( COLOR_RED , COLOR_RED , COLOR_BLACK ) <EOL> init_pair ( COLOR_WHITE , COLOR_WHITE , COLOR_BLACK ) <EOL> init_pair ( COLOR_YELLOW , COLOR_YELLOW , COLOR_BLACK ) <EOL> io = ioc ( ) <EOL> io . set_win ( win , inputsize ) <EOL> theme = CursesTheme ( ) <EOL> ui = uic ( io , env , theme ) <EOL> return ui <EOL> def _test ( argv ) : <EOL> ui = get_userinterface ( ) <EOL> ui . Print ( "<STR_LIT>" ) <EOL> inp = ui . user_input ( "<STR_LIT>" ) <EOL> ui . Print ( "<STR_LIT>" , inp ) <EOL> def _test ( argv ) : <EOL> import string <EOL> ui = get_curses_ui ( ) <EOL> l = list ( string . ascii_letters ) <EOL> c = ui . choose ( l ) <EOL> print c <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> import sys <EOL> _test ( sys . argv ) </s>
