Skip to content

Commit

Permalink
Merge 356f477 into a405fcc
Browse files Browse the repository at this point in the history
  • Loading branch information
zhiburt committed Nov 20, 2021
2 parents a405fcc + 356f477 commit c96ed9d
Show file tree
Hide file tree
Showing 8 changed files with 578 additions and 316 deletions.
131 changes: 131 additions & 0 deletions 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);
}
}
}
}
}
}
6 changes: 4 additions & 2 deletions src/lib.rs
Expand Up @@ -148,6 +148,7 @@ mod alignment;
mod disable;
pub mod display;
mod formating;
mod highlight;
mod indent;
mod object;
mod panel;
Expand All @@ -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;

Expand Down
211 changes: 0 additions & 211 deletions src/rotate.rs
Expand Up @@ -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"
);
}
}

0 comments on commit c96ed9d

Please sign in to comment.