From a594af6b52289ebf9622fb90f031a236d5a1b5cc Mon Sep 17 00:00:00 2001 From: Maxim Zhiburt Date: Sat, 20 Nov 2021 14:46:57 +0300 Subject: [PATCH 1/7] tabled/ Move test from rotate.rs to tests/rotate_test.rs --- src/rotate.rs | 211 ---------------------------------------- tests/rotate_test.rs | 223 +++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 217 insertions(+), 217 deletions(-) diff --git a/src/rotate.rs b/src/rotate.rs index d0341ce3..e542b44e 100644 --- a/src/rotate.rs +++ b/src/rotate.rs @@ -66,214 +66,3 @@ impl TableOption for Rotate { } } } - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn test_rotate() { - let table = || Table::new([(123, 456, 789), (234, 567, 891)]); - - assert_eq!( - table() - .with(Rotate::Left) - .with(Rotate::Left) - .with(Rotate::Left) - .with(Rotate::Left) - .to_string(), - table().to_string() - ); - assert_eq!( - table() - .with(Rotate::Right) - .with(Rotate::Right) - .with(Rotate::Right) - .with(Rotate::Right) - .to_string(), - table().to_string() - ); - assert_eq!( - table().with(Rotate::Right).with(Rotate::Left).to_string(), - table().to_string() - ); - assert_eq!( - table().with(Rotate::Left).with(Rotate::Right).to_string(), - table().to_string() - ); - assert_eq!( - table().with(Rotate::Bottom).with(Rotate::Top).to_string(), - table().to_string() - ); - assert_eq!( - table() - .with(Rotate::Bottom) - .with(Rotate::Bottom) - .to_string(), - table().to_string() - ); - assert_eq!( - table().with(Rotate::Top).with(Rotate::Top).to_string(), - table().to_string() - ); - } - - #[test] - fn test_3x3_box() { - let table = Table::new([(123, 456, 789), (234, 567, 891)]); - assert_eq!( - table.to_string(), - "+-----+-----+-----+\n\ - | i32 | i32 | i32 |\n\ - +-----+-----+-----+\n\ - | 123 | 456 | 789 |\n\ - +-----+-----+-----+\n\ - | 234 | 567 | 891 |\n\ - +-----+-----+-----+\n" - ); - - let table = table.with(Rotate::Left); - assert_eq!( - table.to_string(), - "+-----+-----+-----+\n\ - | i32 | 789 | 891 |\n\ - +-----+-----+-----+\n\ - | i32 | 456 | 567 |\n\ - +-----+-----+-----+\n\ - | i32 | 123 | 234 |\n\ - +-----+-----+-----+\n" - ); - - let table = table.with(Rotate::Right).with(Rotate::Right); - assert_eq!( - table.to_string(), - "+-----+-----+-----+\n\ - | 234 | 123 | i32 |\n\ - +-----+-----+-----+\n\ - | 567 | 456 | i32 |\n\ - +-----+-----+-----+\n\ - | 891 | 789 | i32 |\n\ - +-----+-----+-----+\n" - ); - } - - #[test] - fn test_left_rotate() { - let table = Table::new([(123, 456, 789), (234, 567, 891), (111, 222, 333)]); - assert_eq!( - table.to_string(), - "+-----+-----+-----+\n\ - | i32 | i32 | i32 |\n\ - +-----+-----+-----+\n\ - | 123 | 456 | 789 |\n\ - +-----+-----+-----+\n\ - | 234 | 567 | 891 |\n\ - +-----+-----+-----+\n\ - | 111 | 222 | 333 |\n\ - +-----+-----+-----+\n" - ); - - let table = table.with(Rotate::Left); - assert_eq!( - table.to_string(), - "+-----+-----+-----+-----+\n\ - | i32 | 789 | 891 | 333 |\n\ - +-----+-----+-----+-----+\n\ - | i32 | 456 | 567 | 222 |\n\ - +-----+-----+-----+-----+\n\ - | i32 | 123 | 234 | 111 |\n\ - +-----+-----+-----+-----+\n" - ); - } - - #[test] - fn test_right_rotate() { - let table = Table::new([(123, 456, 789), (234, 567, 891), (111, 222, 333)]); - assert_eq!( - table.to_string(), - "+-----+-----+-----+\n\ - | i32 | i32 | i32 |\n\ - +-----+-----+-----+\n\ - | 123 | 456 | 789 |\n\ - +-----+-----+-----+\n\ - | 234 | 567 | 891 |\n\ - +-----+-----+-----+\n\ - | 111 | 222 | 333 |\n\ - +-----+-----+-----+\n" - ); - - let table = table.with(Rotate::Right); - assert_eq!( - table.to_string(), - "+-----+-----+-----+-----+\n\ - | 111 | 234 | 123 | i32 |\n\ - +-----+-----+-----+-----+\n\ - | 222 | 567 | 456 | i32 |\n\ - +-----+-----+-----+-----+\n\ - | 333 | 891 | 789 | i32 |\n\ - +-----+-----+-----+-----+\n" - ); - } - - #[test] - fn test_bottom_rotate() { - let table = Table::new([(123, 456, 789), (234, 567, 891), (111, 222, 333)]); - assert_eq!( - table.to_string(), - "+-----+-----+-----+\n\ - | i32 | i32 | i32 |\n\ - +-----+-----+-----+\n\ - | 123 | 456 | 789 |\n\ - +-----+-----+-----+\n\ - | 234 | 567 | 891 |\n\ - +-----+-----+-----+\n\ - | 111 | 222 | 333 |\n\ - +-----+-----+-----+\n" - ); - - let table = table.with(Rotate::Bottom); - assert_eq!( - table.to_string(), - "+-----+-----+-----+\n\ - | 111 | 222 | 333 |\n\ - +-----+-----+-----+\n\ - | 234 | 567 | 891 |\n\ - +-----+-----+-----+\n\ - | 123 | 456 | 789 |\n\ - +-----+-----+-----+\n\ - | i32 | i32 | i32 |\n\ - +-----+-----+-----+\n" - ); - } - - #[test] - fn test_top_rotate() { - let table = Table::new([(123, 456, 789), (234, 567, 891), (111, 222, 333)]); - assert_eq!( - table.to_string(), - "+-----+-----+-----+\n\ - | i32 | i32 | i32 |\n\ - +-----+-----+-----+\n\ - | 123 | 456 | 789 |\n\ - +-----+-----+-----+\n\ - | 234 | 567 | 891 |\n\ - +-----+-----+-----+\n\ - | 111 | 222 | 333 |\n\ - +-----+-----+-----+\n" - ); - - let table = table.with(Rotate::Top); - assert_eq!( - table.to_string(), - "+-----+-----+-----+\n\ - | 111 | 222 | 333 |\n\ - +-----+-----+-----+\n\ - | 234 | 567 | 891 |\n\ - +-----+-----+-----+\n\ - | 123 | 456 | 789 |\n\ - +-----+-----+-----+\n\ - | i32 | i32 | i32 |\n\ - +-----+-----+-----+\n" - ); - } -} diff --git a/tests/rotate_test.rs b/tests/rotate_test.rs index 6399b198..a30b4f3c 100644 --- a/tests/rotate_test.rs +++ b/tests/rotate_test.rs @@ -2,15 +2,226 @@ // add MARGIN && PADDING instead of indent? use tabled::{Full, Indent, Modify, Rotate, Style, Table, Tabled}; -#[derive(Tabled)] -struct Linux { - id: u8, - destribution: &'static str, - link: &'static str, +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_rotate() { + let table = || Table::new([(123, 456, 789), (234, 567, 891)]); + + assert_eq!( + table() + .with(Rotate::Left) + .with(Rotate::Left) + .with(Rotate::Left) + .with(Rotate::Left) + .to_string(), + table().to_string() + ); + assert_eq!( + table() + .with(Rotate::Right) + .with(Rotate::Right) + .with(Rotate::Right) + .with(Rotate::Right) + .to_string(), + table().to_string() + ); + assert_eq!( + table().with(Rotate::Right).with(Rotate::Left).to_string(), + table().to_string() + ); + assert_eq!( + table().with(Rotate::Left).with(Rotate::Right).to_string(), + table().to_string() + ); + assert_eq!( + table().with(Rotate::Bottom).with(Rotate::Top).to_string(), + table().to_string() + ); + assert_eq!( + table() + .with(Rotate::Bottom) + .with(Rotate::Bottom) + .to_string(), + table().to_string() + ); + assert_eq!( + table().with(Rotate::Top).with(Rotate::Top).to_string(), + table().to_string() + ); + } + + #[test] + fn test_3x3_box() { + let table = Table::new([(123, 456, 789), (234, 567, 891)]); + assert_eq!( + table.to_string(), + "+-----+-----+-----+\n\ + | i32 | i32 | i32 |\n\ + +-----+-----+-----+\n\ + | 123 | 456 | 789 |\n\ + +-----+-----+-----+\n\ + | 234 | 567 | 891 |\n\ + +-----+-----+-----+\n" + ); + + let table = table.with(Rotate::Left); + assert_eq!( + table.to_string(), + "+-----+-----+-----+\n\ + | i32 | 789 | 891 |\n\ + +-----+-----+-----+\n\ + | i32 | 456 | 567 |\n\ + +-----+-----+-----+\n\ + | i32 | 123 | 234 |\n\ + +-----+-----+-----+\n" + ); + + let table = table.with(Rotate::Right).with(Rotate::Right); + assert_eq!( + table.to_string(), + "+-----+-----+-----+\n\ + | 234 | 123 | i32 |\n\ + +-----+-----+-----+\n\ + | 567 | 456 | i32 |\n\ + +-----+-----+-----+\n\ + | 891 | 789 | i32 |\n\ + +-----+-----+-----+\n" + ); + } + + #[test] + fn test_left_rotate() { + let table = Table::new([(123, 456, 789), (234, 567, 891), (111, 222, 333)]); + assert_eq!( + table.to_string(), + "+-----+-----+-----+\n\ + | i32 | i32 | i32 |\n\ + +-----+-----+-----+\n\ + | 123 | 456 | 789 |\n\ + +-----+-----+-----+\n\ + | 234 | 567 | 891 |\n\ + +-----+-----+-----+\n\ + | 111 | 222 | 333 |\n\ + +-----+-----+-----+\n" + ); + + let table = table.with(Rotate::Left); + assert_eq!( + table.to_string(), + "+-----+-----+-----+-----+\n\ + | i32 | 789 | 891 | 333 |\n\ + +-----+-----+-----+-----+\n\ + | i32 | 456 | 567 | 222 |\n\ + +-----+-----+-----+-----+\n\ + | i32 | 123 | 234 | 111 |\n\ + +-----+-----+-----+-----+\n" + ); + } + + #[test] + fn test_right_rotate() { + let table = Table::new([(123, 456, 789), (234, 567, 891), (111, 222, 333)]); + assert_eq!( + table.to_string(), + "+-----+-----+-----+\n\ + | i32 | i32 | i32 |\n\ + +-----+-----+-----+\n\ + | 123 | 456 | 789 |\n\ + +-----+-----+-----+\n\ + | 234 | 567 | 891 |\n\ + +-----+-----+-----+\n\ + | 111 | 222 | 333 |\n\ + +-----+-----+-----+\n" + ); + + let table = table.with(Rotate::Right); + assert_eq!( + table.to_string(), + "+-----+-----+-----+-----+\n\ + | 111 | 234 | 123 | i32 |\n\ + +-----+-----+-----+-----+\n\ + | 222 | 567 | 456 | i32 |\n\ + +-----+-----+-----+-----+\n\ + | 333 | 891 | 789 | i32 |\n\ + +-----+-----+-----+-----+\n" + ); + } + + #[test] + fn test_bottom_rotate() { + let table = Table::new([(123, 456, 789), (234, 567, 891), (111, 222, 333)]); + assert_eq!( + table.to_string(), + "+-----+-----+-----+\n\ + | i32 | i32 | i32 |\n\ + +-----+-----+-----+\n\ + | 123 | 456 | 789 |\n\ + +-----+-----+-----+\n\ + | 234 | 567 | 891 |\n\ + +-----+-----+-----+\n\ + | 111 | 222 | 333 |\n\ + +-----+-----+-----+\n" + ); + + let table = table.with(Rotate::Bottom); + assert_eq!( + table.to_string(), + "+-----+-----+-----+\n\ + | 111 | 222 | 333 |\n\ + +-----+-----+-----+\n\ + | 234 | 567 | 891 |\n\ + +-----+-----+-----+\n\ + | 123 | 456 | 789 |\n\ + +-----+-----+-----+\n\ + | i32 | i32 | i32 |\n\ + +-----+-----+-----+\n" + ); + } + + #[test] + fn test_top_rotate() { + let table = Table::new([(123, 456, 789), (234, 567, 891), (111, 222, 333)]); + assert_eq!( + table.to_string(), + "+-----+-----+-----+\n\ + | i32 | i32 | i32 |\n\ + +-----+-----+-----+\n\ + | 123 | 456 | 789 |\n\ + +-----+-----+-----+\n\ + | 234 | 567 | 891 |\n\ + +-----+-----+-----+\n\ + | 111 | 222 | 333 |\n\ + +-----+-----+-----+\n" + ); + + let table = table.with(Rotate::Top); + assert_eq!( + table.to_string(), + "+-----+-----+-----+\n\ + | 111 | 222 | 333 |\n\ + +-----+-----+-----+\n\ + | 234 | 567 | 891 |\n\ + +-----+-----+-----+\n\ + | 123 | 456 | 789 |\n\ + +-----+-----+-----+\n\ + | i32 | i32 | i32 |\n\ + +-----+-----+-----+\n" + ); + } } #[test] -fn rotate_left() { +fn rotate_left_test() { + #[derive(Tabled)] + struct Linux { + id: u8, + destribution: &'static str, + link: &'static str, + } + let data = vec![ Linux { id: 0, From b327062c0a0460f579fe96d183eadd3b40fb11df Mon Sep 17 00:00:00 2001 From: Maxim Zhiburt Date: Sat, 20 Nov 2021 15:05:39 +0300 Subject: [PATCH 2/7] tabled/ fix tests formatting --- src/style.rs | 3 + tests/rotate_test.rs | 331 ++++++++++++++++++------------------------- 2 files changed, 137 insertions(+), 197 deletions(-) diff --git a/src/style.rs b/src/style.rs index 073881b1..8ec6e866 100644 --- a/src/style.rs +++ b/src/style.rs @@ -3,6 +3,9 @@ use crate::Table; use crate::TableOption; use papergrid::{Border, Entity, Grid, Settings}; +// todo: rename default style to ascii +// default must be a general default implementation to be able to use highlight without influening actual style. + /// Style is responsible for a look of a [Table]. /// /// # Example diff --git a/tests/rotate_test.rs b/tests/rotate_test.rs index a30b4f3c..6dbfa1b7 100644 --- a/tests/rotate_test.rs +++ b/tests/rotate_test.rs @@ -2,215 +2,152 @@ // add MARGIN && PADDING instead of indent? use tabled::{Full, Indent, Modify, Rotate, Style, Table, Tabled}; -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn test_rotate() { - let table = || Table::new([(123, 456, 789), (234, 567, 891)]); +#[test] +fn test_rotate() { + let table = || Table::new([(123, 456, 789), (234, 567, 891)]); - assert_eq!( - table() - .with(Rotate::Left) - .with(Rotate::Left) - .with(Rotate::Left) - .with(Rotate::Left) - .to_string(), - table().to_string() - ); - assert_eq!( - table() - .with(Rotate::Right) - .with(Rotate::Right) - .with(Rotate::Right) - .with(Rotate::Right) - .to_string(), - table().to_string() - ); - assert_eq!( - table().with(Rotate::Right).with(Rotate::Left).to_string(), - table().to_string() - ); - assert_eq!( - table().with(Rotate::Left).with(Rotate::Right).to_string(), - table().to_string() - ); - assert_eq!( - table().with(Rotate::Bottom).with(Rotate::Top).to_string(), - table().to_string() - ); - assert_eq!( - table() - .with(Rotate::Bottom) - .with(Rotate::Bottom) - .to_string(), - table().to_string() - ); - assert_eq!( - table().with(Rotate::Top).with(Rotate::Top).to_string(), - table().to_string() - ); - } + assert_eq!( + table() + .with(Rotate::Left) + .with(Rotate::Left) + .with(Rotate::Left) + .with(Rotate::Left) + .to_string(), + table().to_string() + ); + assert_eq!( + table() + .with(Rotate::Right) + .with(Rotate::Right) + .with(Rotate::Right) + .with(Rotate::Right) + .to_string(), + table().to_string() + ); + assert_eq!( + table().with(Rotate::Right).with(Rotate::Left).to_string(), + table().to_string() + ); + assert_eq!( + table().with(Rotate::Left).with(Rotate::Right).to_string(), + table().to_string() + ); + assert_eq!( + table().with(Rotate::Bottom).with(Rotate::Top).to_string(), + table().to_string() + ); + assert_eq!( + table() + .with(Rotate::Bottom) + .with(Rotate::Bottom) + .to_string(), + table().to_string() + ); + assert_eq!( + table().with(Rotate::Top).with(Rotate::Top).to_string(), + table().to_string() + ); +} - #[test] - fn test_3x3_box() { - let table = Table::new([(123, 456, 789), (234, 567, 891)]); - assert_eq!( - table.to_string(), - "+-----+-----+-----+\n\ - | i32 | i32 | i32 |\n\ - +-----+-----+-----+\n\ - | 123 | 456 | 789 |\n\ - +-----+-----+-----+\n\ - | 234 | 567 | 891 |\n\ - +-----+-----+-----+\n" - ); +#[test] +fn test_3x3_box() { + let table = Table::new([(123, 456, 789), (234, 567, 891)]); - let table = table.with(Rotate::Left); - assert_eq!( - table.to_string(), - "+-----+-----+-----+\n\ - | i32 | 789 | 891 |\n\ - +-----+-----+-----+\n\ - | i32 | 456 | 567 |\n\ - +-----+-----+-----+\n\ - | i32 | 123 | 234 |\n\ - +-----+-----+-----+\n" - ); + let table = table.with(Rotate::Left); + assert_eq!( + table.to_string(), + "+-----+-----+-----+\n\ + | i32 | 789 | 891 |\n\ + +-----+-----+-----+\n\ + | i32 | 456 | 567 |\n\ + +-----+-----+-----+\n\ + | i32 | 123 | 234 |\n\ + +-----+-----+-----+\n" + ); - let table = table.with(Rotate::Right).with(Rotate::Right); - assert_eq!( - table.to_string(), - "+-----+-----+-----+\n\ - | 234 | 123 | i32 |\n\ - +-----+-----+-----+\n\ - | 567 | 456 | i32 |\n\ - +-----+-----+-----+\n\ - | 891 | 789 | i32 |\n\ - +-----+-----+-----+\n" - ); - } + let table = table.with(Rotate::Right).with(Rotate::Right); + assert_eq!( + table.to_string(), + "+-----+-----+-----+\n\ + | 234 | 123 | i32 |\n\ + +-----+-----+-----+\n\ + | 567 | 456 | i32 |\n\ + +-----+-----+-----+\n\ + | 891 | 789 | i32 |\n\ + +-----+-----+-----+\n" + ); +} - #[test] - fn test_left_rotate() { - let table = Table::new([(123, 456, 789), (234, 567, 891), (111, 222, 333)]); - assert_eq!( - table.to_string(), - "+-----+-----+-----+\n\ - | i32 | i32 | i32 |\n\ - +-----+-----+-----+\n\ - | 123 | 456 | 789 |\n\ - +-----+-----+-----+\n\ - | 234 | 567 | 891 |\n\ - +-----+-----+-----+\n\ - | 111 | 222 | 333 |\n\ - +-----+-----+-----+\n" - ); +#[test] +fn test_left_rotate() { + let table = Table::new([(123, 456, 789), (234, 567, 891), (111, 222, 333)]); - let table = table.with(Rotate::Left); - assert_eq!( - table.to_string(), - "+-----+-----+-----+-----+\n\ - | i32 | 789 | 891 | 333 |\n\ - +-----+-----+-----+-----+\n\ - | i32 | 456 | 567 | 222 |\n\ - +-----+-----+-----+-----+\n\ - | i32 | 123 | 234 | 111 |\n\ - +-----+-----+-----+-----+\n" - ); - } + let table = table.with(Rotate::Left); + assert_eq!( + table.to_string(), + "+-----+-----+-----+-----+\n\ + | i32 | 789 | 891 | 333 |\n\ + +-----+-----+-----+-----+\n\ + | i32 | 456 | 567 | 222 |\n\ + +-----+-----+-----+-----+\n\ + | i32 | 123 | 234 | 111 |\n\ + +-----+-----+-----+-----+\n" + ); +} - #[test] - fn test_right_rotate() { - let table = Table::new([(123, 456, 789), (234, 567, 891), (111, 222, 333)]); - assert_eq!( - table.to_string(), - "+-----+-----+-----+\n\ - | i32 | i32 | i32 |\n\ - +-----+-----+-----+\n\ - | 123 | 456 | 789 |\n\ - +-----+-----+-----+\n\ - | 234 | 567 | 891 |\n\ - +-----+-----+-----+\n\ - | 111 | 222 | 333 |\n\ - +-----+-----+-----+\n" - ); +#[test] +fn test_right_rotate() { + let table = Table::new([(123, 456, 789), (234, 567, 891), (111, 222, 333)]); - let table = table.with(Rotate::Right); - assert_eq!( - table.to_string(), - "+-----+-----+-----+-----+\n\ - | 111 | 234 | 123 | i32 |\n\ - +-----+-----+-----+-----+\n\ - | 222 | 567 | 456 | i32 |\n\ - +-----+-----+-----+-----+\n\ - | 333 | 891 | 789 | i32 |\n\ - +-----+-----+-----+-----+\n" - ); - } + let table = table.with(Rotate::Right); + assert_eq!( + table.to_string(), + "+-----+-----+-----+-----+\n\ + | 111 | 234 | 123 | i32 |\n\ + +-----+-----+-----+-----+\n\ + | 222 | 567 | 456 | i32 |\n\ + +-----+-----+-----+-----+\n\ + | 333 | 891 | 789 | i32 |\n\ + +-----+-----+-----+-----+\n" + ); +} - #[test] - fn test_bottom_rotate() { - let table = Table::new([(123, 456, 789), (234, 567, 891), (111, 222, 333)]); - assert_eq!( - table.to_string(), - "+-----+-----+-----+\n\ - | i32 | i32 | i32 |\n\ - +-----+-----+-----+\n\ - | 123 | 456 | 789 |\n\ - +-----+-----+-----+\n\ - | 234 | 567 | 891 |\n\ - +-----+-----+-----+\n\ - | 111 | 222 | 333 |\n\ - +-----+-----+-----+\n" - ); +#[test] +fn test_bottom_rotate() { + let table = Table::new([(123, 456, 789), (234, 567, 891), (111, 222, 333)]); - let table = table.with(Rotate::Bottom); - assert_eq!( - table.to_string(), - "+-----+-----+-----+\n\ - | 111 | 222 | 333 |\n\ - +-----+-----+-----+\n\ - | 234 | 567 | 891 |\n\ - +-----+-----+-----+\n\ - | 123 | 456 | 789 |\n\ - +-----+-----+-----+\n\ - | i32 | i32 | i32 |\n\ - +-----+-----+-----+\n" - ); - } + let table = table.with(Rotate::Bottom); + assert_eq!( + table.to_string(), + "+-----+-----+-----+\n\ + | 111 | 222 | 333 |\n\ + +-----+-----+-----+\n\ + | 234 | 567 | 891 |\n\ + +-----+-----+-----+\n\ + | 123 | 456 | 789 |\n\ + +-----+-----+-----+\n\ + | i32 | i32 | i32 |\n\ + +-----+-----+-----+\n" + ); +} - #[test] - fn test_top_rotate() { - let table = Table::new([(123, 456, 789), (234, 567, 891), (111, 222, 333)]); - assert_eq!( - table.to_string(), - "+-----+-----+-----+\n\ - | i32 | i32 | i32 |\n\ - +-----+-----+-----+\n\ - | 123 | 456 | 789 |\n\ - +-----+-----+-----+\n\ - | 234 | 567 | 891 |\n\ - +-----+-----+-----+\n\ - | 111 | 222 | 333 |\n\ - +-----+-----+-----+\n" - ); +#[test] +fn test_top_rotate() { + let table = Table::new([(123, 456, 789), (234, 567, 891), (111, 222, 333)]); - let table = table.with(Rotate::Top); - assert_eq!( - table.to_string(), - "+-----+-----+-----+\n\ - | 111 | 222 | 333 |\n\ - +-----+-----+-----+\n\ - | 234 | 567 | 891 |\n\ - +-----+-----+-----+\n\ - | 123 | 456 | 789 |\n\ - +-----+-----+-----+\n\ - | i32 | i32 | i32 |\n\ - +-----+-----+-----+\n" - ); - } + let table = table.with(Rotate::Top); + assert_eq!( + table.to_string(), + "+-----+-----+-----+\n\ + | 111 | 222 | 333 |\n\ + +-----+-----+-----+\n\ + | 234 | 567 | 891 |\n\ + +-----+-----+-----+\n\ + | 123 | 456 | 789 |\n\ + +-----+-----+-----+\n\ + | i32 | i32 | i32 |\n\ + +-----+-----+-----+\n" + ); } #[test] From 7cfbd5e23300717fdcffdc706ab6ac105f724ef7 Mon Sep 17 00:00:00 2001 From: Maxim Zhiburt Date: Sat, 20 Nov 2021 15:45:37 +0300 Subject: [PATCH 3/7] tabled/ Add more highliting tests Signed-off-by: Maxim Zhiburt --- tests/style_test.rs | 105 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 104 insertions(+), 1 deletion(-) diff --git a/tests/style_test.rs b/tests/style_test.rs index ea9262b2..8e51db3f 100644 --- a/tests/style_test.rs +++ b/tests/style_test.rs @@ -335,7 +335,7 @@ fn custom_style() { } #[test] -fn style_highlingting() { +fn style_highlingt_cell() { let data = vec![ Linux { id: 0, @@ -384,3 +384,106 @@ fn style_highlingting() { assert_eq!(table, expected); } + +#[test] +fn style_highlingt_row() { + let data = vec![ + Linux { + id: 0, + destribution: "Fedora", + link: "https://getfedora.org/", + }, + Linux { + id: 2, + destribution: "OpenSUSE", + link: "https://www.opensuse.org/", + }, + Linux { + id: 3, + destribution: "Endeavouros", + link: "https://endeavouros.com/", + }, + ]; + + let expected = concat!( + "+++++++++++++++++++++++++++++++++++++++++++++++++\n", + "+ id │ destribution │ link +\n", + "+++++++++++++++++++++++++++++++++++++++++++++++++\n", + "│ 0 │ Fedora │ https://getfedora.org/ │\n", + "├────┼──────────────┼───────────────────────────┤\n", + "│ 2 │ OpenSUSE │ https://www.opensuse.org/ │\n", + "*************************************************\n", + "* 3 │ Endeavouros │ https://endeavouros.com/ *\n", + "*************************************************\n", + ); + + let table = Table::new(&data) + .with( + Style::pseudo() + .highlight( + Entity::Row(0), + Border::full('+', '+', '+', '+', '+', '+', '+', '+'), + ) + .highlight( + Entity::Row(3), + Border::full('*', '*', '*', '*', '*', '*', '*', '*'), + ), + ) + .to_string(); + + println!("{}", table); + + assert_eq!(table, expected); +} + + +#[test] +fn style_highlingt_column() { + let data = vec![ + Linux { + id: 0, + destribution: "Fedora", + link: "https://getfedora.org/", + }, + Linux { + id: 2, + destribution: "OpenSUSE", + link: "https://www.opensuse.org/", + }, + Linux { + id: 3, + destribution: "Endeavouros", + link: "https://endeavouros.com/", + }, + ]; + + let expected = concat!( + "++++++──────────────*****************************\n", + "+ id + destribution * link *\n", + "+────+──────────────*───────────────────────────*\n", + "+ 0 + Fedora * https://getfedora.org/ *\n", + "+────+──────────────*───────────────────────────*\n", + "+ 2 + OpenSUSE * https://www.opensuse.org/ *\n", + "+────+──────────────*───────────────────────────*\n", + "+ 3 + Endeavouros * https://endeavouros.com/ *\n", + "++++++──────────────*****************************\n", + ); + + let table = Table::new(&data) + .with( + Style::pseudo() + .highlight( + Entity::Column(0), + Border::full('+', '+', '+', '+', '+', '+', '+', '+'), + ) + .highlight( + Entity::Column(2), + Border::full('*', '*', '*', '*', '*', '*', '*', '*'), + ), + ) + .to_string(); + + println!("{}", table); + + assert_eq!(table, expected); +} \ No newline at end of file From 9cf9cd9bde7cec91bb5b4c0a290bc440bad965c9 Mon Sep 17 00:00:00 2001 From: Maxim Zhiburt Date: Sat, 20 Nov 2021 15:55:38 +0300 Subject: [PATCH 4/7] tabled/ add test for ratated highlit Signed-off-by: Maxim Zhiburt --- tests/rotate_test.rs | 66 +++++++++++++++++++++++--------------------- 1 file changed, 35 insertions(+), 31 deletions(-) diff --git a/tests/rotate_test.rs b/tests/rotate_test.rs index 6dbfa1b7..5c6b3199 100644 --- a/tests/rotate_test.rs +++ b/tests/rotate_test.rs @@ -1,6 +1,9 @@ // todo: add method for SPACING between cells. // add MARGIN && PADDING instead of indent? -use tabled::{Full, Indent, Modify, Rotate, Style, Table, Tabled}; +use tabled::{ + papergrid::{Border, Entity}, + Rotate, Style, Table, +}; #[test] fn test_rotate() { @@ -151,44 +154,45 @@ fn test_top_rotate() { } #[test] -fn rotate_left_test() { - #[derive(Tabled)] - struct Linux { - id: u8, - destribution: &'static str, - link: &'static str, - } - - let data = vec![ - Linux { - id: 0, - destribution: "Fedora", - link: "https://getfedora.org/", - }, - Linux { - id: 2, - destribution: "OpenSUSE", - link: "https://www.opensuse.org/", - }, - Linux { - id: 3, - destribution: "Endeavouros", - link: "https://endeavouros.com/", - }, - ]; +fn rotate_preserve_border_styles_test() { + let data = [(123, 456, 789), (234, 567, 891), (111, 222, 333)]; let table = Table::new(&data) + .with(Style::default().highlight(Entity::Row(0), Border::default().top('*'))) .with(Rotate::Left) - .with(Style::noborder()) - .with(Modify::new(Full).with(Indent::new(1, 1, 0, 0))) .to_string(); assert_eq!( table, concat!( - " link https://getfedora.org/ https://www.opensuse.org/ https://endeavouros.com/ \n", - " destribution Fedora OpenSUSE Endeavouros \n", - " id 0 2 3 \n" + "******+-----+-----+-----+\n", + "| i32 | 789 | 891 | 333 |\n", + "+-----+-----+-----+-----+\n", + "| i32 | 456 | 567 | 222 |\n", + "+-----+-----+-----+-----+\n", + "| i32 | 123 | 234 | 111 |\n", + "+-----+-----+-----+-----+\n", + ), + ); + + + let table = Table::new(&data) + .with(Style::default().highlight(Entity::Cell(0, 2), Border::default().bottom('*'))) + .with(Rotate::Left) + .to_string(); + + // it's a correct behaviour because + // when we sen bottom border of cell(0, 2) we also set top border of cell(1, 2) + assert_eq!( + table, + concat!( + "+-----+*****+-----+-----+\n", + "| i32 | 789 | 891 | 333 |\n", + "+*****+-----+-----+-----+\n", + "| i32 | 456 | 567 | 222 |\n", + "+-----+-----+-----+-----+\n", + "| i32 | 123 | 234 | 111 |\n", + "+-----+-----+-----+-----+\n", ), ); } From d0d12843f3ca5c8037475b8f21f839f19039e7ad Mon Sep 17 00:00:00 2001 From: Maxim Zhiburt Date: Sat, 20 Nov 2021 16:34:58 +0300 Subject: [PATCH 5/7] tabled/ move Style::highlight to its own type Signed-off-by: Maxim Zhiburt --- src/highlight.rs | 65 +++++++++++++++++ src/lib.rs | 4 +- src/style.rs | 21 +----- tests/highlingt_test.rs | 141 ++++++++++++++++++++++++++++++++++++ tests/panel_test.rs | 12 ++-- tests/rotate_test.rs | 12 ++-- tests/style_test.rs | 155 ---------------------------------------- 7 files changed, 225 insertions(+), 185 deletions(-) create mode 100644 src/highlight.rs create mode 100644 tests/highlingt_test.rs diff --git a/src/highlight.rs b/src/highlight.rs new file mode 100644 index 00000000..7ab43eb6 --- /dev/null +++ b/src/highlight.rs @@ -0,0 +1,65 @@ +#[allow(unused)] +use crate::Table; +use crate::TableOption; +use papergrid::{Border, Entity, Grid, Settings}; + +pub struct Highlight { + target: Target, + border: Border, +} + +impl Highlight { + pub fn frame(border: Border) -> Self { + Self::new(Target::Frame, border) + } + + pub fn cell(row: usize, column: usize, border: Border) -> Self { + Self::new(Target::Cell{row, column}, border) + } + + pub fn row(row: usize, border: Border) -> Self { + Self::new(Target::Row{from: row, to: row+1}, border) + } + + pub fn column(column: usize, border: Border) -> Self { + Self::new(Target::Column{from: column, to: column+1}, border) + } + + fn new(target: Target, border: Border) -> Self { Self { target, border } } +} + +pub enum Target { + Cell { + row: usize, + column: usize, + }, + Row{ + from: usize, + to: usize, + }, + Column { + from: usize, + to: usize, + }, + Frame, +} + +impl TableOption for Highlight { + fn change(&mut self, grid: &mut Grid) { + let settings = Settings::default().border(self.border.clone()).border_restriction(false); + match &self.target { + &Target::Cell { row, column } => { + grid.set(&Entity::Cell(row, column), settings); + }, + &Target::Row { from, .. } => { + grid.set(&Entity::Row(from), settings); + }, + &Target::Column { from, .. } => { + grid.set(&Entity::Column(from), settings); + }, + Target::Frame => { + grid.set(&Entity::Global, settings); + }, + } + } +} \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 8901db31..1f22b27d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -154,11 +154,13 @@ mod panel; mod rotate; pub mod style; mod width; +mod highlight; pub use crate::{ alignment::*, disable::*, formating::*, indent::*, object::*, panel::*, rotate::*, - style::Style, width::*, + style::Style, width::*, highlight::*, }; +// todo: Remove this publice exposure pub use papergrid; pub use tabled_derive::Tabled; diff --git a/src/style.rs b/src/style.rs index 8ec6e866..b4390618 100644 --- a/src/style.rs +++ b/src/style.rs @@ -6,6 +6,9 @@ use papergrid::{Border, Entity, Grid, Settings}; // todo: rename default style to ascii // default must be a general default implementation to be able to use highlight without influening actual style. +// todo: make highlight argument to be Object instead of Entity +// to be able to support highliting a subset of rows e.g. + /// Style is responsible for a look of a [Table]. /// /// # Example @@ -28,7 +31,6 @@ pub struct Style { header_split_line: Option, split: Option, inner_split_char: char, - highlight: Vec<(Entity, Border)>, } impl Style { @@ -200,20 +202,12 @@ impl Style { self } - /// Add highlight for a given cell. - pub fn highlight(mut self, entity: Entity, border: Border) -> Self { - // suppose to be LeftToRight algorithm - self.highlight.push((entity, border)); - self - } - fn new(frame: Frame, header: Option, split: Option, inner: char) -> Self { Self { frame, split, header_split_line: header, inner_split_char: inner, - highlight: Vec::new(), } } } @@ -278,15 +272,6 @@ impl TableOption for Style { ); } } - - for (entity, brush) in &self.highlight { - grid.set( - entity, - Settings::default() - .border(brush.clone()) - .border_restriction(false), - ); - } } } diff --git a/tests/highlingt_test.rs b/tests/highlingt_test.rs new file mode 100644 index 00000000..58790210 --- /dev/null +++ b/tests/highlingt_test.rs @@ -0,0 +1,141 @@ +use papergrid::Border; +use tabled::{Highlight, Style, Table, Tabled}; + + +#[derive(Tabled)] +struct Linux { + id: u8, + destribution: &'static str, + link: &'static str, +} + +#[test] +fn style_highlingt_cell() { + let data = vec![ + Linux { + id: 0, + destribution: "Fedora", + link: "https://getfedora.org/", + }, + Linux { + id: 2, + destribution: "OpenSUSE", + link: "https://www.opensuse.org/", + }, + Linux { + id: 3, + destribution: "Endeavouros", + link: "https://endeavouros.com/", + }, + ]; + + let expected = concat!( + "++++++──────────────┬───────────────────────────┐\n", + "+ id + destribution │ link │\n", + "+++++****************───────────────────────────┤\n", + "│ 0 * Fedora * https://getfedora.org/ │\n", + "├────****************───────────────────────────┤\n", + "│ 2 │ OpenSUSE │ https://www.opensuse.org/ │\n", + "├────┼──────────────┼───────────────────────────┤\n", + "│ 3 │ Endeavouros │ https://endeavouros.com/ │\n", + "└────┴──────────────┴───────────────────────────┘\n", + ); + + let table = Table::new(&data) + .with(Style::pseudo()) + .with(Highlight::cell(0, 0, Border::full('+', '+', '+', '+', '+', '+', '+', '+'))) + .with(Highlight::cell(1, 1, Border::full('*', '*', '*', '*', '*', '*', '*', '*'))) + .to_string(); + + println!("{}", table); + + assert_eq!(table, expected); +} + +#[test] +fn style_highlingt_row() { + let data = vec![ + Linux { + id: 0, + destribution: "Fedora", + link: "https://getfedora.org/", + }, + Linux { + id: 2, + destribution: "OpenSUSE", + link: "https://www.opensuse.org/", + }, + Linux { + id: 3, + destribution: "Endeavouros", + link: "https://endeavouros.com/", + }, + ]; + + let expected = concat!( + "+++++++++++++++++++++++++++++++++++++++++++++++++\n", + "+ id │ destribution │ link +\n", + "+++++++++++++++++++++++++++++++++++++++++++++++++\n", + "│ 0 │ Fedora │ https://getfedora.org/ │\n", + "├────┼──────────────┼───────────────────────────┤\n", + "│ 2 │ OpenSUSE │ https://www.opensuse.org/ │\n", + "*************************************************\n", + "* 3 │ Endeavouros │ https://endeavouros.com/ *\n", + "*************************************************\n", + ); + + let table = Table::new(&data) + .with(Style::pseudo()) + .with(Highlight::row(0, Border::full('+', '+', '+', '+', '+', '+', '+', '+'))) + .with(Highlight::row(3, Border::full('*', '*', '*', '*', '*', '*', '*', '*'))) + .to_string(); + + println!("{}", table); + + assert_eq!(table, expected); +} + + +#[test] +fn style_highlingt_column() { + let data = vec![ + Linux { + id: 0, + destribution: "Fedora", + link: "https://getfedora.org/", + }, + Linux { + id: 2, + destribution: "OpenSUSE", + link: "https://www.opensuse.org/", + }, + Linux { + id: 3, + destribution: "Endeavouros", + link: "https://endeavouros.com/", + }, + ]; + + let expected = concat!( + "++++++──────────────*****************************\n", + "+ id + destribution * link *\n", + "+────+──────────────*───────────────────────────*\n", + "+ 0 + Fedora * https://getfedora.org/ *\n", + "+────+──────────────*───────────────────────────*\n", + "+ 2 + OpenSUSE * https://www.opensuse.org/ *\n", + "+────+──────────────*───────────────────────────*\n", + "+ 3 + Endeavouros * https://endeavouros.com/ *\n", + "++++++──────────────*****************************\n", + ); + + + let table = Table::new(&data) + .with(Style::pseudo()) + .with(Highlight::column(0, Border::full('+', '+', '+', '+', '+', '+', '+', '+'))) + .with(Highlight::column(2, Border::full('*', '*', '*', '*', '*', '*', '*', '*'))) + .to_string(); + + println!("{}", table); + + assert_eq!(table, expected); +} \ No newline at end of file diff --git a/tests/panel_test.rs b/tests/panel_test.rs index a18370fd..80285a10 100644 --- a/tests/panel_test.rs +++ b/tests/panel_test.rs @@ -1,5 +1,7 @@ -use papergrid::{Border, Entity}; -use tabled::{Alignment, Footer, Full, Header, Modify, Object, Panel, Row, Style, Table, Tabled}; +use papergrid::Border; +use tabled::{ + Alignment, Footer, Full, Header, Highlight, Modify, Object, Panel, Row, Style, Table, Tabled, +}; #[test] fn panel_has_no_style_by_default() { @@ -28,8 +30,10 @@ fn panel_has_no_style_by_default() { fn highligt_panel() { let table = Table::new(test_data()) .with(Panel("Linux Distributions", 0)) - .with(Style::psql().highlight( - Entity::Cell(0, 0), + .with(Style::psql()) + .with(Highlight::cell( + 0, + 0, Border::full('#', '#', '#', '#', '#', '#', '#', '#'), )) .to_string(); diff --git a/tests/rotate_test.rs b/tests/rotate_test.rs index 5c6b3199..eab9d1a6 100644 --- a/tests/rotate_test.rs +++ b/tests/rotate_test.rs @@ -1,9 +1,6 @@ // todo: add method for SPACING between cells. // add MARGIN && PADDING instead of indent? -use tabled::{ - papergrid::{Border, Entity}, - Rotate, Style, Table, -}; +use tabled::{papergrid::Border, Highlight, Rotate, Style, Table}; #[test] fn test_rotate() { @@ -158,7 +155,8 @@ fn rotate_preserve_border_styles_test() { let data = [(123, 456, 789), (234, 567, 891), (111, 222, 333)]; let table = Table::new(&data) - .with(Style::default().highlight(Entity::Row(0), Border::default().top('*'))) + .with(Style::default()) + .with(Highlight::row(0, Border::default().top('*'))) .with(Rotate::Left) .to_string(); @@ -175,9 +173,9 @@ fn rotate_preserve_border_styles_test() { ), ); - let table = Table::new(&data) - .with(Style::default().highlight(Entity::Cell(0, 2), Border::default().bottom('*'))) + .with(Style::default()) + .with(Highlight::cell(0, 2, Border::default().bottom('*'))) .with(Rotate::Left) .to_string(); diff --git a/tests/style_test.rs b/tests/style_test.rs index 8e51db3f..14fc79c9 100644 --- a/tests/style_test.rs +++ b/tests/style_test.rs @@ -1,4 +1,3 @@ -use papergrid::{Border, Entity}; use tabled::style::Line; use tabled::{Style, Table, Tabled}; @@ -333,157 +332,3 @@ fn custom_style() { assert_eq!(table, expected); } - -#[test] -fn style_highlingt_cell() { - let data = vec![ - Linux { - id: 0, - destribution: "Fedora", - link: "https://getfedora.org/", - }, - Linux { - id: 2, - destribution: "OpenSUSE", - link: "https://www.opensuse.org/", - }, - Linux { - id: 3, - destribution: "Endeavouros", - link: "https://endeavouros.com/", - }, - ]; - - let expected = concat!( - "++++++──────────────┬───────────────────────────┐\n", - "+ id + destribution │ link │\n", - "+++++****************───────────────────────────┤\n", - "│ 0 * Fedora * https://getfedora.org/ │\n", - "├────****************───────────────────────────┤\n", - "│ 2 │ OpenSUSE │ https://www.opensuse.org/ │\n", - "├────┼──────────────┼───────────────────────────┤\n", - "│ 3 │ Endeavouros │ https://endeavouros.com/ │\n", - "└────┴──────────────┴───────────────────────────┘\n", - ); - - let table = Table::new(&data) - .with( - Style::pseudo() - .highlight( - Entity::Cell(0, 0), - Border::full('+', '+', '+', '+', '+', '+', '+', '+'), - ) - .highlight( - Entity::Cell(1, 1), - Border::full('*', '*', '*', '*', '*', '*', '*', '*'), - ), - ) - .to_string(); - - println!("{}", table); - - assert_eq!(table, expected); -} - -#[test] -fn style_highlingt_row() { - let data = vec![ - Linux { - id: 0, - destribution: "Fedora", - link: "https://getfedora.org/", - }, - Linux { - id: 2, - destribution: "OpenSUSE", - link: "https://www.opensuse.org/", - }, - Linux { - id: 3, - destribution: "Endeavouros", - link: "https://endeavouros.com/", - }, - ]; - - let expected = concat!( - "+++++++++++++++++++++++++++++++++++++++++++++++++\n", - "+ id │ destribution │ link +\n", - "+++++++++++++++++++++++++++++++++++++++++++++++++\n", - "│ 0 │ Fedora │ https://getfedora.org/ │\n", - "├────┼──────────────┼───────────────────────────┤\n", - "│ 2 │ OpenSUSE │ https://www.opensuse.org/ │\n", - "*************************************************\n", - "* 3 │ Endeavouros │ https://endeavouros.com/ *\n", - "*************************************************\n", - ); - - let table = Table::new(&data) - .with( - Style::pseudo() - .highlight( - Entity::Row(0), - Border::full('+', '+', '+', '+', '+', '+', '+', '+'), - ) - .highlight( - Entity::Row(3), - Border::full('*', '*', '*', '*', '*', '*', '*', '*'), - ), - ) - .to_string(); - - println!("{}", table); - - assert_eq!(table, expected); -} - - -#[test] -fn style_highlingt_column() { - let data = vec![ - Linux { - id: 0, - destribution: "Fedora", - link: "https://getfedora.org/", - }, - Linux { - id: 2, - destribution: "OpenSUSE", - link: "https://www.opensuse.org/", - }, - Linux { - id: 3, - destribution: "Endeavouros", - link: "https://endeavouros.com/", - }, - ]; - - let expected = concat!( - "++++++──────────────*****************************\n", - "+ id + destribution * link *\n", - "+────+──────────────*───────────────────────────*\n", - "+ 0 + Fedora * https://getfedora.org/ *\n", - "+────+──────────────*───────────────────────────*\n", - "+ 2 + OpenSUSE * https://www.opensuse.org/ *\n", - "+────+──────────────*───────────────────────────*\n", - "+ 3 + Endeavouros * https://endeavouros.com/ *\n", - "++++++──────────────*****************************\n", - ); - - let table = Table::new(&data) - .with( - Style::pseudo() - .highlight( - Entity::Column(0), - Border::full('+', '+', '+', '+', '+', '+', '+', '+'), - ) - .highlight( - Entity::Column(2), - Border::full('*', '*', '*', '*', '*', '*', '*', '*'), - ), - ) - .to_string(); - - println!("{}", table); - - assert_eq!(table, expected); -} \ No newline at end of file From 5ae9a6d8f3fb31d060e44347b3eab968e779542d Mon Sep 17 00:00:00 2001 From: Maxim Zhiburt Date: Sat, 20 Nov 2021 17:01:45 +0300 Subject: [PATCH 6/7] tabled/ Add support of ranges in Highlight Signed-off-by: Maxim Zhiburt --- src/highlight.rs | 75 ++++++++++++++++++++++++++++++------ tests/highlingt_test.rs | 85 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 149 insertions(+), 11 deletions(-) diff --git a/src/highlight.rs b/src/highlight.rs index 7ab43eb6..129b1a12 100644 --- a/src/highlight.rs +++ b/src/highlight.rs @@ -18,11 +18,21 @@ impl Highlight { } pub fn row(row: usize, border: Border) -> Self { - Self::new(Target::Row{from: row, to: row+1}, border) + Self::row_range(row, row+1, border) + } + + pub fn row_range(start: usize, end: usize, border: Border) -> Self { + assert!(end > start); + Self::new(Target::Row{from: start, to: end}, border) } pub fn column(column: usize, border: Border) -> Self { - Self::new(Target::Column{from: column, to: column+1}, border) + Self::column_range(column, column+1, border) + } + + pub fn column_range(start: usize, end: usize, border: Border) -> Self { + assert!(end > start); + Self::new(Target::Column{from: start, to: end}, border) } fn new(target: Target, border: Border) -> Self { Self { target, border } } @@ -46,20 +56,63 @@ pub enum Target { impl TableOption for Highlight { fn change(&mut self, grid: &mut Grid) { - let settings = Settings::default().border(self.border.clone()).border_restriction(false); - match &self.target { - &Target::Cell { row, column } => { + match self.target { + Target::Cell { row, column } => { + let settings = Settings::default().border(self.border.clone()).border_restriction(false); grid.set(&Entity::Cell(row, column), settings); }, - &Target::Row { from, .. } => { - grid.set(&Entity::Row(from), settings); - }, - &Target::Column { from, .. } => { - grid.set(&Entity::Column(from), settings); - }, Target::Frame => { + let settings = Settings::default().border(self.border.clone()).border_restriction(false); grid.set(&Entity::Global, settings); }, + Target::Row { from, to } => { + if to == from + 1 { + let settings = Settings::default().border(self.border.clone()).border_restriction(false); + grid.set(&Entity::Row(from), settings); + } else { + for row in from .. to { + let mut border = self.border.clone(); + + let is_first_row = row == from; + let is_last_row = row + 1 == to; + + if !is_first_row { + border.top = None; + } + + if !is_last_row { + border.bottom = None; + } + + let settings = Settings::default().border(border).border_restriction(false); + grid.set(&Entity::Row(row), settings); + } + } + }, + Target::Column { from, to } => { + if to == from + 1 { + let settings = Settings::default().border(self.border.clone()).border_restriction(false); + grid.set(&Entity::Column(from), settings); + } else { + for column in from .. to { + let mut border = self.border.clone(); + + let is_first_column = column == from; + let is_last_column = column + 1 == to; + + if !is_first_column { + border.left = None; + } + + if !is_last_column { + border.right = None; + } + + let settings = Settings::default().border(border).border_restriction(false); + grid.set(&Entity::Column(column), settings); + } + } + }, } } } \ No newline at end of file diff --git a/tests/highlingt_test.rs b/tests/highlingt_test.rs index 58790210..0007935c 100644 --- a/tests/highlingt_test.rs +++ b/tests/highlingt_test.rs @@ -137,5 +137,90 @@ fn style_highlingt_column() { println!("{}", table); + assert_eq!(table, expected); +} + +#[test] +fn style_highlingt_row_range() { + let data = vec![ + Linux { + id: 0, + destribution: "Fedora", + link: "https://getfedora.org/", + }, + Linux { + id: 2, + destribution: "OpenSUSE", + link: "https://www.opensuse.org/", + }, + Linux { + id: 3, + destribution: "Endeavouros", + link: "https://endeavouros.com/", + }, + ]; + + let expected = concat!( + "┌────┬──────────────┬───────────────────────────┐\n", + "│ id │ destribution │ link │\n", + "+++++++++++++++++++++++++++++++++++++++++++++++++\n", + "+ 0 │ Fedora │ https://getfedora.org/ +\n", + "+────┼──────────────┼───────────────────────────+\n", + "+ 2 │ OpenSUSE │ https://www.opensuse.org/ +\n", + "+++++++++++++++++++++++++++++++++++++++++++++++++\n", + "│ 3 │ Endeavouros │ https://endeavouros.com/ │\n", + "└────┴──────────────┴───────────────────────────┘\n", + ); + + let table = Table::new(&data) + .with(Style::pseudo()) + .with(Highlight::row_range(1, 3, Border::full('+', '+', '+', '+', '+', '+', '+', '+'))) + .to_string(); + + println!("{}", table); + + assert_eq!(table, expected); +} + +#[test] +fn style_highlingt_column_range() { + let data = vec![ + Linux { + id: 0, + destribution: "Fedora", + link: "https://getfedora.org/", + }, + Linux { + id: 2, + destribution: "OpenSUSE", + link: "https://www.opensuse.org/", + }, + Linux { + id: 3, + destribution: "Endeavouros", + link: "https://endeavouros.com/", + }, + ]; + + let expected = concat!( + "+++++++++++++++++++++───────────────────────────┐\n", + "+ id │ destribution + link │\n", + "+────┼──────────────+───────────────────────────┤\n", + "+ 0 │ Fedora + https://getfedora.org/ │\n", + "+────┼──────────────+───────────────────────────┤\n", + "+ 2 │ OpenSUSE + https://www.opensuse.org/ │\n", + "+────┼──────────────+───────────────────────────┤\n", + "+ 3 │ Endeavouros + https://endeavouros.com/ │\n", + "+++++++++++++++++++++───────────────────────────┘\n", + ); + + + let table = Table::new(&data) + .with(Style::pseudo()) + .with(Highlight::column_range(0, 2, Border::full('+', '+', '+', '+', '+', '+', '+', '+'))) + .to_string(); + + println!("{}", table); + assert_eq!(table, expected); } \ No newline at end of file From 356f4770ce45a5487b88139c6193d4bbd18ff8d2 Mon Sep 17 00:00:00 2001 From: Maxim Zhiburt Date: Sat, 20 Nov 2021 17:02:26 +0300 Subject: [PATCH 7/7] Fix cargo fmt Signed-off-by: Maxim Zhiburt --- src/highlight.rs | 75 ++++++++++++++++++++++++----------------- src/lib.rs | 6 ++-- src/style.rs | 2 +- tests/highlingt_test.rs | 50 ++++++++++++++++++++------- 4 files changed, 85 insertions(+), 48 deletions(-) diff --git a/src/highlight.rs b/src/highlight.rs index 129b1a12..2268f5c0 100644 --- a/src/highlight.rs +++ b/src/highlight.rs @@ -14,43 +14,48 @@ impl Highlight { } pub fn cell(row: usize, column: usize, border: Border) -> Self { - Self::new(Target::Cell{row, column}, border) + Self::new(Target::Cell { row, column }, border) } - + pub fn row(row: usize, border: Border) -> Self { - Self::row_range(row, row+1, border) + Self::row_range(row, row + 1, border) } pub fn row_range(start: usize, end: usize, border: Border) -> Self { assert!(end > start); - Self::new(Target::Row{from: start, to: end}, border) + Self::new( + Target::Row { + from: start, + to: end, + }, + border, + ) } pub fn column(column: usize, border: Border) -> Self { - Self::column_range(column, column+1, border) + Self::column_range(column, column + 1, border) } pub fn column_range(start: usize, end: usize, border: Border) -> Self { assert!(end > start); - Self::new(Target::Column{from: start, to: end}, border) + Self::new( + Target::Column { + from: start, + to: end, + }, + border, + ) } - fn new(target: Target, border: Border) -> Self { Self { target, border } } + fn new(target: Target, border: Border) -> Self { + Self { target, border } + } } pub enum Target { - Cell { - row: usize, - column: usize, - }, - Row{ - from: usize, - to: usize, - }, - Column { - from: usize, - to: usize, - }, + Cell { row: usize, column: usize }, + Row { from: usize, to: usize }, + Column { from: usize, to: usize }, Frame, } @@ -58,19 +63,25 @@ impl TableOption for Highlight { fn change(&mut self, grid: &mut Grid) { match self.target { Target::Cell { row, column } => { - let settings = Settings::default().border(self.border.clone()).border_restriction(false); + let settings = Settings::default() + .border(self.border.clone()) + .border_restriction(false); grid.set(&Entity::Cell(row, column), settings); - }, + } Target::Frame => { - let settings = Settings::default().border(self.border.clone()).border_restriction(false); + let settings = Settings::default() + .border(self.border.clone()) + .border_restriction(false); grid.set(&Entity::Global, settings); - }, + } Target::Row { from, to } => { if to == from + 1 { - let settings = Settings::default().border(self.border.clone()).border_restriction(false); + let settings = Settings::default() + .border(self.border.clone()) + .border_restriction(false); grid.set(&Entity::Row(from), settings); } else { - for row in from .. to { + for row in from..to { let mut border = self.border.clone(); let is_first_row = row == from; @@ -82,19 +93,21 @@ impl TableOption for Highlight { if !is_last_row { border.bottom = None; - } + } let settings = Settings::default().border(border).border_restriction(false); grid.set(&Entity::Row(row), settings); } } - }, + } Target::Column { from, to } => { if to == from + 1 { - let settings = Settings::default().border(self.border.clone()).border_restriction(false); + let settings = Settings::default() + .border(self.border.clone()) + .border_restriction(false); grid.set(&Entity::Column(from), settings); } else { - for column in from .. to { + for column in from..to { let mut border = self.border.clone(); let is_first_column = column == from; @@ -112,7 +125,7 @@ impl TableOption for Highlight { grid.set(&Entity::Column(column), settings); } } - }, + } } } -} \ No newline at end of file +} diff --git a/src/lib.rs b/src/lib.rs index 1f22b27d..5d0b8180 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -148,17 +148,17 @@ mod alignment; mod disable; pub mod display; mod formating; +mod highlight; mod indent; mod object; mod panel; mod rotate; pub mod style; mod width; -mod highlight; pub use crate::{ - alignment::*, disable::*, formating::*, indent::*, object::*, panel::*, rotate::*, - style::Style, width::*, highlight::*, + alignment::*, disable::*, formating::*, highlight::*, indent::*, object::*, panel::*, + rotate::*, style::Style, width::*, }; // todo: Remove this publice exposure pub use papergrid; diff --git a/src/style.rs b/src/style.rs index b4390618..2e497803 100644 --- a/src/style.rs +++ b/src/style.rs @@ -7,7 +7,7 @@ use papergrid::{Border, Entity, Grid, Settings}; // default must be a general default implementation to be able to use highlight without influening actual style. // todo: make highlight argument to be Object instead of Entity -// to be able to support highliting a subset of rows e.g. +// to be able to support highliting a subset of rows e.g. /// Style is responsible for a look of a [Table]. /// diff --git a/tests/highlingt_test.rs b/tests/highlingt_test.rs index 0007935c..91818d73 100644 --- a/tests/highlingt_test.rs +++ b/tests/highlingt_test.rs @@ -1,7 +1,6 @@ use papergrid::Border; use tabled::{Highlight, Style, Table, Tabled}; - #[derive(Tabled)] struct Linux { id: u8, @@ -43,8 +42,16 @@ fn style_highlingt_cell() { let table = Table::new(&data) .with(Style::pseudo()) - .with(Highlight::cell(0, 0, Border::full('+', '+', '+', '+', '+', '+', '+', '+'))) - .with(Highlight::cell(1, 1, Border::full('*', '*', '*', '*', '*', '*', '*', '*'))) + .with(Highlight::cell( + 0, + 0, + Border::full('+', '+', '+', '+', '+', '+', '+', '+'), + )) + .with(Highlight::cell( + 1, + 1, + Border::full('*', '*', '*', '*', '*', '*', '*', '*'), + )) .to_string(); println!("{}", table); @@ -86,8 +93,14 @@ fn style_highlingt_row() { let table = Table::new(&data) .with(Style::pseudo()) - .with(Highlight::row(0, Border::full('+', '+', '+', '+', '+', '+', '+', '+'))) - .with(Highlight::row(3, Border::full('*', '*', '*', '*', '*', '*', '*', '*'))) + .with(Highlight::row( + 0, + Border::full('+', '+', '+', '+', '+', '+', '+', '+'), + )) + .with(Highlight::row( + 3, + Border::full('*', '*', '*', '*', '*', '*', '*', '*'), + )) .to_string(); println!("{}", table); @@ -95,7 +108,6 @@ fn style_highlingt_row() { assert_eq!(table, expected); } - #[test] fn style_highlingt_column() { let data = vec![ @@ -128,11 +140,16 @@ fn style_highlingt_column() { "++++++──────────────*****************************\n", ); - let table = Table::new(&data) .with(Style::pseudo()) - .with(Highlight::column(0, Border::full('+', '+', '+', '+', '+', '+', '+', '+'))) - .with(Highlight::column(2, Border::full('*', '*', '*', '*', '*', '*', '*', '*'))) + .with(Highlight::column( + 0, + Border::full('+', '+', '+', '+', '+', '+', '+', '+'), + )) + .with(Highlight::column( + 2, + Border::full('*', '*', '*', '*', '*', '*', '*', '*'), + )) .to_string(); println!("{}", table); @@ -174,7 +191,11 @@ fn style_highlingt_row_range() { let table = Table::new(&data) .with(Style::pseudo()) - .with(Highlight::row_range(1, 3, Border::full('+', '+', '+', '+', '+', '+', '+', '+'))) + .with(Highlight::row_range( + 1, + 3, + Border::full('+', '+', '+', '+', '+', '+', '+', '+'), + )) .to_string(); println!("{}", table); @@ -214,13 +235,16 @@ fn style_highlingt_column_range() { "+++++++++++++++++++++───────────────────────────┘\n", ); - let table = Table::new(&data) .with(Style::pseudo()) - .with(Highlight::column_range(0, 2, Border::full('+', '+', '+', '+', '+', '+', '+', '+'))) + .with(Highlight::column_range( + 0, + 2, + Border::full('+', '+', '+', '+', '+', '+', '+', '+'), + )) .to_string(); println!("{}", table); assert_eq!(table, expected); -} \ No newline at end of file +}