Skip to content

Commit

Permalink
All tests passed
Browse files Browse the repository at this point in the history
  • Loading branch information
InAnYan committed Nov 14, 2024
1 parent dff2ccb commit 35e23d1
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 23 deletions.
4 changes: 2 additions & 2 deletions module/core/format_tools/src/format/output_format/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,15 @@ impl TableOutputFormat for Keys

if x.has_header && x.data.len() != 0
{
for col in &x.data[0]
for col in &x.data[0].1
{
write!( c.buf, " - {}\n", col )?;
}
}

if x.data.len() != 0
{
write!( c.buf, " {} fields\n", x.data[0].len() )?;
write!( c.buf, " {} fields\n", x.data[0].1.len() )?;
}

Ok(())
Expand Down
10 changes: 7 additions & 3 deletions module/core/format_tools/src/format/output_format/records.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,14 +161,18 @@ impl TableOutputFormat for Records

write!( c.buf, "{}", self.table_prefix )?;

for ( ientry, entry ) in x.rows().enumerate()
let mut actual_entries = 0;

for ( ientry, entry ) in x.rows()
{
if ientry > 0
if actual_entries > 0
{
write!( c.buf, "{}", self.table_separator )?;
}

writeln!( c.buf, " = {}", ientry + 1 )?;
actual_entries += 1;

writeln!( c.buf, " = {}", ientry )?;

let row = wrap_text( entry, 0 );

Expand Down
25 changes: 16 additions & 9 deletions module/core/format_tools/src/format/output_format/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ impl TableOutputFormat for Table

let mut col_width : Vec< usize > = vec![ 0; column_count ];

for ( _, row ) in data.iter().enumerate()
for ( _, row ) in data.iter()
{
for ( icol, col ) in row.iter().enumerate()
{
Expand All @@ -197,19 +197,23 @@ impl TableOutputFormat for Table
+ column_count * ( self.cell_postfix.chars().count() + self.cell_prefix.chars().count() )
+ if column_count == 0 { 0 } else { ( column_count - 1 ) * self.cell_separator.chars().count() };

for ( irow, row ) in data.iter().enumerate()
let mut actual_rows = 0;

for ( _, row ) in data.iter()
{
if irow == 1 && x.has_header && self.delimitting_header
if actual_rows == 1 && x.has_header && self.delimitting_header
{
write!( c.buf, "{}", row_separator )?;
write!( c.buf, "{}", h.repeat( max_row_width ) )?;
}

if irow > 0
if actual_rows > 0
{
write!( c.buf, "{}", row_separator )?;
}

actual_rows += 1;

write!( c.buf, "{}", row_prefix )?;

for ( icol, col ) in row.iter().enumerate()
Expand Down Expand Up @@ -259,13 +263,14 @@ struct WrappedCell< 'data >

fn wrap_text< 'data >
(
data: &'data Vec< Vec< Cow< 'data, str > > >,
data: &'data Vec< ( usize, Vec< Cow< 'data, str > > ) >,
_limit: usize
) -> Vec< Vec< WrappedCell< 'data > > >
)
-> Vec< ( usize, Vec< WrappedCell< 'data > > ) >
{
let mut new_data = Vec::new();

for row in data
for ( id, row ) in data
{
let unwrapped_text : Vec< Vec< Cow< 'data, str > > > = row.iter().map( |c| string::lines( c.as_ref() ).map( Cow::from ).collect() ).collect();

Expand Down Expand Up @@ -298,9 +303,11 @@ fn wrap_text< 'data >
transposed.push( row_vec );
}

new_data.extend( transposed );
for row in transposed
{
new_data.push( ( *id, row ) );
}
}

dbg!(&new_data);
new_data
}
25 changes: 16 additions & 9 deletions module/core/format_tools/src/format/print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,8 @@ mod private

/// Extracted data for each cell.
/// If the table has a header, then the first row is treated as a header row with column names.
pub data : Vec< Vec< Cow< 'data, str > > >,
/// Each row is a tuple where the first element is original (unfiltered) row index, and the second are columns.
pub data : Vec< ( usize, Vec< Cow< 'data, str > > ) >,

}

Expand All @@ -252,7 +253,7 @@ mod private
/// This function provides an iterator that yields each row descriptor along with its index.
/// If the table has a header, the first row is skipped, ensuring that iteration starts from
/// the first data row.
pub fn rows( & self ) -> impl _IteratorTrait< Item = &Vec< Cow< 'data, str > > >
pub fn rows( & self ) -> impl _IteratorTrait< Item = &( usize, Vec< Cow< 'data, str > > ) >
{
self.data
.iter()
Expand All @@ -267,11 +268,11 @@ mod private
{
if self.has_header && self.data.len() != 0
{
Box::new( self.data[ 0 ].iter().cloned() )
Box::new( self.data[ 0 ].1.iter().cloned() )
}
else
{
let col_count = if self.data.len() == 0 { 0 } else { self.data[0].len() };
let col_count = if self.data.len() == 0 { 0 } else { self.data[0].1.len() };
Box::new( std::iter::repeat( Cow::Borrowed( "" ) ).take( col_count ) )
}
}
Expand All @@ -297,10 +298,10 @@ mod private
{
let mut has_header = false;

let mut data : Vec< Vec< Cow< 't, str > > > = Vec::new();
let mut data : Vec< ( usize, Vec< Cow< 't, str > > ) > = Vec::new();
let rows = table.rows();

let mut row_add = | row_iter : &'_ mut dyn _IteratorTrait< Item = ( &'t CellKey, Cow< 't, str > ) > |
let mut row_add = | id: usize, row_iter : &'_ mut dyn _IteratorTrait< Item = ( &'t CellKey, Cow< 't, str > ) > |
{
let fields : Vec< Cow< 't, str > > = row_iter
.filter_map
Expand All @@ -319,10 +320,12 @@ mod private

if filter_row.filter_row( &fields )
{
data.push( fields );
data.push( ( id, fields ) );
}
};

let mut row_id = 0;

if let Some( header ) = table.header()
{
has_header = true;
Expand All @@ -332,7 +335,9 @@ mod private
( key, Cow::Borrowed( title ) )
});

row_add( &mut row2 );
row_add( row_id, &mut row2 );

row_id += 1;
}

for row in rows
Expand Down Expand Up @@ -360,7 +365,9 @@ mod private
}
);

row_add( &mut row2 );
row_add( row_id, &mut row2 );

row_id += 1;
}

let x = InputExtract::< '_ >
Expand Down

0 comments on commit 35e23d1

Please sign in to comment.