diff --git a/module/core/format_tools/src/format.rs b/module/core/format_tools/src/format.rs index 03321941f7..fb207181b3 100644 --- a/module/core/format_tools/src/format.rs +++ b/module/core/format_tools/src/format.rs @@ -289,7 +289,6 @@ pub mod string; pub mod table; pub mod to_string; pub mod to_string_with_fallback; -pub mod wrap_text; /// A strucutre for diagnostic and demonstration purpose. #[ doc( hidden ) ] @@ -318,7 +317,6 @@ pub mod own table::orphan::*, to_string::orphan::*, to_string_with_fallback::orphan::*, - wrap_text::orphan::*, }; } @@ -371,7 +369,6 @@ pub mod exposed table::exposed::*, to_string::exposed::*, to_string_with_fallback::exposed::*, - wrap_text::exposed::*, }; } @@ -394,7 +391,6 @@ pub mod prelude table::prelude::*, to_string::prelude::*, to_string_with_fallback::prelude::*, - wrap_text::prelude::*, }; } diff --git a/module/core/format_tools/src/format/output_format/records.rs b/module/core/format_tools/src/format/output_format/records.rs index 4ecc41ade4..78d32b2938 100644 --- a/module/core/format_tools/src/format/output_format/records.rs +++ b/module/core/format_tools/src/format/output_format/records.rs @@ -34,8 +34,6 @@ use core:: }; use std::sync::OnceLock; -use format::wrap_text::wrap_text; - /// A struct representing the list of records( rows ) output format. /// /// `Records` provides an implementation for table formatting that outputs @@ -158,48 +156,57 @@ impl TableOutputFormat for Records ) -> fmt::Result { - let col_names : Vec< Cow< 'data, str > > = x.header().collect(); + 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; - - for ( irow, row ) in x.rows().enumerate() + for ( ientry, entry ) in x.rows().enumerate() { - if first - { - first = false; - } - else + if ientry > 0 { write!( c.buf, "{}", self.table_separator )?; } - writeln!( c.buf, " = {}", irow + 1 )?; + writeln!( c.buf, " = {}", ientry + 1 )?; - let value_width = row.iter().fold( 0, | acc, cell | acc.max( cell.len() ) ); + let row = wrap_text( entry, 0 ); - for ( icol, col ) in row.iter().enumerate() - { - let key = col_names.get(icol).map( Cow::borrow ).unwrap_or( "" ); + let value_width = row.iter().map( |sr| sr.iter().map( |c| c.chars().count() ).max().unwrap_or(0) ).max().unwrap_or(0); - if icol > 0 + let mut row_count = 0; + + for ( ifield, field ) in row.iter().enumerate() + { + for ( irow, row ) in field.iter().enumerate() { - write!( c.buf, "{}", self.row_separator )?; + 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, "{: +( + data: &'data Vec< Cow< 'data, str > >, + _limit: usize +) +-> Vec< Vec< &'data str > > +{ + data.iter().map( |c| string::lines( c ).collect() ).collect() +} \ No newline at end of file diff --git a/module/core/format_tools/src/format/output_format/table.rs b/module/core/format_tools/src/format/output_format/table.rs index 20fa2cc7b9..00eedd790f 100644 --- a/module/core/format_tools/src/format/output_format/table.rs +++ b/module/core/format_tools/src/format/output_format/table.rs @@ -16,14 +16,13 @@ use print:: InputExtract, Context, }; +use std::borrow::Cow; use core:: { fmt, }; use std::sync::OnceLock; -use format::wrap_text::wrap_text; - /// A struct representing the classic table output format. /// /// `Table` provides a standard implementation for table formatting, @@ -188,7 +187,7 @@ impl TableOutputFormat for Table { for ( icol, col ) in row.iter().enumerate() { - col_width[ icol ] = col_width[ icol ].max( col.chars().count() ); + col_width[ icol ] = col_width[ icol ].max( col.content.chars().count() ); } } @@ -215,8 +214,9 @@ impl TableOutputFormat for Table for ( icol, col ) in row.iter().enumerate() { - let cell_width = col_width[ icol ]; - let width = col.len(); + let cell_width = col.wrap_width; + let col_width = col_width[ icol ]; + let slice_width = col.content.chars().count(); if icol > 0 { @@ -225,15 +225,15 @@ impl TableOutputFormat for Table write!( c.buf, "{}", cell_prefix )?; - let lspaces = ( cell_width - width ) / 2; - let rspaces = ( ( cell_width - width ) as f32 / 2 as f32 ).round() as usize; + let lspaces = ( col_width - cell_width ) / 2; + let rspaces = ( ( col_width - cell_width ) as f32 / 2 as f32 ).round() as usize + cell_width - slice_width; if lspaces > 0 { write!( c.buf, "{: 0 { @@ -249,3 +249,51 @@ impl TableOutputFormat for Table Ok(()) } } + +struct WrappedCell< 'data > +{ + wrap_width : usize, + content : Cow< 'data, str > +} + +fn wrap_text< 'data > +( + data: &'data Vec< Vec< Cow< 'data, str > > >, + _limit: usize +) -> Vec< Vec< WrappedCell< 'data > > > +{ + let mut new_data = Vec::new(); + + for 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(); + + let max_rows = unwrapped_text.iter().map( Vec::len ).max().unwrap_or(0); + + let mut transposed : Vec< Vec< WrappedCell< 'data > > > = Vec::new(); + + for i in 0..max_rows + { + let mut row_vec : Vec< WrappedCell< 'data > > = Vec::new(); + + for col_lines in &unwrapped_text + { + if col_lines.len() > i + { + let wrap_width = col_lines.iter().map( |c| c.len() ).max().unwrap_or(0); + row_vec.push( WrappedCell { wrap_width , content : col_lines[ i ].clone() } ); + } + else + { + row_vec.push( WrappedCell { wrap_width : 0, content : Cow::from( "" ) } ); + } + } + + transposed.push( row_vec ); + } + + new_data.extend( transposed ); + } + + new_data +} \ No newline at end of file diff --git a/module/core/format_tools/src/format/wrap_text.rs b/module/core/format_tools/src/format/wrap_text.rs deleted file mode 100644 index 7f43226915..0000000000 --- a/module/core/format_tools/src/format/wrap_text.rs +++ /dev/null @@ -1,106 +0,0 @@ -mod private -{ - - use std::borrow::{ Cow }; - - use crate::*; - - pub fn wrap_text< 'data > - ( - data: &'data Vec< Vec< Cow< 'data, str > > >, - _limit: usize - ) -> Vec< Vec< Cow< 'data, str > > > - { - let mut new_data = Vec::new(); - - for 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(); - - let max_rows = unwrapped_text.iter().map( Vec::len ).max().unwrap_or(0); - - let mut transposed : Vec< Vec< Cow< 'data, str > > > = Vec::new(); - - for i in 0..max_rows - { - let mut row_vec : Vec< Cow< 'data, str > > = Vec::new(); - - for col_lines in &unwrapped_text - { - if col_lines.len() > i - { - row_vec.push( col_lines[ i ].clone() ); - } - else - { - row_vec.push( Cow::from( "" ) ); - } - } - - transposed.push( row_vec ); - } - - new_data.extend( transposed ); - } - - new_data - } -} - -#[ doc( inline ) ] -#[ allow( unused_imports ) ] -pub use own::*; - -/// Own namespace of the module. -#[ allow( unused_imports ) ] -pub mod own -{ - use super::*; - - #[ doc( inline ) ] - pub use orphan::*; - #[ doc( inline ) ] - pub use private:: - { - wrap_text, - }; -} - -/// Orphan namespace of the module. -#[ allow( unused_imports ) ] -pub mod orphan -{ - use super::*; - pub use super::super::to_string_with_fallback; - - #[ doc( inline ) ] - pub use exposed::*; - - #[ doc( inline ) ] - pub use private:: - { - }; - -} - -/// Exposed namespace of the module. -#[ allow( unused_imports ) ] -pub mod exposed -{ - use super::*; - #[ doc( inline ) ] - pub use prelude::*; - - #[ doc( inline ) ] - pub use private:: - { - }; - -} - -/// Prelude to use essentials: `use my_module::prelude::*`. -#[ allow( unused_imports ) ] -pub mod prelude -{ - use super::*; -} \ No newline at end of file