Skip to content

Commit

Permalink
tabled/ Add settings::Dup
Browse files Browse the repository at this point in the history
Signed-off-by: Maxim Zhiburt <zhiburt@gmail.com>
  • Loading branch information
zhiburt committed Jul 3, 2023
1 parent 59862a3 commit 74b3a63
Show file tree
Hide file tree
Showing 9 changed files with 382 additions and 30 deletions.
5 changes: 3 additions & 2 deletions tabled/src/settings/alignment/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ use crate::grid::config::{ColoredConfig, Entity};
/// ];
///
/// let mut table = Table::new(&data);
/// table.with(Style::modern())
/// .with(
/// table
/// .with(Style::modern())
/// .with(
/// Modify::new(Segment::all())
/// .with(Alignment::right())
/// .with(Alignment::center())
Expand Down
150 changes: 150 additions & 0 deletions tabled/src/settings/duplicate/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
//! This module contains an [`Dup`] setting the [`Table`].
//!
//! # Example
//!
//! ```
//! # use tabled::{Table, settings::{Dup, object::{Columns, Rows}}};
//! # let data: Vec<&'static str> = Vec::new();
//! let mut table = Table::new(&data);
//! table.with(Dup::new(Rows::first(), Columns::first()));
//! ```
//!
//! [`Table`]: crate::Table

use papergrid::config::Position;

use crate::{
grid::records::{ExactRecords, PeekableRecords, Records, RecordsMut},
settings::{object::Object, TableOption},
};

/// [`Dup`] duplicates a given set of cells into another set of ones [`Table`].
///
/// # Example
///
/// ```
/// use tabled::{Table, settings::{object::Rows, Dup}};
///
/// let data = [
/// ["1", "2", "3"],
/// ["Some\nMulti\nLine\nText", "and a line", "here"],
/// ["4", "5", "6"],
/// ];
///
/// let mut table = Table::new(&data);
/// table.with(Dup::new(Rows::single(1), Rows::single(2)));
///
/// assert_eq!(
/// table.to_string(),
/// "+-------+------------+------+\n\
/// | 0 | 1 | 2 |\n\
/// +-------+------------+------+\n\
/// | Some | and a line | here |\n\
/// | Multi | | |\n\
/// | Line | | |\n\
/// | Text | | |\n\
/// +-------+------------+------+\n\
/// | Some | and a line | here |\n\
/// | Multi | | |\n\
/// | Line | | |\n\
/// | Text | | |\n\
/// +-------+------------+------+\n\
/// | 4 | 5 | 6 |\n\
/// +-------+------------+------+",
/// )
/// ```
///
/// [`Table`]: crate::Table
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct Dup<Dst, Src> {
src: Src,
dst: Dst,
}

impl<Dst, Src> Dup<Dst, Src> {
/// New creates a new [`Dup`] modifier.
///
/// # Example
///
/// ```
/// # use tabled::{Table, settings::{Dup, object::{Columns, Rows}}};
/// # let data: Vec<&'static str> = Vec::new();
/// let mut table = Table::new(&data);
/// table.with(Dup::new(Rows::first(), Columns::last()));
/// ```
pub fn new(dst: Dst, src: Src) -> Self {
Self { src, dst }
}
}

impl<Dst, Src, R, D, C> TableOption<R, D, C> for Dup<Dst, Src>
where
Dst: Object<R>,
Src: Object<R>,
R: Records + ExactRecords + PeekableRecords + RecordsMut<String>,
{
fn change(self, records: &mut R, _: &mut C, _: &mut D) {
let input = collect_input(records, self.src);
set_cells(records, &input, self.dst);
}
}

fn collect_input<R, O>(records: &mut R, src: O) -> Vec<String>
where
O: Object<R>,
R: Records + ExactRecords + PeekableRecords + RecordsMut<String>,
{
let count_rows = records.count_rows();
let count_columns = records.count_columns();

let mut input = Vec::new();
for entity in src.cells(records) {
for pos in entity.iter(count_rows, count_columns) {
if !is_valid_cell(pos, count_rows, count_columns) {
continue;
}

let text = records.get_text(pos).to_owned();
input.push(text);
}
}

input
}

fn set_cells<R, O>(records: &mut R, src: &[String], dst: O)
where
O: Object<R>,
R: Records + ExactRecords + PeekableRecords + RecordsMut<String>,
{
if src.is_empty() {
return;
}

let count_rows = records.count_rows();
let count_columns = records.count_columns();

for entity in dst.cells(records) {
let mut source = src.iter().cycle();
for pos in entity.iter(count_rows, count_columns) {
if !is_valid_cell(pos, count_rows, count_columns) {
continue;
}

let text = source.next().unwrap().clone();
records.set(pos, text);
}
}
}

fn is_valid_cell((row, col): Position, count_rows: usize, count_columns: usize) -> bool {
if row > count_rows {
return false;
}

if col > count_columns {
return false;
}

true
}
4 changes: 3 additions & 1 deletion tabled/src/settings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ mod rotate;
mod color;
#[cfg(feature = "std")]
mod concat;
#[cfg(feature = "std")]
mod duplicate;

pub mod style;

Expand Down Expand Up @@ -127,7 +129,7 @@ pub use self::{
#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
pub use self::{
color::Color, concat::Concat, disable::Disable, format::Format, height::Height,
color::Color, concat::Concat, disable::Disable, duplicate::Dup, format::Format, height::Height,
highlight::Highlight, merge::Merge, panel::Panel, shadow::Shadow, span::Span, style::Border,
width::Width,
};
16 changes: 8 additions & 8 deletions tabled/tests/core/compact_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ test_table!(
test_table!(
compact_with_dimension,
{
let data = Matrix::empty(3, 3).to_vec();
let data = Matrix::with_no_frame(3, 3).to_vec();
let mut dims = CompactGridDimension::default();
dims.estimate(IterRecords::new(&data, 3, None), &CompactConfig::default());
CompactTable::with_dimension(data, dims).columns(3).to_string()
Expand All @@ -36,7 +36,7 @@ test_table!(

test_table!(
compact_width,
CompactTable::new(Matrix::empty(3, 3).to_vec().to_vec()).columns(3).width(5).to_string(),
CompactTable::new(Matrix::with_no_frame(3, 3).to_vec().to_vec()).columns(3).width(5).to_string(),
"+-----+-----+-----+"
"| 0-0 | 0-1 | 0-2 |"
"|-----+-----+-----|"
Expand All @@ -48,7 +48,7 @@ test_table!(

test_table!(
compact_width_pad_not_included,
CompactTable::new(Matrix::empty(3, 3).to_vec()).columns(3).width(3).to_string(),
CompactTable::new(Matrix::with_no_frame(3, 3).to_vec()).columns(3).width(3).to_string(),
"+---+---+---+"
"| 0-0 | 0-1 | 0-2 |"
"|---+---+---|"
Expand All @@ -60,7 +60,7 @@ test_table!(

test_table!(
compact_width_bigger,
CompactTable::new(Matrix::empty(3, 3).to_vec()).columns(3).width(10).to_string(),
CompactTable::new(Matrix::with_no_frame(3, 3).to_vec()).columns(3).width(10).to_string(),
"+----------+----------+----------+"
"| 0-0 | 0-1 | 0-2 |"
"|----------+----------+----------|"
Expand All @@ -72,7 +72,7 @@ test_table!(

test_table!(
compact_columns,
CompactTable::new(Matrix::empty(3, 3).to_vec()).columns(3).to_string(),
CompactTable::new(Matrix::with_no_frame(3, 3).to_vec()).columns(3).to_string(),
"+--+--+--+"
"| 0-0 | 0-1 | 0-2 |"
"|--+--+--|"
Expand All @@ -84,15 +84,15 @@ test_table!(

test_table!(
compact_cols_zero,
CompactTable::new(Matrix::empty(3, 3).to_vec())
CompactTable::new(Matrix::with_no_frame(3, 3).to_vec())
.columns(0)
.to_string(),
""
);

test_table!(
compact_cols_less,
CompactTable::new(Matrix::empty(3, 3).to_vec())
CompactTable::new(Matrix::with_no_frame(3, 3).to_vec())
.columns(1)
.to_string(),
"+--+"
Expand All @@ -106,7 +106,7 @@ test_table!(

test_table!(
compact_cols_more,
CompactTable::new(Matrix::empty(3, 3).to_vec())
CompactTable::new(Matrix::with_no_frame(3, 3).to_vec())
.columns(5)
.to_string(),
"+--+--+--+--+--+"
Expand Down
14 changes: 7 additions & 7 deletions tabled/tests/core/iter_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use testing_table::test_table;

test_table!(
iter_table,
IterTable::new(Matrix::empty(3, 3).to_vec()),
IterTable::new(Matrix::with_no_frame(3, 3).to_vec()),
"+-----+-----+-----+"
"| 0-0 | 0-1 | 0-2 |"
"+-----+-----+-----+"
Expand All @@ -19,7 +19,7 @@ test_table!(

test_table!(
iter_table_cols,
IterTable::new(Matrix::empty(3, 3).to_vec()).columns(3),
IterTable::new(Matrix::with_no_frame(3, 3).to_vec()).columns(3),
"+-----+-----+-----+"
"| 0-0 | 0-1 | 0-2 |"
"+-----+-----+-----+"
Expand All @@ -31,7 +31,7 @@ test_table!(

test_table!(
iter_table_cols_less,
IterTable::new(Matrix::empty(3, 3).to_vec()).columns(2),
IterTable::new(Matrix::with_no_frame(3, 3).to_vec()).columns(2),
"+-----+-----+"
"| 0-0 | 0-1 |"
"+-----+-----+"
Expand All @@ -43,7 +43,7 @@ test_table!(

test_table!(
iter_table_cols_zero,
IterTable::new(Matrix::empty(3, 3).to_vec()).columns(0),
IterTable::new(Matrix::with_no_frame(3, 3).to_vec()).columns(0),
""
);

Expand All @@ -65,7 +65,7 @@ test_table!(

test_table!(
iter_table_width,
IterTable::new(Matrix::empty(3, 3).to_vec()).width(2),
IterTable::new(Matrix::with_no_frame(3, 3).to_vec()).width(2),
"+----+----+----+"
"| 0- | 0- | 0- |"
"+----+----+----+"
Expand All @@ -77,7 +77,7 @@ test_table!(

test_table!(
iter_table_height_does_not_work,
IterTable::new(Matrix::empty(3, 3).to_vec()).height(5),
IterTable::new(Matrix::with_no_frame(3, 3).to_vec()).height(5),
"+-----+-----+-----+"
"| 0-0 | 0-1 | 0-2 |"
"| | | |"
Expand All @@ -101,7 +101,7 @@ test_table!(

test_table!(
iter_table_sniff_0,
IterTable::new(Matrix::empty(3, 3).to_vec()).sniff(0),
IterTable::new(Matrix::with_no_frame(3, 3).to_vec()).sniff(0),
""
);

Expand Down
14 changes: 7 additions & 7 deletions tabled/tests/core/pool_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use tabled::grid::color::StaticColor;

test_table!(
pool_table,
PoolTable::new(Matrix::empty(3, 3).to_vec()),
PoolTable::new(Matrix::with_no_frame(3, 3).to_vec()),
"+-----+-----+-----+"
"| 0-0 | 0-1 | 0-2 |"
"+-----+-----+-----+"
Expand Down Expand Up @@ -278,7 +278,7 @@ test_table!(

test_table!(
pool_table_padding,
PoolTable::new(Matrix::empty(3, 3).to_vec()).with(Padding::new(1, 2, 3, 4)),
PoolTable::new(Matrix::with_no_frame(3, 3).to_vec()).with(Padding::new(1, 2, 3, 4)),
"+------+------+------+"
"| | | |"
"| | | |"
Expand Down Expand Up @@ -312,7 +312,7 @@ test_table!(
#[cfg(feature = "color")]
test_table!(
pool_table_padding_2,
PoolTable::new(Matrix::empty(3, 3).to_vec())
PoolTable::new(Matrix::with_no_frame(3, 3).to_vec())
.with(Padding::new(1, 2, 3, 4)
.fill('!', '@', '#', '$')
.colorize(
Expand Down Expand Up @@ -354,7 +354,7 @@ test_table!(

test_table!(
pool_table_margin,
PoolTable::new(Matrix::empty(3, 3).to_vec()).with(Margin::new(1, 2, 3, 4).fill('!', '@', '#', '$')),
PoolTable::new(Matrix::with_no_frame(3, 3).to_vec()).with(Margin::new(1, 2, 3, 4).fill('!', '@', '#', '$')),
"!###################@@"
"!###################@@"
"!###################@@"
Expand Down Expand Up @@ -513,23 +513,23 @@ test_table!(

test_table!(
pool_table_style_empty,
PoolTable::new(Matrix::empty(3, 3).to_vec()).with(Style::empty()),
PoolTable::new(Matrix::with_no_frame(3, 3).to_vec()).with(Style::empty()),
" 0-0 0-1 0-2 "
" 1-0 1-1 1-2 "
" 2-0 2-1 2-2 "
);

test_table!(
pool_table_style_markdown,
PoolTable::new(Matrix::empty(3, 3).to_vec()).with(Style::markdown()),
PoolTable::new(Matrix::with_no_frame(3, 3).to_vec()).with(Style::markdown()),
"| 0-0 | 0-1 | 0-2 |"
"| 1-0 | 1-1 | 1-2 |"
"| 2-0 | 2-1 | 2-2 |"
);

test_table!(
pool_table_style_rounded,
PoolTable::new(Matrix::empty(3, 3).to_vec()).with(Style::rounded()),
PoolTable::new(Matrix::with_no_frame(3, 3).to_vec()).with(Style::rounded()),
"╭─────┬─────┬─────╮"
"│ 0-0 │ 0-1 │ 0-2 │"
" ───── ───── ───── "
Expand Down
Loading

0 comments on commit 74b3a63

Please sign in to comment.