Skip to content

Commit

Permalink
Only 2 left
Browse files Browse the repository at this point in the history
  • Loading branch information
InAnYan committed Nov 14, 2024
1 parent 8ccea60 commit 7d5dd3a
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 148 deletions.
4 changes: 0 additions & 4 deletions module/core/format_tools/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) ]
Expand Down Expand Up @@ -318,7 +317,6 @@ pub mod own
table::orphan::*,
to_string::orphan::*,
to_string_with_fallback::orphan::*,
wrap_text::orphan::*,
};

}
Expand Down Expand Up @@ -371,7 +369,6 @@ pub mod exposed
table::exposed::*,
to_string::exposed::*,
to_string_with_fallback::exposed::*,
wrap_text::exposed::*,
};

}
Expand All @@ -394,7 +391,6 @@ pub mod prelude
table::prelude::*,
to_string::prelude::*,
to_string_with_fallback::prelude::*,
wrap_text::prelude::*,
};

}
77 changes: 47 additions & 30 deletions module/core/format_tools/src/format/output_format/records.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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, "{:<key_width$}", key )?;
write!( c.buf, "{}", self.cell_postfix )?;
write!( c.buf, "{}", self.cell_separator )?;
write!( c.buf, "{}", self.cell_prefix )?;
write!( c.buf, "{:<value_width$}", row )?;
write!( c.buf, "{}", self.cell_postfix )?;

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

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

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

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

Expand All @@ -209,3 +216,13 @@ impl TableOutputFormat for Records
}

}

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()
}
64 changes: 56 additions & 8 deletions module/core/format_tools/src/format/output_format/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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() );
}
}

Expand All @@ -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
{
Expand All @@ -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, "{:<width$}", " ", width = lspaces )?;
}

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

if rspaces > 0
{
Expand All @@ -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
}
106 changes: 0 additions & 106 deletions module/core/format_tools/src/format/wrap_text.rs

This file was deleted.

0 comments on commit 7d5dd3a

Please sign in to comment.