From 051134aa741f00f030432f11c35cd4af4d0ce689 Mon Sep 17 00:00:00 2001 From: disco07 Date: Tue, 21 Mar 2023 04:07:16 +0100 Subject: [PATCH 1/3] add reg --- Cargo.lock | 33 +++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/styles/color.rs | 6 +++++- 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 357e35d..4091977 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,12 +2,27 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "aho-corasick" +version = "0.7.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cc936419f96fa211c1b9166887b38e5e40b19958e5b895be7c1f93adec7071ac" +dependencies = [ + "memchr", +] + [[package]] name = "arrayvec" version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "23b62fc65de8e4e7f52534fb52b0f3ed04746ae267519eef2a83941e8085068b" +[[package]] +name = "memchr" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" + [[package]] name = "proc-macro2" version = "1.0.52" @@ -30,10 +45,28 @@ dependencies = [ name = "rct" version = "0.1.3" dependencies = [ + "regex", "strip-ansi-escapes", "unicode-width", ] +[[package]] +name = "regex" +version = "1.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "48aaa5748ba571fb95cd2c85c09f629215d3a6ece942baa100950af03a34f733" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.6.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "456c603be3e8d448b072f410900c09faf164fbce2d480456f50eea6e25f9c848" + [[package]] name = "strip-ansi-escapes" version = "0.1.1" diff --git a/Cargo.toml b/Cargo.toml index 910159c..143fa04 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,3 +15,4 @@ license = "MIT" [dependencies] unicode-width = "0.1" strip-ansi-escapes = "0.1.1" +regex = "1.7.1" diff --git a/src/styles/color.rs b/src/styles/color.rs index ade4ab6..63a6c8a 100644 --- a/src/styles/color.rs +++ b/src/styles/color.rs @@ -1,4 +1,5 @@ use crate::cell::Cell; +use regex::Regex; use std::str; pub trait Colorizer { @@ -12,6 +13,7 @@ pub enum Font { Light = 2, Italic = 3, Underlined = 4, + SlowBlinking = 5, Blinking = 6, Inverse = 7, Invisible = 8, @@ -136,7 +138,9 @@ fn new_ansi(hex: &str, value: usize) -> String { /// assert_eq!(split_color, "string") /// ``` pub fn split_colors(color: &str) -> String { - if color.contains("[38;2;") || color.contains("[48;2;") { + let re = Regex::new(r"\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]").unwrap(); + + if re.is_match(color) { let strip_ansi_escapes = strip_ansi_escapes::strip(color).unwrap(); let color = str::from_utf8(&strip_ansi_escapes).unwrap(); return color.to_string(); From 173529370a05f001d3fd0ac350fbc27f6443018a Mon Sep 17 00:00:00 2001 From: disco07 Date: Tue, 21 Mar 2023 14:58:07 +0100 Subject: [PATCH 2/3] lil change --- src/table.rs | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/table.rs b/src/table.rs index 2795d47..4f5e0a5 100644 --- a/src/table.rs +++ b/src/table.rs @@ -233,16 +233,18 @@ impl Table { // [[" a ", " b ", " e "], [" ", " c ", " "], [" ", " d ", " "]] (0..max_column) .map(|i| { - let mut row = Vec::new(); - for (index, col) in content.iter().enumerate() { - let value = col.get(i).unwrap_or(&String::new()).to_owned(); - let width = *width_column.get(index).unwrap_or(&0); - // Add padding to the cell value to match the desired column width - let padding = - " ".repeat(width.saturating_sub(split_colors(&value).chars().count())); - row.push(format!("{}{}", value, padding)); - } - row + content + .iter() + .enumerate() + .fold(vec![], |mut acc, (index, col)| { + let value = col.get(i).unwrap_or(&String::new()).to_owned(); + let width = *width_column.get(index).unwrap_or(&0); + // Add padding to the cell value to match the desired column width + let padding = + " ".repeat(width.saturating_sub(split_colors(&value).chars().count())); + acc.push(format!("{}{}", value, padding)); + acc + }) }) .collect::>() } From 0768978930a562a4b4a8fe3bfcc58cee27ce40da Mon Sep 17 00:00:00 2001 From: disco07 Date: Tue, 21 Mar 2023 19:58:31 +0100 Subject: [PATCH 3/3] lil change --- Cargo.lock | 2 +- Cargo.toml | 2 +- README.md | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4091977..01af02c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -43,7 +43,7 @@ dependencies = [ [[package]] name = "rct" -version = "0.1.3" +version = "0.1.4" dependencies = [ "regex", "strip-ansi-escapes", diff --git a/Cargo.toml b/Cargo.toml index 143fa04..bfbdeaa 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rct" -version = "0.1.3" +version = "0.1.4" edition = "2021" authors = ["disco07"] description = "CLI Table Output for Rust Project" diff --git a/README.md b/README.md index c789eb1..019f3fa 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ cargo add rct@0.1.3 Or add this to your Cargo.toml file. ``` [dependencies] -rct = "0.1.3" +rct = "0.1.4" # Or add from github main branch. rct = { git = "https://github.com/disco07/rct.git", branch = "main" }