#NEWLINE# def render( self ) : #NEWLINE# #INDENT# unscaled = sum ( self . col_width ) #NEWLINE# sep_width = ( ( len ( self . col_width ) - 1 ) * 3 ) #NEWLINE# available_width = ( ( get_termsize ( ) . cols - sep_width ) - 4 ) #NEWLINE# ratio = min ( 1 , ( available_width / unscaled ) ) #NEWLINE# widths = [ max ( 1 , math . floor ( ( w * ratio ) ) ) for w in self . col_width ] #NEWLINE# def _render_row( row , header = False ) : #NEWLINE# #INDENT# cells = [ wrap ( value , width ) for ( value , width ) in zip ( row , widths ) ] #NEWLINE# lines = [ ] #NEWLINE# while any ( cells ) : #NEWLINE# #INDENT# values = [ ] #NEWLINE# for ( cell , width , style ) in zip ( cells , widths , self . styles ) : #NEWLINE# #INDENT# if header : #NEWLINE# #INDENT# style = CENTER #NEWLINE# #UNINDENT# value = style ( ( cell . pop ( 0 ) if cell else '' ) , width ) #NEWLINE# values += [ value ] #NEWLINE# #UNINDENT# line = '| {} |' . format ( ' | ' . join ( values ) ) #NEWLINE# lines . append ( line ) #NEWLINE# #UNINDENT# return ( '\n' . join ( lines ) + '\n' ) #NEWLINE# #UNINDENT# separator = ( ( '+-' + '-+-' . join ( ( ( '-' * w ) for w in widths ) ) ) + '-+\n' ) #NEWLINE# s = separator #NEWLINE# s += _render_row ( self . columns , header = True ) #NEWLINE# s += separator #NEWLINE# for row in self . rows : #NEWLINE# #INDENT# s += _render_row ( row ) #NEWLINE# #UNINDENT# s += separator #NEWLINE# return s