diff --git a/src/highlight.rs b/src/highlight.rs new file mode 100644 index 00000000..2268f5c0 --- /dev/null +++ b/src/highlight.rs @@ -0,0 +1,131 @@ +#[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::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::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 } + } +} + +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) { + 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::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); + } + } + } + } + } +} diff --git a/src/lib.rs b/src/lib.rs index 8901db31..5d0b8180 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -148,6 +148,7 @@ mod alignment; mod disable; pub mod display; mod formating; +mod highlight; mod indent; mod object; mod panel; @@ -156,9 +157,10 @@ pub mod style; mod width; pub use crate::{ - alignment::*, disable::*, formating::*, indent::*, object::*, panel::*, rotate::*, - style::Style, width::*, + alignment::*, disable::*, formating::*, highlight::*, indent::*, object::*, panel::*, + rotate::*, style::Style, width::*, }; +// todo: Remove this publice exposure pub use papergrid; pub use tabled_derive::Tabled; 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/src/style.rs b/src/style.rs index 073881b1..2e497803 100644 --- a/src/style.rs +++ b/src/style.rs @@ -3,6 +3,12 @@ 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. + +// 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 @@ -25,7 +31,6 @@ pub struct Style { header_split_line: Option, split: Option, inner_split_char: char, - highlight: Vec<(Entity, Border)>, } impl Style { @@ -197,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(), } } } @@ -275,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..91818d73 --- /dev/null +++ b/tests/highlingt_test.rs @@ -0,0 +1,250 @@ +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); +} + +#[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); +} 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 6399b198..eab9d1a6 100644 --- a/tests/rotate_test.rs +++ b/tests/rotate_test.rs @@ -1,46 +1,196 @@ // 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, Highlight, Rotate, Style, Table}; -#[derive(Tabled)] -struct Linux { - id: u8, - destribution: &'static str, - link: &'static str, +#[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 rotate_left() { - 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 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::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)]); + + 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)]); + + 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)]); + + 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)]); + + 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_preserve_border_styles_test() { + let data = [(123, 456, 789), (234, 567, 891), (111, 222, 333)]; + + let table = Table::new(&data) + .with(Style::default()) + .with(Highlight::row(0, Border::default().top('*'))) + .with(Rotate::Left) + .to_string(); + + assert_eq!( + table, + concat!( + "******+-----+-----+-----+\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()) + .with(Highlight::cell(0, 2, Border::default().bottom('*'))) .with(Rotate::Left) - .with(Style::noborder()) - .with(Modify::new(Full).with(Indent::new(1, 1, 0, 0))) .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!( - " 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", ), ); } diff --git a/tests/style_test.rs b/tests/style_test.rs index ea9262b2..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,54 +332,3 @@ fn custom_style() { assert_eq!(table, expected); } - -#[test] -fn style_highlingting() { - 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); -}