<s> '''<STR_LIT>''' <EOL> import clr , sys <EOL> clr . AddReferenceToFileAndPath ( sys . executable ) <EOL> import IronPythonConsole <EOL> import sys <EOL> import re <EOL> import os <EOL> import System <EOL> from event import Event <EOL> from pyreadline . logger import log , log_sock <EOL> from pyreadline . keysyms import make_keysym , make_keyinfo , make_KeyPress , make_KeyPress_from_keydescr <EOL> from pyreadline . console . ansi import AnsiState <EOL> color = System . ConsoleColor <EOL> ansicolor = { "<STR_LIT>" : color . Black , <EOL> "<STR_LIT>" : color . DarkRed , <EOL> "<STR_LIT>" : color . DarkGreen , <EOL> "<STR_LIT>" : color . DarkYellow , <EOL> "<STR_LIT>" : color . DarkBlue , <EOL> "<STR_LIT>" : color . DarkMagenta , <EOL> "<STR_LIT>" : color . DarkCyan , <EOL> "<STR_LIT>" : color . DarkGray , <EOL> "<STR_LIT>" : color . Gray , <EOL> "<STR_LIT>" : color . Red , <EOL> "<STR_LIT>" : color . Green , <EOL> "<STR_LIT>" : color . Yellow , <EOL> "<STR_LIT>" : color . Blue , <EOL> "<STR_LIT>" : color . Magenta , <EOL> "<STR_LIT>" : color . Cyan , <EOL> "<STR_LIT>" : color . White <EOL> } <EOL> winattr = { "<STR_LIT>" : <NUM_LIT:0> , "<STR_LIT>" : <NUM_LIT:0> + <NUM_LIT:8> , <EOL> "<STR_LIT>" : <NUM_LIT:4> , "<STR_LIT>" : <NUM_LIT:4> + <NUM_LIT:8> , <EOL> "<STR_LIT>" : <NUM_LIT:2> , "<STR_LIT>" : <NUM_LIT:2> + <NUM_LIT:8> , <EOL> "<STR_LIT>" : <NUM_LIT:6> , "<STR_LIT>" : <NUM_LIT:6> + <NUM_LIT:8> , <EOL> "<STR_LIT>" : <NUM_LIT:1> , "<STR_LIT>" : <NUM_LIT:1> + <NUM_LIT:8> , <EOL> "<STR_LIT>" : <NUM_LIT:5> , "<STR_LIT>" : <NUM_LIT:5> + <NUM_LIT:8> , <EOL> "<STR_LIT>" : <NUM_LIT:3> , "<STR_LIT>" : <NUM_LIT:3> + <NUM_LIT:8> , <EOL> "<STR_LIT>" : <NUM_LIT:7> , "<STR_LIT>" : <NUM_LIT:7> + <NUM_LIT:8> } <EOL> class Console ( object ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , newbuffer = <NUM_LIT:0> ) : <EOL> '''<STR_LIT>''' <EOL> self . serial = <NUM_LIT:0> <EOL> self . attr = System . Console . ForegroundColor <EOL> self . saveattr = winattr [ str ( System . Console . ForegroundColor ) . lower ( ) ] <EOL> self . savebg = System . Console . BackgroundColor <EOL> log ( '<STR_LIT>' % self . attr ) <EOL> log_sock ( "<STR_LIT:%s>" % self . saveattr ) <EOL> def _get ( self ) : <EOL> top = System . Console . WindowTop <EOL> log_sock ( "<STR_LIT>" % top , "<STR_LIT>" ) <EOL> return top <EOL> def _set ( self , value ) : <EOL> top = System . Console . WindowTop <EOL> log_sock ( "<STR_LIT>" % ( top , value ) , "<STR_LIT>" ) <EOL> WindowTop = property ( _get , _set ) <EOL> del _get , _set <EOL> def __del__ ( self ) : <EOL> '''<STR_LIT>''' <EOL> pass <EOL> def pos ( self , x = None , y = None ) : <EOL> '''<STR_LIT>''' <EOL> if x is not None : <EOL> System . Console . CursorLeft = x <EOL> else : <EOL> x = System . Console . CursorLeft <EOL> if y is not None : <EOL> System . Console . CursorTop = y <EOL> else : <EOL> y = System . Console . CursorTop <EOL> return x , y <EOL> def home ( self ) : <EOL> '''<STR_LIT>''' <EOL> self . pos ( <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> terminal_escape = re . compile ( '<STR_LIT>' ) <EOL> escape_parts = re . compile ( '<STR_LIT>' ) <EOL> motion_char_re = re . compile ( '<STR_LIT>' ) <EOL> def write_scrolling ( self , text , attr = None ) : <EOL> '''<STR_LIT>''' <EOL> x , y = self . pos ( ) <EOL> w , h = self . size ( ) <EOL> scroll = <NUM_LIT:0> <EOL> chunks = self . motion_char_re . split ( text ) <EOL> for chunk in chunks : <EOL> log ( '<STR_LIT>' + chunk ) <EOL> n = self . write_color ( chunk , attr ) <EOL> if len ( chunk ) == <NUM_LIT:1> : <EOL> if chunk [ <NUM_LIT:0> ] == '<STR_LIT:\n>' : <EOL> x = <NUM_LIT:0> <EOL> y += <NUM_LIT:1> <EOL> elif chunk [ <NUM_LIT:0> ] == '<STR_LIT:\r>' : <EOL> x = <NUM_LIT:0> <EOL> elif chunk [ <NUM_LIT:0> ] == '<STR_LIT:\t>' : <EOL> x = <NUM_LIT:8> * ( int ( x / <NUM_LIT:8> ) + <NUM_LIT:1> ) <EOL> if x > w : <EOL> x -= w <EOL> y += <NUM_LIT:1> <EOL> elif chunk [ <NUM_LIT:0> ] == '<STR_LIT>' : <EOL> pass <EOL> elif chunk [ <NUM_LIT:0> ] == '<STR_LIT>' : <EOL> x -= <NUM_LIT:1> <EOL> if x < <NUM_LIT:0> : <EOL> y -= <NUM_LIT:1> <EOL> else : <EOL> x += <NUM_LIT:1> <EOL> if x == w : <EOL> x = <NUM_LIT:0> <EOL> y += <NUM_LIT:1> <EOL> if y == h : <EOL> scroll += <NUM_LIT:1> <EOL> y = h - <NUM_LIT:1> <EOL> else : <EOL> x += n <EOL> l = int ( x / w ) <EOL> x = x % w <EOL> y += l <EOL> if y >= h : <EOL> scroll += y - h + <NUM_LIT:1> <EOL> y = h - <NUM_LIT:1> <EOL> return scroll <EOL> trtable = { <NUM_LIT:0> : color . Black , <NUM_LIT:4> : color . DarkRed , <NUM_LIT:2> : color . DarkGreen , <NUM_LIT:6> : color . DarkYellow , <EOL> <NUM_LIT:1> : color . DarkBlue , <NUM_LIT:5> : color . DarkMagenta , <NUM_LIT:3> : color . DarkCyan , <NUM_LIT:7> : color . Gray , <EOL> <NUM_LIT:8> : color . DarkGray , <NUM_LIT:4> + <NUM_LIT:8> : color . Red , <NUM_LIT:2> + <NUM_LIT:8> : color . Green , <NUM_LIT:6> + <NUM_LIT:8> : color . Yellow , <EOL> <NUM_LIT:1> + <NUM_LIT:8> : color . Blue , <NUM_LIT:5> + <NUM_LIT:8> : color . Magenta , <NUM_LIT:3> + <NUM_LIT:8> : color . Cyan , <NUM_LIT:7> + <NUM_LIT:8> : color . White } <EOL> def write_color ( self , text , attr = None ) : <EOL> '''<STR_LIT>''' <EOL> log ( '<STR_LIT>' % ( text , attr ) ) <EOL> chunks = self . terminal_escape . split ( text ) <EOL> log ( '<STR_LIT>' % repr ( chunks ) ) <EOL> bg = self . savebg <EOL> n = <NUM_LIT:0> <EOL> if attr is None : <EOL> attr = self . attr <EOL> try : <EOL> fg = self . trtable [ ( <NUM_LIT> & attr ) ] <EOL> bg = self . trtable [ ( <NUM_LIT> & attr ) >> <NUM_LIT:4> ] <EOL> except TypeError : <EOL> fg = attr <EOL> for chunk in chunks : <EOL> m = self . escape_parts . match ( chunk ) <EOL> if m : <EOL> log ( m . group ( <NUM_LIT:1> ) ) <EOL> attr = ansicolor . get ( m . group ( <NUM_LIT:1> ) , self . attr ) <EOL> n += len ( chunk ) <EOL> System . Console . ForegroundColor = fg <EOL> System . Console . BackgroundColor = bg <EOL> System . Console . Write ( chunk ) <EOL> return n <EOL> def write_plain ( self , text , attr = None ) : <EOL> '''<STR_LIT>''' <EOL> log ( '<STR_LIT>' % ( text , attr ) ) <EOL> if attr is None : <EOL> attr = self . attr <EOL> n = c_int ( <NUM_LIT:0> ) <EOL> self . SetConsoleTextAttribute ( self . hout , attr ) <EOL> self . WriteConsoleA ( self . hout , text , len ( text ) , byref ( n ) , None ) <EOL> return len ( text ) <EOL> if os . environ . has_key ( "<STR_LIT>" ) : <EOL> def write_color ( self , text , attr = None ) : <EOL> junk = c_int ( <NUM_LIT:0> ) <EOL> self . WriteFile ( self . hout , text , len ( text ) , byref ( junk ) , None ) <EOL> return len ( text ) <EOL> write_plain = write_color <EOL> def write ( self , text ) : <EOL> log ( '<STR_LIT>' % text ) <EOL> return self . write_color ( text ) <EOL> def isatty ( self ) : <EOL> return True <EOL> def flush ( self ) : <EOL> pass <EOL> def page ( self , attr = None , fill = '<STR_LIT:U+0020>' ) : <EOL> '''<STR_LIT>''' <EOL> System . Console . Clear ( ) <EOL> def text ( self , x , y , text , attr = None ) : <EOL> '''<STR_LIT>''' <EOL> self . pos ( x , y ) <EOL> self . write_color ( text , attr ) <EOL> def clear_to_end_of_window ( self ) : <EOL> oldtop = self . WindowTop <EOL> lastline = self . WindowTop + System . Console . WindowHeight <EOL> pos = self . pos ( ) <EOL> w , h = self . size ( ) <EOL> length = w - pos [ <NUM_LIT:0> ] + min ( ( lastline - pos [ <NUM_LIT:1> ] - <NUM_LIT:1> ) , <NUM_LIT:5> ) * w - <NUM_LIT:1> <EOL> self . write_color ( length * "<STR_LIT:U+0020>" ) <EOL> self . pos ( * pos ) <EOL> self . WindowTop = oldtop <EOL> def rectangle ( self , rect , attr = None , fill = '<STR_LIT:U+0020>' ) : <EOL> '''<STR_LIT>''' <EOL> pass <EOL> oldtop = self . WindowTop <EOL> oldpos = self . pos ( ) <EOL> x0 , y0 , x1 , y1 = rect <EOL> if attr is None : <EOL> attr = self . attr <EOL> if fill : <EOL> rowfill = fill [ : <NUM_LIT:1> ] * abs ( x1 - x0 ) <EOL> else : <EOL> rowfill = '<STR_LIT:U+0020>' * abs ( x1 - x0 ) <EOL> for y in range ( y0 , y1 ) : <EOL> System . Console . SetCursorPosition ( x0 , y ) <EOL> self . write_color ( rowfill , attr ) <EOL> self . pos ( * oldpos ) <EOL> def scroll ( self , rect , dx , dy , attr = None , fill = '<STR_LIT:U+0020>' ) : <EOL> '''<STR_LIT>''' <EOL> pass <EOL> raise NotImplementedError <EOL> def scroll_window ( self , lines ) : <EOL> '''<STR_LIT>''' <EOL> top = self . WindowTop + lines <EOL> if top < <NUM_LIT:0> : <EOL> top = <NUM_LIT:0> <EOL> if top + System . Console . WindowHeight > System . Console . BufferHeight : <EOL> top = System . Console . BufferHeight <EOL> self . WindowTop = top <EOL> def getkeypress ( self ) : <EOL> '''<STR_LIT>''' <EOL> ck = System . ConsoleKey <EOL> while <NUM_LIT:1> : <EOL> e = System . Console . ReadKey ( True ) <EOL> if e . Key == System . ConsoleKey . PageDown : <EOL> self . scroll_window ( <NUM_LIT:12> ) <EOL> elif e . Key == System . ConsoleKey . PageUp : <EOL> self . scroll_window ( - <NUM_LIT:12> ) <EOL> elif str ( e . KeyChar ) == "<STR_LIT>" : <EOL> log_sock ( "<STR_LIT>" % e ) <EOL> return event ( self , e ) <EOL> pass <EOL> else : <EOL> return event ( self , e ) <EOL> def title ( self , txt = None ) : <EOL> '''<STR_LIT>''' <EOL> if txt : <EOL> System . Console . Title = txt <EOL> else : <EOL> return System . Console . Title <EOL> def size ( self , width = None , height = None ) : <EOL> '''<STR_LIT>''' <EOL> sc = System . Console <EOL> if width is not None and height is not None : <EOL> sc . BufferWidth , sc . BufferHeight = width , height <EOL> else : <EOL> return sc . BufferWidth , sc . BufferHeight <EOL> if width is not None and height is not None : <EOL> sc . WindowWidth , sc . WindowHeight = width , height <EOL> else : <EOL> return sc . WindowWidth - <NUM_LIT:1> , sc . WindowHeight - <NUM_LIT:1> <EOL> def cursor ( self , visible = True , size = None ) : <EOL> '''<STR_LIT>''' <EOL> System . Console . CursorVisible = visible <EOL> def bell ( self ) : <EOL> System . Console . Beep ( ) <EOL> def next_serial ( self ) : <EOL> '''<STR_LIT>''' <EOL> self . serial += <NUM_LIT:1> <EOL> return self . serial <EOL> class event ( Event ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , console , input ) : <EOL> '''<STR_LIT>''' <EOL> self . type = '<STR_LIT>' <EOL> self . serial = console . next_serial ( ) <EOL> self . width = <NUM_LIT:0> <EOL> self . height = <NUM_LIT:0> <EOL> self . x = <NUM_LIT:0> <EOL> self . y = <NUM_LIT:0> <EOL> self . char = str ( input . KeyChar ) <EOL> self . keycode = input . Key <EOL> self . state = input . Modifiers <EOL> log_sock ( "<STR_LIT>" % ( input . Modifiers , input . Key , input . KeyChar ) , "<STR_LIT>" ) <EOL> self . type = "<STR_LIT>" <EOL> self . keysym = make_keysym ( self . keycode ) <EOL> self . keyinfo = make_KeyPress ( self . char , self . state , self . keycode ) <EOL> def make_event_from_keydescr ( keydescr ) : <EOL> def input ( ) : <EOL> return <NUM_LIT:1> <EOL> input . KeyChar = "<STR_LIT:a>" <EOL> input . Key = System . ConsoleKey . A <EOL> input . Modifiers = System . ConsoleModifiers . Shift <EOL> input . next_serial = input <EOL> e = event ( input , input ) <EOL> del input . next_serial <EOL> keyinfo = make_KeyPress_from_keydescr ( keydescr ) <EOL> e . keyinfo = keyinfo <EOL> return e <EOL> CTRL_C_EVENT = make_event_from_keydescr ( "<STR_LIT>" ) <EOL> def install_readline ( hook ) : <EOL> def hook_wrap ( ) : <EOL> try : <EOL> res = hook ( ) <EOL> except KeyboardInterrupt , x : <EOL> res = "<STR_LIT>" <EOL> except EOFError : <EOL> return None <EOL> if res [ - <NUM_LIT:1> : ] == "<STR_LIT:\n>" : <EOL> return res [ : - <NUM_LIT:1> ] <EOL> else : <EOL> return res <EOL> class IronPythonWrapper ( IronPythonConsole . IConsole ) : <EOL> def ReadLine ( self , autoIndentSize ) : <EOL> return hook_wrap ( ) <EOL> def Write ( self , text , style ) : <EOL> System . Console . Write ( text ) <EOL> def WriteLine ( self , text , style ) : <EOL> System . Console . WriteLine ( text ) <EOL> IronPythonConsole . PythonCommandLine . MyConsole = IronPythonWrapper ( ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> import time , sys <EOL> c = Console ( <NUM_LIT:0> ) <EOL> sys . stdout = c <EOL> sys . stderr = c <EOL> c . page ( ) <EOL> c . pos ( <NUM_LIT:5> , <NUM_LIT:10> ) <EOL> c . write ( '<STR_LIT>' ) <EOL> c . title ( "<STR_LIT>" ) <EOL> print <EOL> print "<STR_LIT:size>" , c . size ( ) <EOL> print '<STR_LIT>' <EOL> for i in range ( <NUM_LIT:10> ) : <EOL> e = c . getkeypress ( ) <EOL> print e . Key , chr ( e . KeyChar ) , ord ( e . KeyChar ) , e . Modifiers <EOL> del c <EOL> System . Console . Clear ( ) </s>
