<s> import curses <EOL> import os <EOL> from output_processors . output_processor import OutputProcessor <EOL> from table . table import Table <EOL> from utils import printstr , join_line <EOL> class TableProcessor ( OutputProcessor ) : <EOL> @ property <EOL> def height ( self ) : <EOL> return self . _table . height <EOL> @ property <EOL> def name ( self ) : <EOL> return "<STR_LIT>" <EOL> def draw_preview ( self , pad ) : <EOL> if self . _table . selection : <EOL> pad . move ( <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> output = self . _structured_output ( ) <EOL> output_table = Table ( output ) <EOL> printstr ( pad , "<STR_LIT>" . format ( output_table . height , output_table . ncolumns , <EOL> len ( self . _table . selection ) ) , <EOL> curses . color_pair ( <NUM_LIT:7> ) ) <EOL> output_table . draw ( pad , lambda p , content : <NUM_LIT:0> , margin = ( <NUM_LIT:1> , <NUM_LIT:0> ) ) <EOL> def process ( self ) : <EOL> output = self . _structured_output ( ) <EOL> return os . linesep . join ( join_line ( line , self . _delimiter ) for line in output ) <EOL> def _structured_output ( self ) : <EOL> selection = sorted ( self . _table . selection ) <EOL> selection_set = set ( selection ) <EOL> cols = set ( ) <EOL> rows = set ( ) <EOL> for ( i , j ) in selection : <EOL> rows . add ( i ) <EOL> cols . add ( j ) <EOL> output = [ ] <EOL> for i in sorted ( rows ) : <EOL> line = [ ] <EOL> for j in sorted ( cols ) : <EOL> p = ( i , j ) <EOL> width = self . _table . column_width ( j ) <EOL> content = self . _table . get ( p ) if p in selection_set else '<STR_LIT>' <EOL> content = content . ljust ( width ) <EOL> line . append ( content ) <EOL> output . append ( line ) <EOL> return output </s>
