Skip to content

Commit

Permalink
Merge pull request #1249 from SRetip/table_facade
Browse files Browse the repository at this point in the history
RADY : add facade for `preatytable`
  • Loading branch information
Wandalen authored Mar 25, 2024
2 parents f4011b4 + 82cdf6d commit 32774ba
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 55 deletions.
4 changes: 4 additions & 0 deletions module/move/willbe/src/entity/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ crate::mod_interface!
/// Handles operations related to packed Rust crates
layer packed_crate;
orphan use super::packed_crate;

/// Facade for `preatytable` crate.
layer table;
orphan use super::table;

/// Provides a set of functionalities for handling and manipulating packages.
layer packages;
Expand Down
109 changes: 109 additions & 0 deletions module/move/willbe/src/entity/table.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
mod private
{
use std::fmt::{Display, Formatter};

/// An owned printable table.
#[ derive( Debug ) ]
pub struct Table
{
inner : prettytable::Table,
}

impl Display for Table
{
fn fmt( &self, f : &mut Formatter< '_ > ) -> std::fmt::Result
{
writeln!( f, "{}", self.inner.to_string() )
}
}

impl Table
{
/// Create an empty table.
pub fn new() -> Self
{
Self
{
inner : prettytable::Table::new(),
}
}
}

impl Table
{
/// Set the optional header.
pub fn set_header(&mut self, row : Row )
{
self.inner.set_titles( row.inner );
}

/// Append a row in the table.
pub fn add_row(&mut self, row : Row )
{
self.inner.add_row( row.inner );
}
}

impl Default for Table
{
fn default() -> Self
{
let mut table = Self::new();
let format = default_format();
table.inner.set_format( format );
table
}
}

fn default_format() -> prettytable::format::TableFormat
{
let format = prettytable::format::FormatBuilder::new()
.column_separator( ' ' )
.borders( ' ' )
.separators
(
&[ prettytable::format::LinePosition::Title ],
prettytable::format::LineSeparator::new( '-', '+', '+', '+' )
)
.padding( 1, 1 )
.build();
format
}

/// Represent a table row made of cells.
#[ derive( Debug ) ]
pub struct Row
{
inner : prettytable::Row,
}

impl Row
{

/// Append a cell at the end of the row.
pub fn add_cell( &mut self, title : &str )
{
let mut cell = prettytable::Cell::new( title );
cell.align( prettytable::format::Alignment::CENTER );
self.inner.add_cell( prettytable::Cell::new( title ) );
}
}

impl Row
{
/// Create an row of length size, with empty strings stored.
pub fn new() -> Self
{
Self
{
inner : prettytable::Row::empty(),
}
}
}
}

crate::mod_interface!
{
protected use Table;
protected use Row;
}
81 changes: 26 additions & 55 deletions module/move/willbe/src/entity/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod private
// qqq : for Petro : use https://github.com/console-rs/indicatif

use crate::*;
use table::*;
use std::
{
collections::{ BTreeMap, BTreeSet, HashSet },
Expand All @@ -20,12 +21,6 @@ mod private
// aaa : ✅
use colored::Colorize;
// qqq : for Petro : don't do micro imports
use prettytable::
{
Cell,
Row,
Table,
};
// qqq : for Petro : don't do micro imports
#[ cfg( feature = "progress_bar" ) ]
use indicatif::
Expand All @@ -34,12 +29,6 @@ mod private
ProgressBar,
ProgressStyle
};
use prettytable::format::
{
Alignment,
FormatBuilder,
TableFormat
};
use rayon::ThreadPoolBuilder;
use process_tools::process::*;
use wtools::error::anyhow::{ Error, format_err };
Expand Down Expand Up @@ -181,26 +170,26 @@ mod private
ff.push( feature );
}
}
let mut table = Table::new();
let format = format();
table.set_format( format );
let mut table = Table::default();
// let format = format();
// table.set_format( format );

let mut header_row = Row::empty();
header_row.add_cell( Cell::new( "Channel" ) );
header_row.add_cell( Cell::new( "Opt" ) );
let mut header_row = Row::new();
header_row.add_cell( "Channel" );
header_row.add_cell( "Opt" );

for feature in &ff
{
header_row.add_cell( Cell::new( feature ) );
header_row.add_cell( feature );
}
table.set_titles( header_row );
table.set_header( header_row );

for variant in &self.test_variants
{
let mut row = Row::empty();
let mut row = Row::new();

row.add_cell( Cell::new( &variant.channel.to_string() ) );
row.add_cell( Cell::new( &variant.optimization.to_string() ) );
row.add_cell( &variant.channel.to_string() );
row.add_cell( &variant.optimization.to_string() );
let counter = 0;
let flag = true;
generate_features_cells(&mut ff, variant, &mut row, counter, flag, &self.enabled_features );
Expand Down Expand Up @@ -288,8 +277,7 @@ mod private
{
for feature in ff
{
let mut c = Cell::new("+");
c.align( Alignment::CENTER );
let mut c = "+";
if variant.features.is_empty() && counter == enabled_features.len() && flag
{
flag = false;
Expand All @@ -301,28 +289,13 @@ mod private
}
else
{
c = Cell::new( "" );
c = "";
row.add_cell( c );
}
counter += 1;
}
}

fn format() -> TableFormat
{
let format = FormatBuilder::new()
.column_separator( ' ' )
.borders( ' ' )
.separators
(
&[ prettytable::format::LinePosition::Title ],
prettytable::format::LineSeparator::new( '-', '+', '+', '+' )
)
.padding( 1, 1 )
.build();
format
}


#[ derive( Debug, Former ) ]
pub struct PackageTestOptions< 'a >
{
Expand Down Expand Up @@ -566,18 +539,16 @@ mod private
ff.push( feature );
}
}
let mut table = Table::new();
let format = format();
table.set_format( format );
let mut header_row = Row::empty();
header_row.add_cell( Cell::new( "Result" ) );
header_row.add_cell( Cell::new( "Channel" ) );
header_row.add_cell( Cell::new( "Opt" ) );
let mut table = Table::default();
let mut header_row = Row::new();
header_row.add_cell( "Result" );
header_row.add_cell( "Channel" );
header_row.add_cell( "Opt" );
for feature in &ff
{
header_row.add_cell( Cell::new( feature ) );
header_row.add_cell( feature );
}
table.set_titles( header_row );
table.set_header( header_row );

writeln!( f, "{} {}\n", "\n=== Module".bold(), self.package_name.0.bold() )?;
if self.tests.is_empty()
Expand All @@ -587,7 +558,7 @@ mod private
}
for ( variant, result) in &self.tests
{
let mut row = Row::empty();
let mut row = Row::new();
let result_text = match result
{
Ok( _ ) =>
Expand All @@ -604,9 +575,9 @@ mod private
"❌"
},
};
row.add_cell( Cell::new( result_text ) );
row.add_cell( Cell::new( &variant.channel.to_string() ) );
row.add_cell( Cell::new( &variant.optimization.to_string() ) );
row.add_cell( result_text );
row.add_cell( &variant.channel.to_string() );
row.add_cell( &variant.optimization.to_string() );
let counter = 0;
let flag = true;
generate_features_cells( &mut ff, variant, &mut row, counter, flag, &self.enabled_features );
Expand Down

0 comments on commit 32774ba

Please sign in to comment.