<s> import termios , select , os , struct , errno <EOL> import signal , re , time , sys <EOL> from fcntl import ioctl <EOL> from pyrepl import curses <EOL> from pyrepl . fancy_termios import tcgetattr , tcsetattr <EOL> from pyrepl . console import Console , Event <EOL> from pyrepl import unix_eventqueue <EOL> SIGWINCH_EVENT = '<STR_LIT>' <EOL> FIONREAD = getattr ( termios , "<STR_LIT>" , None ) <EOL> TIOCGWINSZ = getattr ( termios , "<STR_LIT>" , None ) <EOL> def _my_getstr ( cap , optional = <NUM_LIT:0> ) : <EOL> r = curses . tigetstr ( cap ) <EOL> if not optional and r is None : <EOL> raise RuntimeError , "<STR_LIT>" % cap <EOL> return r <EOL> def maybe_add_baudrate ( dict , rate ) : <EOL> name = '<STR_LIT>' % rate <EOL> if hasattr ( termios , name ) : <EOL> dict [ getattr ( termios , name ) ] = rate <EOL> ratedict = { } <EOL> for r in [ <NUM_LIT:0> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:200> , <NUM_LIT> , <EOL> <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:50> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] : <EOL> maybe_add_baudrate ( ratedict , r ) <EOL> del r , maybe_add_baudrate <EOL> delayprog = re . compile ( "<STR_LIT>" ) <EOL> try : <EOL> poll = select . poll <EOL> except AttributeError : <EOL> class poll : <EOL> def __init__ ( self ) : <EOL> pass <EOL> def register ( self , fd , flag ) : <EOL> self . fd = fd <EOL> def poll ( self , timeout = None ) : <EOL> r , w , e = select . select ( [ self . fd ] , [ ] , [ ] , timeout ) <EOL> return r <EOL> POLLIN = getattr ( select , "<STR_LIT>" , None ) <EOL> class UnixConsole ( Console ) : <EOL> def __init__ ( self , f_in = <NUM_LIT:0> , f_out = <NUM_LIT:1> , term = None , encoding = None ) : <EOL> if encoding is None : <EOL> encoding = sys . getdefaultencoding ( ) <EOL> self . encoding = encoding <EOL> if isinstance ( f_in , int ) : <EOL> self . input_fd = f_in <EOL> else : <EOL> self . input_fd = f_in . fileno ( ) <EOL> if isinstance ( f_out , int ) : <EOL> self . output_fd = f_out <EOL> else : <EOL> self . output_fd = f_out . fileno ( ) <EOL> self . pollob = poll ( ) <EOL> self . pollob . register ( self . input_fd , POLLIN ) <EOL> curses . setupterm ( term , self . output_fd ) <EOL> self . term = term <EOL> self . _bel = _my_getstr ( "<STR_LIT>" ) <EOL> self . _civis = _my_getstr ( "<STR_LIT>" , optional = <NUM_LIT:1> ) <EOL> self . _clear = _my_getstr ( "<STR_LIT>" ) <EOL> self . _cnorm = _my_getstr ( "<STR_LIT>" , optional = <NUM_LIT:1> ) <EOL> self . _cub = _my_getstr ( "<STR_LIT>" , optional = <NUM_LIT:1> ) <EOL> self . _cub1 = _my_getstr ( "<STR_LIT>" , <NUM_LIT:1> ) <EOL> self . _cud = _my_getstr ( "<STR_LIT>" , <NUM_LIT:1> ) <EOL> self . _cud1 = _my_getstr ( "<STR_LIT>" , <NUM_LIT:1> ) <EOL> self . _cuf = _my_getstr ( "<STR_LIT>" , <NUM_LIT:1> ) <EOL> self . _cuf1 = _my_getstr ( "<STR_LIT>" , <NUM_LIT:1> ) <EOL> self . _cup = _my_getstr ( "<STR_LIT>" ) <EOL> self . _cuu = _my_getstr ( "<STR_LIT>" , <NUM_LIT:1> ) <EOL> self . _cuu1 = _my_getstr ( "<STR_LIT>" , <NUM_LIT:1> ) <EOL> self . _dch1 = _my_getstr ( "<STR_LIT>" , <NUM_LIT:1> ) <EOL> self . _dch = _my_getstr ( "<STR_LIT>" , <NUM_LIT:1> ) <EOL> self . _el = _my_getstr ( "<STR_LIT>" ) <EOL> self . _hpa = _my_getstr ( "<STR_LIT>" , <NUM_LIT:1> ) <EOL> self . _ich = _my_getstr ( "<STR_LIT>" , <NUM_LIT:1> ) <EOL> self . _ich1 = _my_getstr ( "<STR_LIT>" , <NUM_LIT:1> ) <EOL> self . _ind = _my_getstr ( "<STR_LIT>" , <NUM_LIT:1> ) <EOL> self . _pad = _my_getstr ( "<STR_LIT>" , <NUM_LIT:1> ) <EOL> self . _ri = _my_getstr ( "<STR_LIT>" , <NUM_LIT:1> ) <EOL> self . _rmkx = _my_getstr ( "<STR_LIT>" , <NUM_LIT:1> ) <EOL> self . _smkx = _my_getstr ( "<STR_LIT>" , <NUM_LIT:1> ) <EOL> if <NUM_LIT:0> and self . _hpa : <EOL> self . __move_x = self . __move_x_hpa <EOL> elif self . _cub and self . _cuf : <EOL> self . __move_x = self . __move_x_cub_cuf <EOL> elif self . _cub1 and self . _cuf1 : <EOL> self . __move_x = self . __move_x_cub1_cuf1 <EOL> else : <EOL> raise RuntimeError , "<STR_LIT>" <EOL> if self . _cuu and self . _cud : <EOL> self . __move_y = self . __move_y_cuu_cud <EOL> elif self . _cuu1 and self . _cud1 : <EOL> self . __move_y = self . __move_y_cuu1_cud1 <EOL> else : <EOL> raise RuntimeError , "<STR_LIT>" <EOL> if self . _dch1 : <EOL> self . dch1 = self . _dch1 <EOL> elif self . _dch : <EOL> self . dch1 = curses . tparm ( self . _dch , <NUM_LIT:1> ) <EOL> else : <EOL> self . dch1 = None <EOL> if self . _ich1 : <EOL> self . ich1 = self . _ich1 <EOL> elif self . _ich : <EOL> self . ich1 = curses . tparm ( self . _ich , <NUM_LIT:1> ) <EOL> else : <EOL> self . ich1 = None <EOL> self . __move = self . __move_short <EOL> self . event_queue = unix_eventqueue . EventQueue ( self . input_fd ) <EOL> self . partial_char = '<STR_LIT>' <EOL> self . cursor_visible = <NUM_LIT:1> <EOL> def change_encoding ( self , encoding ) : <EOL> self . encoding = encoding <EOL> def refresh ( self , screen , ( cx , cy ) ) : <EOL> if not self . __gone_tall : <EOL> while len ( self . screen ) < min ( len ( screen ) , self . height ) : <EOL> self . __hide_cursor ( ) <EOL> self . __move ( <NUM_LIT:0> , len ( self . screen ) - <NUM_LIT:1> ) <EOL> self . __write ( "<STR_LIT:\n>" ) <EOL> self . __posxy = <NUM_LIT:0> , len ( self . screen ) <EOL> self . screen . append ( "<STR_LIT>" ) <EOL> else : <EOL> while len ( self . screen ) < len ( screen ) : <EOL> self . screen . append ( "<STR_LIT>" ) <EOL> if len ( screen ) > self . height : <EOL> self . __gone_tall = <NUM_LIT:1> <EOL> self . __move = self . __move_tall <EOL> px , py = self . __posxy <EOL> old_offset = offset = self . __offset <EOL> height = self . height <EOL> if <NUM_LIT:0> : <EOL> global counter <EOL> try : <EOL> counter <EOL> except NameError : <EOL> counter = <NUM_LIT:0> <EOL> self . __write_code ( curses . tigetstr ( "<STR_LIT>" ) , counter ) <EOL> counter += <NUM_LIT:1> <EOL> if counter > <NUM_LIT:8> : <EOL> counter = <NUM_LIT:0> <EOL> if cy < offset : <EOL> offset = cy <EOL> elif cy >= offset + height : <EOL> offset = cy - height + <NUM_LIT:1> <EOL> elif offset > <NUM_LIT:0> and len ( screen ) < offset + height : <EOL> offset = max ( len ( screen ) - height , <NUM_LIT:0> ) <EOL> screen . append ( "<STR_LIT>" ) <EOL> oldscr = self . screen [ old_offset : old_offset + height ] <EOL> newscr = screen [ offset : offset + height ] <EOL> if old_offset > offset and self . _ri : <EOL> self . __hide_cursor ( ) <EOL> self . __write_code ( self . _cup , <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> self . __posxy = <NUM_LIT:0> , old_offset <EOL> for i in range ( old_offset - offset ) : <EOL> self . __write_code ( self . _ri ) <EOL> oldscr . pop ( - <NUM_LIT:1> ) <EOL> oldscr . insert ( <NUM_LIT:0> , "<STR_LIT>" ) <EOL> elif old_offset < offset and self . _ind : <EOL> self . __hide_cursor ( ) <EOL> self . __write_code ( self . _cup , self . height - <NUM_LIT:1> , <NUM_LIT:0> ) <EOL> self . __posxy = <NUM_LIT:0> , old_offset + self . height - <NUM_LIT:1> <EOL> for i in range ( offset - old_offset ) : <EOL> self . __write_code ( self . _ind ) <EOL> oldscr . pop ( <NUM_LIT:0> ) <EOL> oldscr . append ( "<STR_LIT>" ) <EOL> self . __offset = offset <EOL> for y , oldline , newline , in zip ( range ( offset , offset + height ) , <EOL> oldscr , <EOL> newscr ) : <EOL> if oldline != newline : <EOL> self . __write_changed_line ( y , oldline , newline , px ) <EOL> y = len ( newscr ) <EOL> while y < len ( oldscr ) : <EOL> self . __hide_cursor ( ) <EOL> self . __move ( <NUM_LIT:0> , y ) <EOL> self . __posxy = <NUM_LIT:0> , y <EOL> self . __write_code ( self . _el ) <EOL> y += <NUM_LIT:1> <EOL> self . __show_cursor ( ) <EOL> self . screen = screen <EOL> self . move_cursor ( cx , cy ) <EOL> self . flushoutput ( ) <EOL> def __write_changed_line ( self , y , oldline , newline , px ) : <EOL> x = <NUM_LIT:0> <EOL> minlen = min ( len ( oldline ) , len ( newline ) ) <EOL> while x < minlen and oldline [ x ] == newline [ x ] : <EOL> x += <NUM_LIT:1> <EOL> if oldline [ x : ] == newline [ x + <NUM_LIT:1> : ] and self . ich1 : <EOL> if ( y == self . __posxy [ <NUM_LIT:1> ] and x > self . __posxy [ <NUM_LIT:0> ] <EOL> and oldline [ px : x ] == newline [ px + <NUM_LIT:1> : x + <NUM_LIT:1> ] ) : <EOL> x = px <EOL> self . __move ( x , y ) <EOL> self . __write_code ( self . ich1 ) <EOL> self . __write ( newline [ x ] ) <EOL> self . __posxy = x + <NUM_LIT:1> , y <EOL> elif x < minlen and oldline [ x + <NUM_LIT:1> : ] == newline [ x + <NUM_LIT:1> : ] : <EOL> self . __move ( x , y ) <EOL> self . __write ( newline [ x ] ) <EOL> self . __posxy = x + <NUM_LIT:1> , y <EOL> elif ( self . dch1 and self . ich1 and len ( newline ) == self . width <EOL> and x < len ( newline ) - <NUM_LIT:2> <EOL> and newline [ x + <NUM_LIT:1> : - <NUM_LIT:1> ] == oldline [ x : - <NUM_LIT:2> ] ) : <EOL> self . __hide_cursor ( ) <EOL> self . __move ( self . width - <NUM_LIT:2> , y ) <EOL> self . __posxy = self . width - <NUM_LIT:2> , y <EOL> self . __write_code ( self . dch1 ) <EOL> self . __move ( x , y ) <EOL> self . __write_code ( self . ich1 ) <EOL> self . __write ( newline [ x ] ) <EOL> self . __posxy = x + <NUM_LIT:1> , y <EOL> else : <EOL> self . __hide_cursor ( ) <EOL> self . __move ( x , y ) <EOL> if len ( oldline ) > len ( newline ) : <EOL> self . __write_code ( self . _el ) <EOL> self . __write ( newline [ x : ] ) <EOL> self . __posxy = len ( newline ) , y <EOL> def __write ( self , text ) : <EOL> self . __buffer . append ( ( text , <NUM_LIT:0> ) ) <EOL> def __write_code ( self , fmt , * args ) : <EOL> self . __buffer . append ( ( curses . tparm ( fmt , * args ) , <NUM_LIT:1> ) ) <EOL> def __maybe_write_code ( self , fmt , * args ) : <EOL> if fmt : <EOL> self . __write_code ( fmt , * args ) <EOL> def __move_y_cuu1_cud1 ( self , y ) : <EOL> dy = y - self . __posxy [ <NUM_LIT:1> ] <EOL> if dy > <NUM_LIT:0> : <EOL> self . __write_code ( dy * self . _cud1 ) <EOL> elif dy < <NUM_LIT:0> : <EOL> self . __write_code ( ( - dy ) * self . _cuu1 ) <EOL> def __move_y_cuu_cud ( self , y ) : <EOL> dy = y - self . __posxy [ <NUM_LIT:1> ] <EOL> if dy > <NUM_LIT:0> : <EOL> self . __write_code ( self . _cud , dy ) <EOL> elif dy < <NUM_LIT:0> : <EOL> self . __write_code ( self . _cuu , - dy ) <EOL> def __move_x_hpa ( self , x ) : <EOL> if x != self . __posxy [ <NUM_LIT:0> ] : <EOL> self . __write_code ( self . _hpa , x ) <EOL> def __move_x_cub1_cuf1 ( self , x ) : <EOL> dx = x - self . __posxy [ <NUM_LIT:0> ] <EOL> if dx > <NUM_LIT:0> : <EOL> self . __write_code ( self . _cuf1 * dx ) <EOL> elif dx < <NUM_LIT:0> : <EOL> self . __write_code ( self . _cub1 * ( - dx ) ) <EOL> def __move_x_cub_cuf ( self , x ) : <EOL> dx = x - self . __posxy [ <NUM_LIT:0> ] <EOL> if dx > <NUM_LIT:0> : <EOL> self . __write_code ( self . _cuf , dx ) <EOL> elif dx < <NUM_LIT:0> : <EOL> self . __write_code ( self . _cub , - dx ) <EOL> def __move_short ( self , x , y ) : <EOL> self . __move_x ( x ) <EOL> self . __move_y ( y ) <EOL> def __move_tall ( self , x , y ) : <EOL> assert <NUM_LIT:0> <= y - self . __offset < self . height , y - self . __offset <EOL> self . __write_code ( self . _cup , y - self . __offset , x ) <EOL> def move_cursor ( self , x , y ) : <EOL> if y < self . __offset or y >= self . __offset + self . height : <EOL> self . event_queue . insert ( Event ( '<STR_LIT>' , None ) ) <EOL> else : <EOL> self . __move ( x , y ) <EOL> self . __posxy = x , y <EOL> self . flushoutput ( ) <EOL> def prepare ( self ) : <EOL> self . __svtermstate = tcgetattr ( self . input_fd ) <EOL> raw = self . __svtermstate . copy ( ) <EOL> raw . iflag &= ~ ( termios . BRKINT | termios . INPCK | <EOL> termios . ISTRIP | termios . IXON ) <EOL> raw . oflag &= ~ ( termios . OPOST ) <EOL> raw . cflag &= ~ ( termios . CSIZE | termios . PARENB ) <EOL> raw . cflag |= ( termios . CS8 ) <EOL> raw . lflag &= ~ ( termios . ICANON | termios . ECHO | <EOL> termios . IEXTEN | ( termios . ISIG * <NUM_LIT:1> ) ) <EOL> raw . cc [ termios . VMIN ] = <NUM_LIT:1> <EOL> raw . cc [ termios . VTIME ] = <NUM_LIT:0> <EOL> tcsetattr ( self . input_fd , termios . TCSADRAIN , raw ) <EOL> self . screen = [ ] <EOL> self . height , self . width = self . getheightwidth ( ) <EOL> self . __buffer = [ ] <EOL> self . __posxy = <NUM_LIT:0> , <NUM_LIT:0> <EOL> self . __gone_tall = <NUM_LIT:0> <EOL> self . __move = self . __move_short <EOL> self . __offset = <NUM_LIT:0> <EOL> self . __maybe_write_code ( self . _smkx ) <EOL> self . old_sigwinch = signal . signal ( <EOL> signal . SIGWINCH , self . __sigwinch ) <EOL> def restore ( self ) : <EOL> self . __maybe_write_code ( self . _rmkx ) <EOL> self . flushoutput ( ) <EOL> tcsetattr ( self . input_fd , termios . TCSADRAIN , self . __svtermstate ) <EOL> signal . signal ( signal . SIGWINCH , self . old_sigwinch ) <EOL> def __sigwinch ( self , signum , frame ) : <EOL> self . height , self . width = self . getheightwidth ( ) <EOL> self . event_queue . insert ( Event ( '<STR_LIT>' , None ) ) <EOL> def push_char ( self , char ) : <EOL> self . partial_char += char <EOL> try : <EOL> c = unicode ( self . partial_char , self . encoding ) <EOL> except UnicodeError , e : <EOL> if len ( e . args ) > <NUM_LIT:4> and e . args [ <NUM_LIT:4> ] == '<STR_LIT>' : <EOL> pass <EOL> else : <EOL> raise <EOL> else : <EOL> self . partial_char = '<STR_LIT>' <EOL> self . event_queue . push ( c ) <EOL> def get_event ( self , block = <NUM_LIT:1> ) : <EOL> while self . event_queue . empty ( ) : <EOL> while <NUM_LIT:1> : <EOL> try : <EOL> self . push_char ( os . read ( self . input_fd , <NUM_LIT:1> ) ) <EOL> except ( IOError , OSError ) , err : <EOL> if err . errno == errno . EINTR : <EOL> if not self . event_queue . empty ( ) : <EOL> return self . event_queue . get ( ) <EOL> else : <EOL> continue <EOL> else : <EOL> raise <EOL> else : <EOL> break <EOL> if not block : <EOL> break <EOL> return self . event_queue . get ( ) <EOL> def wait ( self ) : <EOL> self . pollob . poll ( ) <EOL> def set_cursor_vis ( self , vis ) : <EOL> if vis : <EOL> self . __show_cursor ( ) <EOL> else : <EOL> self . __hide_cursor ( ) <EOL> def __hide_cursor ( self ) : <EOL> if self . cursor_visible : <EOL> self . __maybe_write_code ( self . _civis ) <EOL> self . cursor_visible = <NUM_LIT:0> <EOL> def __show_cursor ( self ) : <EOL> if not self . cursor_visible : <EOL> self . __maybe_write_code ( self . _cnorm ) <EOL> self . cursor_visible = <NUM_LIT:1> <EOL> def repaint_prep ( self ) : <EOL> if not self . __gone_tall : <EOL> self . __posxy = <NUM_LIT:0> , self . __posxy [ <NUM_LIT:1> ] <EOL> self . __write ( "<STR_LIT:\r>" ) <EOL> ns = len ( self . screen ) * [ '<STR_LIT>' * self . width ] <EOL> self . screen = ns <EOL> else : <EOL> self . __posxy = <NUM_LIT:0> , self . __offset <EOL> self . __move ( <NUM_LIT:0> , self . __offset ) <EOL> ns = self . height * [ '<STR_LIT>' * self . width ] <EOL> self . screen = ns <EOL> if TIOCGWINSZ : <EOL> def getheightwidth ( self ) : <EOL> try : <EOL> return int ( os . environ [ "<STR_LIT>" ] ) , int ( os . environ [ "<STR_LIT>" ] ) <EOL> except KeyError : <EOL> height , width = struct . unpack ( <EOL> "<STR_LIT>" , ioctl ( self . input_fd , TIOCGWINSZ , "<STR_LIT>" * <NUM_LIT:8> ) ) [ <NUM_LIT:0> : <NUM_LIT:2> ] <EOL> if not height : return <NUM_LIT> , <NUM_LIT> <EOL> return height , width <EOL> else : <EOL> def getheightwidth ( self ) : <EOL> try : <EOL> return int ( os . environ [ "<STR_LIT>" ] ) , int ( os . environ [ "<STR_LIT>" ] ) <EOL> except KeyError : <EOL> return <NUM_LIT> , <NUM_LIT> <EOL> def forgetinput ( self ) : <EOL> termios . tcflush ( self . input_fd , termios . TCIFLUSH ) <EOL> def flushoutput ( self ) : <EOL> for text , iscode in self . __buffer : <EOL> if iscode : <EOL> self . __tputs ( text ) <EOL> else : <EOL> os . write ( self . output_fd , text . encode ( self . encoding ) ) <EOL> del self . __buffer [ : ] <EOL> def __tputs ( self , fmt , prog = delayprog ) : <EOL> """<STR_LIT>""" <EOL> bps = ratedict . get ( self . __svtermstate . ospeed ) <EOL> while <NUM_LIT:1> : <EOL> m = prog . search ( fmt ) <EOL> if not m : <EOL> os . write ( self . output_fd , fmt ) <EOL> break <EOL> x , y = m . span ( ) <EOL> os . write ( self . output_fd , fmt [ : x ] ) <EOL> fmt = fmt [ y : ] <EOL> delay = int ( m . group ( <NUM_LIT:1> ) ) <EOL> if '<STR_LIT:*>' in m . group ( <NUM_LIT:2> ) : <EOL> delay *= self . height <EOL> if self . _pad : <EOL> nchars = ( bps * delay ) / <NUM_LIT:1000> <EOL> os . write ( self . output_fd , self . _pad * nchars ) <EOL> else : <EOL> time . sleep ( float ( delay ) / <NUM_LIT> ) <EOL> def finish ( self ) : <EOL> y = len ( self . screen ) - <NUM_LIT:1> <EOL> while y >= <NUM_LIT:0> and not self . screen [ y ] : <EOL> y -= <NUM_LIT:1> <EOL> self . __move ( <NUM_LIT:0> , min ( y , self . height + self . __offset - <NUM_LIT:1> ) ) <EOL> self . __write ( "<STR_LIT>" ) <EOL> self . flushoutput ( ) <EOL> def beep ( self ) : <EOL> self . __maybe_write_code ( self . _bel ) <EOL> self . flushoutput ( ) <EOL> if FIONREAD : <EOL> def getpending ( self ) : <EOL> e = Event ( '<STR_LIT:key>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> while not self . event_queue . empty ( ) : <EOL> e2 = self . event_queue . get ( ) <EOL> e . data += e2 . data <EOL> e . raw += e . raw <EOL> amount = struct . unpack ( <EOL> "<STR_LIT:i>" , ioctl ( self . input_fd , FIONREAD , "<STR_LIT>" ) ) [ <NUM_LIT:0> ] <EOL> raw = unicode ( os . read ( self . input_fd , amount ) , self . encoding , '<STR_LIT:replace>' ) <EOL> e . data += raw <EOL> e . raw += raw <EOL> return e <EOL> else : <EOL> def getpending ( self ) : <EOL> e = Event ( '<STR_LIT:key>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> while not self . event_queue . empty ( ) : <EOL> e2 = self . event_queue . get ( ) <EOL> e . data += e2 . data <EOL> e . raw += e . raw <EOL> amount = <NUM_LIT> <EOL> raw = unicode ( os . read ( self . input_fd , amount ) , self . encoding , '<STR_LIT:replace>' ) <EOL> e . data += raw <EOL> e . raw += raw <EOL> return e <EOL> def clear ( self ) : <EOL> self . __write_code ( self . _clear ) <EOL> self . __gone_tall = <NUM_LIT:1> <EOL> self . __move = self . __move_tall <EOL> self . __posxy = <NUM_LIT:0> , <NUM_LIT:0> <EOL> self . screen = [ ] </s>
