Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 6 additions & 52 deletions module/core/format_tools/src/format/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,6 @@ mod private
borrow::Cow,
};

/// Represents a line type in a table, either a header or a regular row.
///
/// `LineType` is used to distinguish between different types of lines
/// in a table structure, aiding in formatting and processing.
///
#[ derive( Debug, Default, PartialEq, Eq, Copy, Clone ) ]
pub enum LineType
{
/// Represents a regular row of data in the table.
#[ default ]
Regular,
/// Represents a header line in the table.
Header,
}

// = filters

/// Filter passing all elements.
Expand All @@ -45,11 +30,6 @@ mod private
{
/// Filter columns of a table to print it only partially.
fn filter_col( &self, key : &str ) -> bool;
/// Determine is arguments needed for the filter or it can give answer even without arguments. Useful for optimization.
fn need_args( &self ) -> bool
{
true
}
}

impl Default for &'static dyn FilterCol
Expand Down Expand Up @@ -78,11 +58,6 @@ mod private
{
true
}
#[ inline( always ) ]
fn need_args( &self ) -> bool
{
false
}
}

impl None
Expand All @@ -102,11 +77,6 @@ mod private
{
false
}
#[ inline( always ) ]
fn need_args( &self ) -> bool
{
false
}
}

impl< F : Fn( &str ) -> bool > FilterCol for F
Expand All @@ -124,12 +94,7 @@ mod private
pub trait FilterRow
{
/// Filter rows of a table to print it only partially.
fn filter_row( &self, typ : LineType, irow : usize, row : &[ ( Cow< '_, str >, [ usize ; 2 ] ) ] ) -> bool;
/// Determine is arguments needed for the filter or it can give answer even without arguments. Useful for optimization.
fn need_args( &self ) -> bool
{
true
}
fn filter_row( &self, row : &[ Cow< '_, str > ] ) -> bool;
}

impl Default for &'static dyn FilterRow
Expand All @@ -144,15 +109,10 @@ mod private
impl FilterRow for All
{
#[ inline( always ) ]
fn filter_row( &self, _typ : LineType, _irow : usize, _row : &[ ( Cow< '_, str >, [ usize ; 2 ] ) ] ) -> bool
fn filter_row( &self, _row : &[ Cow< '_, str > ] ) -> bool
{
true
}
#[ inline( always ) ]
fn need_args( &self ) -> bool
{
false
}
}

impl All
Expand All @@ -168,12 +128,7 @@ mod private
impl FilterRow for None
{
#[ inline( always ) ]
fn filter_row( &self, _typ : LineType, _irow : usize, _row : &[ ( Cow< '_, str >, [ usize ; 2 ] ) ] ) -> bool
{
false
}
#[ inline( always ) ]
fn need_args( &self ) -> bool
fn filter_row( &self, _row : &[ Cow< '_, str > ] ) -> bool
{
false
}
Expand All @@ -189,12 +144,12 @@ mod private
}
}

impl< F : Fn( LineType, usize, &[ ( Cow< '_, str >, [ usize ; 2 ] ) ] ) -> bool > FilterRow for F
impl< F : Fn( &[ Cow< '_, str > ] ) -> bool > FilterRow for F
{
#[ inline( always ) ]
fn filter_row( &self, typ : LineType, irow : usize, row : &[ ( Cow< '_, str >, [ usize ; 2 ] ) ] ) -> bool
fn filter_row( &self, row : &[ Cow< '_, str > ] ) -> bool
{
self( typ, irow, row )
self( row )
}
}

Expand Down Expand Up @@ -231,7 +186,6 @@ pub mod orphan
#[ doc( inline ) ]
pub use private::
{
LineType,
FilterCol,
FilterRow,
};
Expand Down
14 changes: 9 additions & 5 deletions module/core/format_tools/src/format/output_format/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,18 @@ impl TableOutputFormat for Keys
) -> fmt::Result
{

// dbg!( &x );

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

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

Ok(())
}
Expand Down
74 changes: 41 additions & 33 deletions module/core/format_tools/src/format/output_format/records.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@
//!

use crate::*;
use md_math::MdOffset;
use print::
{
InputExtract,
Context,
};
use std::borrow::{ Cow, Borrow };
use core::
{
fmt,
Expand Down Expand Up @@ -156,69 +156,77 @@ impl TableOutputFormat for Records
) -> fmt::Result
{

let label_width = x.header().fold( 0, | acc, cell | acc.max( cell.1[ 0 ] ) );
let field_names : Vec< Cow< 'data, str > > = x.header().collect();
let key_width = x.header().fold( 0, | acc, cell | acc.max( cell.len() ) );

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

let mut first = true;
// Write each record
for ( irow, row ) in x.rows()
{
let mut actual_entries = 0;

if !row.vis
{
continue;
}

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

let slice_width = x.data[ irow ].iter().fold( 0, | acc, cell | acc.max( cell.1[ 0 ] ) );
actual_entries += 1;

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

for ( icol, _col ) in x.col_descriptors.iter().enumerate()
{
let cell = &x.data[ irow ][ icol ];
let height = cell.1[ 1 ];
let row = wrap_text( entry, 0 );

for islice in 0..height
{
let label = x.header_slice( islice, icol );
let md_index = [ islice, icol, irow ];
let slice = x.slices[ x.slices_dim.md_offset( md_index ) ];
let value_width = row.iter().map( |sr| sr.iter().map( |c| c.chars().count() ).max().unwrap_or(0) ).max().unwrap_or(0);

let mut row_count = 0;

if icol > 0 || islice > 0
for ( ifield, field ) in row.iter().enumerate()
{
for ( irow, row ) in field.iter().enumerate()
{
if row_count > 0
{
write!( c.buf, "{}", self.row_separator )?;
}
row_count += 1;

let key = if irow > 0
{
""
}
else
{
field_names.get( ifield ).map( Cow::borrow ).unwrap_or( "" )
};

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

write!( c.buf, "{}", self.cell_prefix )?;
write!( c.buf, "{:<label_width$}", label )?;
write!( c.buf, "{:<key_width$}", key )?;
write!( c.buf, "{}", self.cell_postfix )?;
write!( c.buf, "{}", self.cell_separator )?;
write!( c.buf, "{}", self.cell_prefix )?;
write!( c.buf, "{:<slice_width$}", slice )?;
write!( c.buf, "{:<value_width$}", row )?;
write!( c.buf, "{}", self.cell_postfix )?;

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

}

}

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

Ok(())
Ok( () )
}

}

fn wrap_text<'data>
(
data: &'data Vec< Cow< 'data, str > >,
_limit: usize
)
-> Vec< Vec< &'data str > >
{
data.iter().map( |c| string::lines( c ).collect() ).collect()
}
Loading
Loading