<s> from __future__ import unicode_literals <EOL> import logging <EOL> import itertools <EOL> from six . moves import range <EOL> from curtsies import fsarray , fmtstr <EOL> from curtsies . formatstring import linesplit <EOL> from curtsies . fmtfuncs import bold <EOL> from bpython . curtsiesfrontend . parse import func_for_letter <EOL> from bpython . _py3compat import py3 <EOL> if not py3 : <EOL> import inspect <EOL> logger = logging . getLogger ( __name__ ) <EOL> def display_linize ( msg , columns , blank_line = False ) : <EOL> """<STR_LIT>""" <EOL> display_lines = ( [ msg [ start : end ] <EOL> for start , end in zip ( <EOL> range ( <NUM_LIT:0> , len ( msg ) , columns ) , <EOL> range ( columns , len ( msg ) + columns , columns ) ) ] <EOL> if msg else ( [ '<STR_LIT>' ] if blank_line else [ ] ) ) <EOL> return display_lines <EOL> def paint_history ( rows , columns , display_lines ) : <EOL> lines = [ ] <EOL> for r , line in zip ( range ( rows ) , display_lines [ - rows : ] ) : <EOL> lines . append ( fmtstr ( line [ : columns ] ) ) <EOL> r = fsarray ( lines , width = columns ) <EOL> assert r . shape [ <NUM_LIT:0> ] <= rows , repr ( r . shape ) + '<STR_LIT:U+0020>' + repr ( rows ) <EOL> assert r . shape [ <NUM_LIT:1> ] <= columns , repr ( r . shape ) + '<STR_LIT:U+0020>' + repr ( columns ) <EOL> return r <EOL> def paint_current_line ( rows , columns , current_display_line ) : <EOL> lines = display_linize ( current_display_line , columns , True ) <EOL> return fsarray ( lines , width = columns ) <EOL> def paginate ( rows , matches , current , words_wide ) : <EOL> if current not in matches : <EOL> current = matches [ <NUM_LIT:0> ] <EOL> per_page = rows * words_wide <EOL> current_page = matches . index ( current ) // per_page <EOL> return matches [ per_page * current_page : per_page * ( current_page + <NUM_LIT:1> ) ] <EOL> def matches_lines ( rows , columns , matches , current , config , format ) : <EOL> highlight_color = func_for_letter ( config . color_scheme [ '<STR_LIT>' ] . lower ( ) ) <EOL> if not matches : <EOL> return [ ] <EOL> color = func_for_letter ( config . color_scheme [ '<STR_LIT>' ] ) <EOL> max_match_width = max ( len ( m ) for m in matches ) <EOL> words_wide = max ( <NUM_LIT:1> , ( columns - <NUM_LIT:1> ) // ( max_match_width + <NUM_LIT:1> ) ) <EOL> matches = [ format ( m ) for m in matches ] <EOL> if current : <EOL> current = format ( current ) <EOL> matches = paginate ( rows , matches , current , words_wide ) <EOL> matches_lines = [ fmtstr ( '<STR_LIT:U+0020>' ) . join ( color ( m . ljust ( max_match_width ) ) <EOL> if m != current <EOL> else highlight_color ( <EOL> m . ljust ( max_match_width ) ) <EOL> for m in matches [ i : i + words_wide ] ) <EOL> for i in range ( <NUM_LIT:0> , len ( matches ) , words_wide ) ] <EOL> logger . debug ( '<STR_LIT>' % current ) <EOL> logger . debug ( '<STR_LIT>' % matches_lines ) <EOL> return matches_lines <EOL> def formatted_argspec ( funcprops , arg_pos , columns , config ) : <EOL> func = funcprops . func <EOL> args = funcprops . argspec . args <EOL> kwargs = funcprops . argspec . defaults <EOL> _args = funcprops . argspec . varargs <EOL> _kwargs = funcprops . argspec . varkwargs <EOL> is_bound_method = funcprops . is_bound_method <EOL> if py3 : <EOL> kwonly = funcprops . argspec . kwonly <EOL> kwonly_defaults = funcprops . argspec . kwonly_defaults or dict ( ) <EOL> arg_color = func_for_letter ( config . color_scheme [ '<STR_LIT:name>' ] ) <EOL> func_color = func_for_letter ( config . color_scheme [ '<STR_LIT:name>' ] . swapcase ( ) ) <EOL> punctuation_color = func_for_letter ( config . color_scheme [ '<STR_LIT>' ] ) <EOL> token_color = func_for_letter ( config . color_scheme [ '<STR_LIT>' ] ) <EOL> bolds = { token_color : lambda x : bold ( token_color ( x ) ) , <EOL> arg_color : lambda x : bold ( arg_color ( x ) ) } <EOL> s = func_color ( func ) + arg_color ( '<STR_LIT>' ) <EOL> if is_bound_method and isinstance ( arg_pos , int ) : <EOL> arg_pos += <NUM_LIT:1> <EOL> for i , arg in enumerate ( args ) : <EOL> kw = None <EOL> if kwargs and i >= len ( args ) - len ( kwargs ) : <EOL> kw = str ( kwargs [ i - ( len ( args ) - len ( kwargs ) ) ] ) <EOL> color = token_color if arg_pos in ( i , arg ) else arg_color <EOL> if i == arg_pos or arg == arg_pos : <EOL> color = bolds [ color ] <EOL> if not py3 : <EOL> s += color ( inspect . strseq ( arg , unicode ) ) <EOL> else : <EOL> s += color ( arg ) <EOL> if kw is not None : <EOL> s += punctuation_color ( '<STR_LIT:=>' ) <EOL> if not py3 : <EOL> kw = kw . decode ( '<STR_LIT:ascii>' , '<STR_LIT:replace>' ) <EOL> s += token_color ( kw ) <EOL> if i != len ( args ) - <NUM_LIT:1> : <EOL> s += punctuation_color ( '<STR_LIT:U+002CU+0020>' ) <EOL> if _args : <EOL> if args : <EOL> s += punctuation_color ( '<STR_LIT:U+002CU+0020>' ) <EOL> s += token_color ( '<STR_LIT>' % ( _args , ) ) <EOL> if py3 and kwonly : <EOL> if not _args : <EOL> if args : <EOL> s += punctuation_color ( '<STR_LIT:U+002CU+0020>' ) <EOL> s += punctuation_color ( '<STR_LIT:*>' ) <EOL> marker = object ( ) <EOL> for arg in kwonly : <EOL> s += punctuation_color ( '<STR_LIT:U+002CU+0020>' ) <EOL> color = token_color <EOL> if arg_pos : <EOL> color = bolds [ color ] <EOL> s += color ( arg ) <EOL> default = kwonly_defaults . get ( arg , marker ) <EOL> if default is not marker : <EOL> s += punctuation_color ( '<STR_LIT:=>' ) <EOL> s += token_color ( repr ( default ) ) <EOL> if _kwargs : <EOL> if args or _args or ( py3 and kwonly ) : <EOL> s += punctuation_color ( '<STR_LIT:U+002CU+0020>' ) <EOL> s += token_color ( '<STR_LIT>' % ( _kwargs , ) ) <EOL> s += punctuation_color ( '<STR_LIT:)>' ) <EOL> return linesplit ( s , columns ) <EOL> def formatted_docstring ( docstring , columns , config ) : <EOL> color = func_for_letter ( config . color_scheme [ '<STR_LIT>' ] ) <EOL> return sum ( ( [ color ( x ) for x in ( display_linize ( line , columns ) if line else <EOL> fmtstr ( '<STR_LIT>' ) ) ] <EOL> for line in docstring . split ( '<STR_LIT:\n>' ) ) , [ ] ) <EOL> def paint_infobox ( rows , columns , matches , funcprops , arg_pos , match , docstring , <EOL> config , format ) : <EOL> """<STR_LIT>""" <EOL> if not ( rows and columns ) : <EOL> return fsarray ( <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> width = columns - <NUM_LIT:4> <EOL> from_argspec = ( formatted_argspec ( funcprops , arg_pos , width , config ) <EOL> if funcprops else [ ] ) <EOL> from_doc = ( formatted_docstring ( docstring , width , config ) <EOL> if docstring else [ ] ) <EOL> from_matches = ( matches_lines ( max ( <NUM_LIT:1> , rows - len ( from_argspec ) - <NUM_LIT:2> ) , <EOL> width , matches , match , config , format ) <EOL> if matches else [ ] ) <EOL> lines = from_argspec + from_matches + from_doc <EOL> def add_border ( line ) : <EOL> """<STR_LIT>""" <EOL> new_line = border_color ( config . left_border + '<STR_LIT:U+0020>' ) <EOL> new_line += line . ljust ( width ) [ : width ] <EOL> new_line += border_color ( '<STR_LIT:U+0020>' + config . right_border ) <EOL> return new_line <EOL> border_color = func_for_letter ( config . color_scheme [ '<STR_LIT>' ] ) <EOL> top_line = border_color ( config . left_top_corner + <EOL> config . top_border * ( width + <NUM_LIT:2> ) + <EOL> config . right_top_corner ) <EOL> bottom_line = border_color ( config . left_bottom_corner + <EOL> config . bottom_border * ( width + <NUM_LIT:2> ) + <EOL> config . right_bottom_corner ) <EOL> output_lines = list ( itertools . chain ( ( top_line , ) , map ( add_border , lines ) , <EOL> ( bottom_line , ) ) ) <EOL> r = fsarray ( output_lines [ : min ( rows - <NUM_LIT:1> , <EOL> len ( output_lines ) - <NUM_LIT:1> ) ] + output_lines [ - <NUM_LIT:1> : ] ) <EOL> return r <EOL> def paint_last_events ( rows , columns , names , config ) : <EOL> if not names : <EOL> return fsarray ( [ ] ) <EOL> width = min ( max ( len ( name ) for name in names ) , columns - <NUM_LIT:2> ) <EOL> output_lines = [ ] <EOL> output_lines . append ( config . left_top_corner + config . top_border * width + <EOL> config . right_top_corner ) <EOL> for name in reversed ( names [ max ( <NUM_LIT:0> , len ( names ) - ( rows - <NUM_LIT:2> ) ) : ] ) : <EOL> output_lines . append ( config . left_border + name [ : width ] . center ( width ) + <EOL> config . right_border ) <EOL> output_lines . append ( config . left_bottom_corner + <EOL> config . bottom_border * width + <EOL> config . right_bottom_corner ) <EOL> return fsarray ( output_lines ) <EOL> def paint_statusbar ( rows , columns , msg , config ) : <EOL> func = func_for_letter ( config . color_scheme [ '<STR_LIT>' ] ) <EOL> return fsarray ( [ func ( msg . ljust ( columns ) ) [ : columns ] ] ) </s>
