Skip to content

Commit

Permalink
AUTO : Forward from format_tools_evolving_1 to alpha (#1450)
Browse files Browse the repository at this point in the history
format_tools : evolve records format
  • Loading branch information
wtools-bot authored Sep 3, 2024
1 parent b96531c commit 41a4e7b
Show file tree
Hide file tree
Showing 6 changed files with 520 additions and 302 deletions.
15 changes: 14 additions & 1 deletion module/core/format_tools/src/format/md_math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,20 @@ mod private
/// # Returns
///
/// A value of type `T` representing the flat offset.
fn md_offset( & self, md_index : [ T ; 3 ] ) -> T;
fn md_offset( & self, md_index : Self ) -> T;
}

impl< T > MdOffset< T > for [ T ; 2 ]
where
T : Mul< T, Output = T > + Add< T, Output = T > + PartialOrd + Copy + fmt::Debug,
{
fn md_offset( & self, md_index : [ T ; 2 ] ) -> T
{
debug_assert!( md_index[ 0 ] < self[ 0 ], "md_index : {md_index:?} | md_size : {self:?}" );
debug_assert!( md_index[ 1 ] < self[ 1 ], "md_index : {md_index:?} | md_size : {self:?}" );
let m1 = self[ 0 ];
md_index[ 0 ] + m1 * md_index[ 1 ]
}
}

impl< T > MdOffset< T > for [ T ; 3 ]
Expand Down
156 changes: 145 additions & 11 deletions module/core/format_tools/src/format/output_format/records.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,52 +39,186 @@ use std::sync::OnceLock;
/// `Records` provides an implementation for table formatting that outputs
/// each row as a separate table with 2 columns, first is name of column in the original data and second is cell value itself.
#[derive( Debug )]
pub struct Records;
pub struct Records
{
/// Prefix added to each row.
pub table_prefix : String,
/// Postfix added to each row.
pub table_postfix : String,
/// Separator used between rows.
pub table_separator : String,
/// Prefix added to each row.
pub row_prefix : String,
/// Postfix added to each row.
pub row_postfix : String,
/// Separator used between rows.
pub row_separator : String,
/// Prefix added to each cell.
pub cell_prefix : String,
/// Postfix added to each cell.
pub cell_postfix : String,
/// Separator used between table columns.
pub cell_separator : String,
// /// Horizontal line character.
// pub h : char,
// /// Vertical line character.
// pub v : char,
// /// Left T-junction character.
// pub t_l : char,
// /// Right T-junction character.
// pub t_r : char,
// /// Top T-junction character.
// pub t_t : char,
// /// Bottom T-junction character.
// pub t_b : char,
// /// Cross junction character.
// pub cross : char,
// /// Top-left corner character.
// pub corner_lt : char,
// /// Top-right corner character.
// pub corner_rt : char,
// /// Bottom-left corner character.
// pub corner_lb : char,
// /// Bottom-right corner character.
// pub corner_rb : char,
}

impl Records
{
/// Returns a reference to a static instance of `Records`.
pub fn instance() -> & 'static dyn TableOutputFormat
{
static INSTANCE : OnceLock< Records > = OnceLock::new();
INSTANCE.get_or_init( || Records )
INSTANCE.get_or_init( || Records::default() )
}
}

impl Default for Records
{
fn default() -> Self
{

let cell_prefix = "".to_string();
let cell_postfix = "".to_string();
let cell_separator = " │ ".to_string();
let row_prefix = "│ ".to_string();
let row_postfix = " │".to_string();
let row_separator = "\n".to_string();
let table_prefix = "".to_string();
let table_postfix = "".to_string();
let table_separator = "\n".to_string();

// let h = '─';
// let v = '|';
// let t_l = '├';
// let t_r = '┤';
// let t_t = '┬';
// let t_b = '┴';
// let cross = '┼';
// let corner_lt = '┌';
// let corner_rt = '┐';
// let corner_lb = '└';
// let corner_rb = '┘';

Self
{
table_prefix,
table_postfix,
table_separator,
row_prefix,
row_postfix,
row_separator,
cell_prefix,
cell_postfix,
cell_separator,
// h,
// v,
// t_l,
// t_r,
// t_t,
// t_b,
// cross,
// corner_lt,
// corner_rt,
// corner_lb,
// corner_rb,
}
}
}

impl TableOutputFormat for Records
{
fn extract_write< 'buf, 'data >
(

fn extract_write< 'buf, 'data >(
& self,
x : & InputExtract< 'data >,
c : & mut Context< 'buf >,
) -> fmt::Result
{
for ( i, row ) in x.row_descriptors.iter().enumerate()

let label_width = x.header().fold( 0, | acc, cell | acc.max( cell.1[ 0 ] ) );

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

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

if !row.vis
{
continue;
}
writeln!( c.buf, "-[ RECORD {} ]", i + 1 )?;
for ( icol, col ) in x.col_descriptors.iter().enumerate()

if first
{
first = false;
}
else
{
write!( c.buf, "{}", self.table_separator )?;
}

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

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

for ( icol, _col ) in x.col_descriptors.iter().enumerate()
{
// let cell_width = x.data[ i ][ icol ].1[ 0 ];
let md_index = [ 0, icol, i ];
let slice = x.slices[ x.slices_dim.md_offset( md_index ) ];
writeln!( c.buf, "{} | {}", col.width, slice )?;
let cell = &x.data[ irow ][ icol ];
let height = cell.1[ 1 ];

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 ) ];

if icol > 0 || islice > 0
{
write!( c.buf, "{}", self.row_separator )?;
}

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

write!( c.buf, "{}", self.cell_prefix )?;
write!( c.buf, "{:<label_width$}", label )?;
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, "{}", self.cell_postfix )?;

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

}

}

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

Ok(())
}

}
Loading

0 comments on commit 41a4e7b

Please sign in to comment.