Skip to content

Commit

Permalink
Merge pull request #30 from disco07/table
Browse files Browse the repository at this point in the history
Table
  • Loading branch information
disco07 authored Mar 21, 2023
2 parents 3725b1b + 0768978 commit 6b36a27
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 14 deletions.
35 changes: 34 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -15,3 +15,4 @@ license = "MIT"
[dependencies]
unicode-width = "0.1"
strip-ansi-escapes = "0.1.1"
regex = "1.7.1"
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ cargo add [email protected]
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" }
Expand Down
6 changes: 5 additions & 1 deletion src/styles/color.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::cell::Cell;
use regex::Regex;
use std::str;

pub trait Colorizer {
Expand All @@ -12,6 +13,7 @@ pub enum Font {
Light = 2,
Italic = 3,
Underlined = 4,
SlowBlinking = 5,
Blinking = 6,
Inverse = 7,
Invisible = 8,
Expand Down Expand Up @@ -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();
Expand Down
22 changes: 12 additions & 10 deletions src/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Vec<_>>()
}
Expand Down

0 comments on commit 6b36a27

Please sign in to comment.