<s> """<STR_LIT>""" <EOL> import curses , time , traceback , sys <EOL> import curses . wrapper <EOL> from twisted . internet import reactor <EOL> from twisted . internet . protocol import ClientFactory <EOL> from twisted . words . protocols . irc import IRCClient <EOL> from twisted . python import log <EOL> class TextTooLongError ( Exception ) : <EOL> pass <EOL> class CursesStdIO : <EOL> """<STR_LIT>""" <EOL> def fileno ( self ) : <EOL> """<STR_LIT>""" <EOL> return <NUM_LIT:0> <EOL> def doRead ( self ) : <EOL> """<STR_LIT>""" <EOL> def logPrefix ( self ) : return '<STR_LIT>' <EOL> class IRC ( IRCClient ) : <EOL> """<STR_LIT>""" <EOL> nickname = "<STR_LIT>" <EOL> def __init__ ( self , screenObj ) : <EOL> self . screenObj = screenObj <EOL> self . screenObj . irc = self <EOL> def lineReceived ( self , line ) : <EOL> """<STR_LIT>""" <EOL> self . screenObj . addLine ( line ) <EOL> def connectionMade ( self ) : <EOL> IRCClient . connectionMade ( self ) <EOL> self . screenObj . addLine ( "<STR_LIT>" ) <EOL> def clientConnectionLost ( self , connection , reason ) : <EOL> pass <EOL> class IRCFactory ( ClientFactory ) : <EOL> """<STR_LIT>""" <EOL> protocol = IRC <EOL> def __init__ ( self , screenObj ) : <EOL> self . irc = self . protocol ( screenObj ) <EOL> def buildProtocol ( self , addr = None ) : <EOL> return self . irc <EOL> def clientConnectionLost ( self , conn , reason ) : <EOL> pass <EOL> class Screen ( CursesStdIO ) : <EOL> def __init__ ( self , stdscr ) : <EOL> self . timer = <NUM_LIT:0> <EOL> self . statusText = "<STR_LIT>" <EOL> self . searchText = '<STR_LIT>' <EOL> self . stdscr = stdscr <EOL> self . stdscr . nodelay ( <NUM_LIT:1> ) <EOL> curses . cbreak ( ) <EOL> self . stdscr . keypad ( <NUM_LIT:1> ) <EOL> curses . curs_set ( <NUM_LIT:0> ) <EOL> self . rows , self . cols = self . stdscr . getmaxyx ( ) <EOL> self . lines = [ ] <EOL> curses . start_color ( ) <EOL> curses . init_pair ( <NUM_LIT:1> , curses . COLOR_BLACK , curses . COLOR_WHITE ) <EOL> curses . init_pair ( <NUM_LIT:2> , curses . COLOR_CYAN , curses . COLOR_BLACK ) <EOL> self . paintStatus ( self . statusText ) <EOL> def connectionLost ( self , reason ) : <EOL> self . close ( ) <EOL> def addLine ( self , text ) : <EOL> """<STR_LIT>""" <EOL> self . lines . append ( text ) <EOL> self . redisplayLines ( ) <EOL> def redisplayLines ( self ) : <EOL> """<STR_LIT>""" <EOL> self . stdscr . clear ( ) <EOL> self . paintStatus ( self . statusText ) <EOL> i = <NUM_LIT:0> <EOL> index = len ( self . lines ) - <NUM_LIT:1> <EOL> while i < ( self . rows - <NUM_LIT:3> ) and index >= <NUM_LIT:0> : <EOL> self . stdscr . addstr ( self . rows - <NUM_LIT:3> - i , <NUM_LIT:0> , self . lines [ index ] , <EOL> curses . color_pair ( <NUM_LIT:2> ) ) <EOL> i = i + <NUM_LIT:1> <EOL> index = index - <NUM_LIT:1> <EOL> self . stdscr . refresh ( ) <EOL> def paintStatus ( self , text ) : <EOL> if len ( text ) > self . cols : raise TextTooLongError <EOL> self . stdscr . addstr ( self . rows - <NUM_LIT:2> , <NUM_LIT:0> , text + '<STR_LIT:U+0020>' * ( self . cols - len ( text ) ) , <EOL> curses . color_pair ( <NUM_LIT:1> ) ) <EOL> self . stdscr . move ( self . rows - <NUM_LIT:1> , self . cols - <NUM_LIT:1> ) <EOL> def doRead ( self ) : <EOL> """<STR_LIT>""" <EOL> curses . noecho ( ) <EOL> self . timer = self . timer + <NUM_LIT:1> <EOL> c = self . stdscr . getch ( ) <EOL> if c == curses . KEY_BACKSPACE : <EOL> self . searchText = self . searchText [ : - <NUM_LIT:1> ] <EOL> elif c == curses . KEY_ENTER or c == <NUM_LIT:10> : <EOL> self . addLine ( self . searchText ) <EOL> try : self . irc . sendLine ( self . searchText ) <EOL> except : pass <EOL> self . stdscr . refresh ( ) <EOL> self . searchText = '<STR_LIT>' <EOL> else : <EOL> if len ( self . searchText ) == self . cols - <NUM_LIT:2> : return <EOL> self . searchText = self . searchText + chr ( c ) <EOL> self . stdscr . addstr ( self . rows - <NUM_LIT:1> , <NUM_LIT:0> , <EOL> self . searchText + ( '<STR_LIT:U+0020>' * ( <EOL> self . cols - len ( self . searchText ) - <NUM_LIT:2> ) ) ) <EOL> self . stdscr . move ( self . rows - <NUM_LIT:1> , len ( self . searchText ) ) <EOL> self . paintStatus ( self . statusText + '<STR_LIT>' % len ( self . searchText ) ) <EOL> self . stdscr . refresh ( ) <EOL> def close ( self ) : <EOL> """<STR_LIT>""" <EOL> curses . nocbreak ( ) <EOL> self . stdscr . keypad ( <NUM_LIT:0> ) <EOL> curses . echo ( ) <EOL> curses . endwin ( ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> stdscr = curses . initscr ( ) <EOL> screen = Screen ( stdscr ) <EOL> stdscr . refresh ( ) <EOL> ircFactory = IRCFactory ( screen ) <EOL> reactor . addReader ( screen ) <EOL> reactor . connectTCP ( "<STR_LIT>" , <NUM_LIT> , ircFactory ) <EOL> reactor . run ( ) <EOL> screen . close ( ) </s>
