From 1f5dab68d2182e484cff833be7c0dd45f384ccef Mon Sep 17 00:00:00 2001 From: Gerrit Viljoen Date: Sat, 16 May 2020 21:53:43 +0200 Subject: [PATCH] save game --- Cargo.toml | 3 +++ src/lib.rs | 6 +++++- src/test.rs | 29 +++++++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 3266438..ed8ad55 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,3 +12,6 @@ documentation = "https://docs.rs/ascii_table" readme = "readme.md" categories = ["command-line-interface"] keywords = ["ascii", "table"] + +[dev-dependencies] +colorful = "0.2" diff --git a/src/lib.rs b/src/lib.rs index 6944338..1e10fc2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -225,13 +225,17 @@ impl AsciiTable { let result: Vec<_> = (0..num_cols).map(|a| { let default_conf = &DEFAULT_COLUMN; let conf = self.columns.get(&a).unwrap_or(default_conf); - let column_width = data.iter().map(|row| row[a].chars().count()).max().unwrap(); + let column_width = data.iter().map(|row| Self::count_characters(&row[a])).max().unwrap(); let header_width = conf.header.chars().count(); column_width.max(header_width).min(conf.max_width) }).collect(); self.truncate_widths(result) } + fn count_characters(cell: &str) -> usize { + cell.chars().count() + } + fn truncate_widths(&self, mut widths: Vec) -> Vec { let max_width = self.max_width; let table_padding = Self::smallest_width(widths.len()); diff --git a/src/test.rs b/src/test.rs index 7f86836..449b2a2 100644 --- a/src/test.rs +++ b/src/test.rs @@ -15,9 +15,13 @@ // You should have received a copy of the GNU General Public License // along with ascii-table. If not, see . +use colorful::Color; +use colorful::Colorful; + use crate::{AsciiTable, Column}; use crate::Align::*; +use std::collections::BTreeMap; use std::fmt::Display; fn cube_config() -> AsciiTable { @@ -28,6 +32,19 @@ fn cube_config() -> AsciiTable { result } +#[test] +fn backwards_compatible() { + AsciiTable { + max_width: 0, + columns: BTreeMap::new() + }; + Column { + header: String::new(), + align: Left, + max_width: 0 + }; +} + #[test] fn empty_rows() { let config = AsciiTable::default(); @@ -504,3 +521,15 @@ fn mixed_types() { assert_eq!(expected, config.format(input)); } + +#[test] +fn color_codes() { + let config = AsciiTable::default(); + let text = "Hello".color(Color::Blue).bg_color(Color::Yellow).bold(); + let input = vec![vec![text]]; + let expected = "┌───────┐\n\ + │ \u{1b}[38;5;4m\u{1b}[48;5;3;1mHello\u{1b}[0m │\n\ + └───────┘\n"; + + assert_eq!(expected, config.format(input)); +}