Skip to content

Commit

Permalink
Add support for colored wrapping
Browse files Browse the repository at this point in the history
Signed-off-by: Maxim Zhiburt <zhiburt@gmail.com>
  • Loading branch information
zhiburt committed Sep 9, 2021
1 parent a8aab8f commit 5697ede
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 11 deletions.
3 changes: 1 addition & 2 deletions Cargo.toml
Expand Up @@ -26,8 +26,7 @@ color = ["papergrid/color", "ansi-cut"]
[dependencies]
tabled_derive = {path = "./tabled_derive"}
papergrid = "0.1.22"
ansi-cut = { version = "0.1.0", optional = true }
unicode-segmentation = "1.8.0"
ansi-cut = { version = "0.1.1", optional = true }

[dev-dependencies]
owo-colors = "1"
1 change: 1 addition & 0 deletions examples/color.rs
Expand Up @@ -11,6 +11,7 @@ fn main() {
use owo_colors::OwoColorize;
use tabled::{Alignment, Column, Format, Head, Modify, Object, Row, Style, Table, Tabled};

#[allow(clippy::upper_case_acronyms)]
#[derive(Tabled)]
struct BSD {
distribution: &'static str,
Expand Down
31 changes: 22 additions & 9 deletions src/width.rs
Expand Up @@ -61,12 +61,14 @@ impl<S: AsRef<str>> CellOption for MaxWidth<S> {
let new_content = format!("{}{}", striped_content, filler.as_ref());
grid.set(Entity::Cell(row, column), Settings::new().text(new_content))
}

}
Wrap::Wrap => {
let wrapped_content = split(content, self.width);
if wrapped_content.len() != content.len() {
grid.set(Entity::Cell(row, column), Settings::new().text(wrapped_content))
grid.set(
Entity::Cell(row, column),
Settings::new().text(wrapped_content),
)
}
}
}
Expand All @@ -81,15 +83,26 @@ fn strip(s: &str, width: usize) -> String {
#[cfg(feature = "color")]
{
let max_width = std::cmp::min(s.chars().count(), width);
ansi_cut::AnsiCut::cut(&s, ..max_width).to_string()
ansi_cut::AnsiCut::cut(&s, ..max_width)
}
}

fn split(s: &str, width: usize) -> String {
s.chars()
.collect::<Vec<char>>()
.chunks(width)
.map(|chunk| chunk.iter().collect::<String>())
.collect::<Vec<String>>()
.join("\n")
#[cfg(not(feature = "color"))]
{
s.chars()
.collect::<Vec<char>>()
.chunks(width)
.map(|chunk| chunk.iter().collect::<String>())
.collect::<Vec<String>>()
.join("\n")
}
#[cfg(feature = "color")]
{
if width == 0 {
s.to_string()
} else {
ansi_cut::chunks(s, width).join("\n")
}
}
}
35 changes: 35 additions & 0 deletions tests/width_test.rs
Expand Up @@ -85,6 +85,41 @@ fn max_width_wrapped() {
assert_eq!(table, expected);
}

#[cfg(feature = "color")]
#[test]
fn max_width_wrapped_collored() {
use owo_colors::OwoColorize;

let data = &[
"asd".red().to_string(),
"zxc2".blue().to_string(),
"asdasd".on_black().green().to_string(),
];

let expected = concat!(
"| St |\n",
"| ri |\n",
"| ng |\n",
"|----|\n",
"| \u{1b}[31mas\u{1b}[0m |\n",
"| \u{1b}[31md\u{1b}[0m |\n",
"| \u{1b}[34mzx\u{1b}[0m |\n",
"| \u{1b}[34mc2\u{1b}[0m |\n",
"| \u{1b}[32m\u{1b}[40mas\u{1b}[0m\u{1b}[0m |\n",
"| \u{1b}[32m\u{1b}[40mda\u{1b}[0m\u{1b}[0m |\n",
"| \u{1b}[32m\u{1b}[40msd\u{1b}[0m\u{1b}[0m |\n",
);

let table = Table::new(data)
.with(Style::github_markdown())
.with(Modify::new(Full).with(MaxWidth::wrapping(2)))
.to_string();

println!("{}", table);

assert_eq!(expected, table);
}

#[test]
fn dont_change_content_if_width_is_less_then_max_width() {
let data = vec![
Expand Down

0 comments on commit 5697ede

Please sign in to comment.