<s> from pygame . locals import * <EOL> from pyrepl . console import Console , Event <EOL> from pyrepl import pygame_keymap <EOL> import pygame <EOL> import types <EOL> lmargin = <NUM_LIT:5> <EOL> rmargin = <NUM_LIT:5> <EOL> tmargin = <NUM_LIT:5> <EOL> bmargin = <NUM_LIT:5> <EOL> try : <EOL> bool <EOL> except NameError : <EOL> def bool ( x ) : <EOL> return not not x <EOL> modcolors = { K_LCTRL : <NUM_LIT:1> , <EOL> K_RCTRL : <NUM_LIT:1> , <EOL> K_LMETA : <NUM_LIT:1> , <EOL> K_RMETA : <NUM_LIT:1> , <EOL> K_LALT : <NUM_LIT:1> , <EOL> K_RALT : <NUM_LIT:1> , <EOL> K_LSHIFT : <NUM_LIT:1> , <EOL> K_RSHIFT : <NUM_LIT:1> } <EOL> class colors : <EOL> fg = <NUM_LIT> , <NUM_LIT> , <NUM_LIT> <EOL> bg = <NUM_LIT:5> , <NUM_LIT:5> , <NUM_LIT:5> <EOL> cursor = <NUM_LIT> , <NUM_LIT:0> , <NUM_LIT> <EOL> margin = <NUM_LIT:5> , <NUM_LIT:5> , <NUM_LIT:15> <EOL> class FakeStdout : <EOL> def __init__ ( self , con ) : <EOL> self . con = con <EOL> def write ( self , text ) : <EOL> self . con . write ( text ) <EOL> def flush ( self ) : <EOL> pass <EOL> class FakeStdin : <EOL> def __init__ ( self , con ) : <EOL> self . con = con <EOL> def read ( self , n = None ) : <EOL> raise NotImplementedError <EOL> def readline ( self , n = None ) : <EOL> from reader import Reader <EOL> try : <EOL> return Reader ( self . con ) . readline ( ) <EOL> except EOFError : <EOL> return '<STR_LIT>' <EOL> class PyGameConsole ( Console ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . pygame_screen = pygame . display . set_mode ( ( <NUM_LIT> , <NUM_LIT> ) ) <EOL> pygame . font . init ( ) <EOL> pygame . key . set_repeat ( <NUM_LIT> , <NUM_LIT:30> ) <EOL> self . font = pygame . font . Font ( <EOL> "<STR_LIT>" , <NUM_LIT:15> ) <EOL> self . fw , self . fh = self . fontsize = self . font . size ( "<STR_LIT:X>" ) <EOL> self . cursor = pygame . Surface ( self . fontsize ) <EOL> self . cursor . fill ( colors . cursor ) <EOL> self . clear ( ) <EOL> self . curs_vis = <NUM_LIT:1> <EOL> self . height , self . width = self . getheightwidth ( ) <EOL> pygame . display . update ( ) <EOL> pygame . event . set_allowed ( None ) <EOL> pygame . event . set_allowed ( KEYDOWN ) <EOL> def install_keymap ( self , keymap ) : <EOL> """<STR_LIT>""" <EOL> self . k = self . keymap = pygame_keymap . compile_keymap ( keymap ) <EOL> def char_rect ( self , x , y ) : <EOL> return self . char_pos ( x , y ) , self . fontsize <EOL> def char_pos ( self , x , y ) : <EOL> return ( lmargin + x * self . fw , <EOL> tmargin + y * self . fh + self . cur_top + self . scroll ) <EOL> def paint_margin ( self ) : <EOL> s = self . pygame_screen <EOL> c = colors . margin <EOL> s . fill ( c , [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT> , tmargin ] ) <EOL> s . fill ( c , [ <NUM_LIT:0> , <NUM_LIT:0> , lmargin , <NUM_LIT> ] ) <EOL> s . fill ( c , [ <NUM_LIT:0> , <NUM_LIT> - bmargin , <NUM_LIT> , bmargin ] ) <EOL> s . fill ( c , [ <NUM_LIT> - rmargin , <NUM_LIT:0> , lmargin , <NUM_LIT> ] ) <EOL> def refresh ( self , screen , ( cx , cy ) ) : <EOL> self . screen = screen <EOL> self . pygame_screen . fill ( colors . bg , <EOL> [ <NUM_LIT:0> , tmargin + self . cur_top + self . scroll , <EOL> <NUM_LIT> , <NUM_LIT> ] ) <EOL> self . paint_margin ( ) <EOL> line_top = self . cur_top <EOL> width , height = self . fontsize <EOL> self . cxy = ( cx , cy ) <EOL> cp = self . char_pos ( cx , cy ) <EOL> if cp [ <NUM_LIT:1> ] < tmargin : <EOL> self . scroll = - ( cy * self . fh + self . cur_top ) <EOL> self . repaint ( ) <EOL> elif cp [ <NUM_LIT:1> ] + self . fh > <NUM_LIT> - bmargin : <EOL> self . scroll += ( <NUM_LIT> - bmargin ) - ( cp [ <NUM_LIT:1> ] + self . fh ) <EOL> self . repaint ( ) <EOL> if self . curs_vis : <EOL> self . pygame_screen . blit ( self . cursor , self . char_pos ( cx , cy ) ) <EOL> for line in screen : <EOL> if <NUM_LIT:0> <= line_top + self . scroll <= ( <NUM_LIT> - bmargin - tmargin - self . fh ) : <EOL> if line : <EOL> ren = self . font . render ( line , <NUM_LIT:1> , colors . fg ) <EOL> self . pygame_screen . blit ( ren , ( lmargin , <EOL> tmargin + line_top + self . scroll ) ) <EOL> line_top += self . fh <EOL> pygame . display . update ( ) <EOL> def prepare ( self ) : <EOL> self . cmd_buf = '<STR_LIT>' <EOL> self . k = self . keymap <EOL> self . height , self . width = self . getheightwidth ( ) <EOL> self . curs_vis = <NUM_LIT:1> <EOL> self . cur_top = self . pos [ <NUM_LIT:0> ] <EOL> self . event_queue = [ ] <EOL> def restore ( self ) : <EOL> pass <EOL> def blit_a_char ( self , linen , charn ) : <EOL> line = self . screen [ linen ] <EOL> if charn < len ( line ) : <EOL> text = self . font . render ( line [ charn ] , <NUM_LIT:1> , colors . fg ) <EOL> self . pygame_screen . blit ( text , self . char_pos ( charn , linen ) ) <EOL> def move_cursor ( self , x , y ) : <EOL> cp = self . char_pos ( x , y ) <EOL> if cp [ <NUM_LIT:1> ] < tmargin or cp [ <NUM_LIT:1> ] + self . fh > <NUM_LIT> - bmargin : <EOL> self . event_queue . append ( Event ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> else : <EOL> if self . curs_vis : <EOL> cx , cy = self . cxy <EOL> self . pygame_screen . fill ( colors . bg , self . char_rect ( cx , cy ) ) <EOL> self . blit_a_char ( cy , cx ) <EOL> self . pygame_screen . blit ( self . cursor , cp ) <EOL> self . blit_a_char ( y , x ) <EOL> pygame . display . update ( ) <EOL> self . cxy = ( x , y ) <EOL> def set_cursor_vis ( self , vis ) : <EOL> self . curs_vis = vis <EOL> if vis : <EOL> self . move_cursor ( * self . cxy ) <EOL> else : <EOL> cx , cy = self . cxy <EOL> self . pygame_screen . fill ( colors . bg , self . char_rect ( cx , cy ) ) <EOL> self . blit_a_char ( cy , cx ) <EOL> pygame . display . update ( ) <EOL> def getheightwidth ( self ) : <EOL> """<STR_LIT>""" <EOL> return ( ( <NUM_LIT> - tmargin - bmargin ) / self . fh , <EOL> ( <NUM_LIT> - lmargin - rmargin ) / self . fw ) <EOL> def tr_event ( self , pyg_event ) : <EOL> shift = bool ( pyg_event . mod & KMOD_SHIFT ) <EOL> ctrl = bool ( pyg_event . mod & KMOD_CTRL ) <EOL> meta = bool ( pyg_event . mod & ( KMOD_ALT | KMOD_META ) ) <EOL> try : <EOL> return self . k [ ( pyg_event . unicode , meta , ctrl ) ] , pyg_event . unicode <EOL> except KeyError : <EOL> try : <EOL> return self . k [ ( pyg_event . key , meta , ctrl ) ] , pyg_event . unicode <EOL> except KeyError : <EOL> return "<STR_LIT>" , pyg_event . unicode <EOL> def get_event ( self , block = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> while <NUM_LIT:1> : <EOL> if self . event_queue : <EOL> return self . event_queue . pop ( <NUM_LIT:0> ) <EOL> elif block : <EOL> pyg_event = pygame . event . wait ( ) <EOL> else : <EOL> pyg_event = pygame . event . poll ( ) <EOL> if pyg_event . type == NOEVENT : <EOL> return <EOL> if pyg_event . key in modcolors : <EOL> continue <EOL> k , c = self . tr_event ( pyg_event ) <EOL> self . cmd_buf += c . encode ( '<STR_LIT:ascii>' , '<STR_LIT:replace>' ) <EOL> self . k = k <EOL> if not isinstance ( k , types . DictType ) : <EOL> e = Event ( k , self . cmd_buf , [ ] ) <EOL> self . k = self . keymap <EOL> self . cmd_buf = '<STR_LIT>' <EOL> return e <EOL> def beep ( self ) : <EOL> pass <EOL> def clear ( self ) : <EOL> """<STR_LIT>""" <EOL> self . pygame_screen . fill ( colors . bg ) <EOL> self . pos = [ <NUM_LIT:0> , <NUM_LIT:0> ] <EOL> self . grobs = [ ] <EOL> self . cur_top = <NUM_LIT:0> <EOL> self . scroll = <NUM_LIT:0> <EOL> def finish ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . curs_vis : <EOL> cx , cy = self . cxy <EOL> self . pygame_screen . fill ( colors . bg , self . char_rect ( cx , cy ) ) <EOL> self . blit_a_char ( cy , cx ) <EOL> for line in self . screen : <EOL> self . write_line ( line , <NUM_LIT:1> ) <EOL> if self . curs_vis : <EOL> self . pygame_screen . blit ( self . cursor , <EOL> ( lmargin + self . pos [ <NUM_LIT:1> ] , <EOL> tmargin + self . pos [ <NUM_LIT:0> ] + self . scroll ) ) <EOL> pygame . display . update ( ) <EOL> def flushoutput ( self ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def forgetinput ( self ) : <EOL> """<STR_LIT>""" <EOL> while pygame . event . poll ( ) . type < > NOEVENT : <EOL> pass <EOL> def getpending ( self ) : <EOL> """<STR_LIT>""" <EOL> events = [ ] <EOL> while <NUM_LIT:1> : <EOL> event = pygame . event . poll ( ) <EOL> if event . type == NOEVENT : <EOL> break <EOL> events . append ( event ) <EOL> return events <EOL> def wait ( self ) : <EOL> """<STR_LIT>""" <EOL> raise Exception , "<STR_LIT>" <EOL> def repaint ( self ) : <EOL> self . pygame_screen . fill ( colors . bg ) <EOL> self . paint_margin ( ) <EOL> for ( y , x ) , surf , text in self . grobs : <EOL> if surf and <NUM_LIT:0> < y + self . scroll : <EOL> self . pygame_screen . blit ( surf , ( lmargin + x , <EOL> tmargin + y + self . scroll ) ) <EOL> pygame . display . update ( ) <EOL> def write_line ( self , line , ret ) : <EOL> charsleft = ( self . width * self . fw - self . pos [ <NUM_LIT:1> ] ) / self . fw <EOL> while len ( line ) > charsleft : <EOL> self . write_line ( line [ : charsleft ] , <NUM_LIT:1> ) <EOL> line = line [ charsleft : ] <EOL> if line : <EOL> ren = self . font . render ( line , <NUM_LIT:1> , colors . fg , colors . bg ) <EOL> self . grobs . append ( ( self . pos [ : ] , ren , line ) ) <EOL> self . pygame_screen . blit ( ren , <EOL> ( lmargin + self . pos [ <NUM_LIT:1> ] , <EOL> tmargin + self . pos [ <NUM_LIT:0> ] + self . scroll ) ) <EOL> else : <EOL> self . grobs . append ( ( self . pos [ : ] , None , line ) ) <EOL> if ret : <EOL> self . pos [ <NUM_LIT:0> ] += self . fh <EOL> if tmargin + self . pos [ <NUM_LIT:0> ] + self . scroll + self . fh > <NUM_LIT> - bmargin : <EOL> self . scroll = <NUM_LIT> - bmargin - self . pos [ <NUM_LIT:0> ] - self . fh - tmargin <EOL> self . repaint ( ) <EOL> self . pos [ <NUM_LIT:1> ] = <NUM_LIT:0> <EOL> else : <EOL> self . pos [ <NUM_LIT:1> ] += self . fw * len ( line ) <EOL> def write ( self , text ) : <EOL> lines = text . split ( "<STR_LIT:\n>" ) <EOL> if self . curs_vis : <EOL> self . pygame_screen . fill ( colors . bg , <EOL> ( lmargin + self . pos [ <NUM_LIT:1> ] , <EOL> tmargin + self . pos [ <NUM_LIT:0> ] + self . scroll , <EOL> self . fw , self . fh ) ) <EOL> for line in lines [ : - <NUM_LIT:1> ] : <EOL> self . write_line ( line , <NUM_LIT:1> ) <EOL> self . write_line ( lines [ - <NUM_LIT:1> ] , <NUM_LIT:0> ) <EOL> if self . curs_vis : <EOL> self . pygame_screen . blit ( self . cursor , <EOL> ( lmargin + self . pos [ <NUM_LIT:1> ] , <EOL> tmargin + self . pos [ <NUM_LIT:0> ] + self . scroll ) ) <EOL> pygame . display . update ( ) <EOL> def flush ( self ) : <EOL> pass </s>
