From 493eaff3dfa5ab3b30f2242f088d4e0e8c97fb2c Mon Sep 17 00:00:00 2001 From: Tristan Lepine Date: Wed, 6 Nov 2024 16:22:35 -0500 Subject: [PATCH 1/3] added `Color::rgb_fg` and `Color::rgb_bg` --- tabled/src/settings/color/mod.rs | 55 ++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/tabled/src/settings/color/mod.rs b/tabled/src/settings/color/mod.rs index f7b084f9..c314dafb 100644 --- a/tabled/src/settings/color/mod.rs +++ b/tabled/src/settings/color/mod.rs @@ -181,6 +181,10 @@ impl Color { /// /// Notice that the colors are constants so you can't combine them. pub const BOLD: Self = Self::new_static("\u{1b}[1m", "\u{1b}[22m"); + /// A color representation. + /// + /// Notice that the colors are constants so you can't combine them. + pub const UNDERLINE: Self = Self::new_static("\u{1b}[4m", "\u{1b}[24m"); } impl Color { @@ -246,6 +250,28 @@ impl Color { { std::convert::TryFrom::try_from(text.as_ref()).unwrap() } + + /// Create a 24 bit foreground color with RGB + pub fn rgb_fg(r: u8, g: u8, b: u8) -> Self { + Self { + inner: ColorInner::Buf(ANSIBuf::new( + format!("\u{1b}[38;2;{};{};{}m", r, g, b), + "\u{1b}[39m", + )), + } + } + + /// Create a 24 bit background color with RGB. + /// + /// The terminal need to support the escape sequence + pub fn rgb_bg(r: u8, g: u8, b: u8) -> Self { + Self { + inner: ColorInner::Buf(ANSIBuf::new( + format!("\u{1b}[48;2;{};{};{}m", r, g, b), + "\u{1b}[49m", + )), + } + } } impl Default for Color { @@ -422,4 +448,33 @@ mod tests { Ok(Color::new("\u{1b}[31m\u{1b}[42m", "\u{1b}[39m\u{1b}[49m")) ); } + + #[test] + fn test_rgb_color() { + assert_eq!( + Color::rgb_bg(255, 255, 255), + Color::new("\u{1b}[48;2;255;255;255m", "\u{1b}[49m") + ); + assert_eq!( + Color::rgb_bg(0, 255, 128), + Color::new("\u{1b}[48;2;0;255;128m", "\u{1b}[49m") + ); + + assert_eq!( + Color::rgb_fg(0, 255, 128), + Color::new("\u{1b}[38;2;0;255;128m", "\u{1b}[39m") + ); + assert_eq!( + Color::rgb_fg(255, 255, 255), + Color::new("\u{1b}[38;2;255;255;255m", "\u{1b}[39m") + ); + + assert_eq!( + Color::rgb_bg(255, 255, 255) | Color::rgb_fg(0, 0, 0), + Color::new( + "\u{1b}[48;2;255;255;255m\u{1b}[38;2;0;0;0m", + "\u{1b}[49m\u{1b}[39m" + ) + ) + } } From 000a62448edfbe127f8f47ebe21026688ca8cbaa Mon Sep 17 00:00:00 2001 From: Tristan Lepine Date: Wed, 6 Nov 2024 17:47:10 -0500 Subject: [PATCH 2/3] added simple animated example to display alternating colored row using rgb colors --- tabled/examples/interactive.rs | 135 +++++++++++++++++++++++++++++++++ 1 file changed, 135 insertions(+) create mode 100644 tabled/examples/interactive.rs diff --git a/tabled/examples/interactive.rs b/tabled/examples/interactive.rs new file mode 100644 index 00000000..30cd868a --- /dev/null +++ b/tabled/examples/interactive.rs @@ -0,0 +1,135 @@ +use std::{thread::sleep, time::Duration}; + +use tabled::{ + settings::{ + object::{ObjectIterator, Rows}, + style::BorderColor, + themes::Colorization, + Color, Style, + }, + Table, +}; +use tabled_derive::Tabled; + +#[derive(Tabled, Clone, Debug)] +struct Item { + name: &'static str, + category: &'static str, + value: f64, +} + +const CLEAR: &str = "\u{1b}[2J"; + +type Step = Box (u64, &mut Table)>; + +const ITEM_LIST: &[Item] = &[ + Item { + name: "Light Bulb", + category: "Household", + value: 3.67, + }, + Item { + name: "Toothbrush", + category: "Bathroom", + value: 0.99, + }, + Item { + name: "Tire", + category: "Vehicle", + value: 230.0, + }, +]; + +const TIME: u64 = 400; + +fn main() { + let mut table = Table::new(ITEM_LIST); + let steps: Vec = vec![ + Box::new(|t: &mut Table| (TIME, t.with(Style::blank()))), + Box::new(|t: &mut Table| { + ( + TIME, + t.with(Colorization::rows([ + Color::rgb_bg(0, 0, 0) | Color::rgb_fg(255, 255, 255), + Color::rgb_bg(255, 255, 255) | Color::rgb_fg(0, 0, 0), + ])), + ) + }), + Box::new(|t: &mut Table| { + ( + TIME, + t.with(Colorization::exact([Color::UNDERLINE], Rows::first())), + ) + }), + Box::new(|t: &mut Table| { + ( + TIME, + t.modify( + Rows::new(1..).step_by(2), + BorderColor::new().left(Color::rgb_bg(255, 255, 255)), + ) + .modify( + Rows::new(2..).step_by(2), + BorderColor::new().left(Color::rgb_bg(0, 0, 0)), + ), + ) + }), + Box::new(|t: &mut Table| { + ( + TIME, + t.with(Colorization::exact( + [ + Color::rgb_bg(0, 0, 0) | Color::rgb_fg(255, 255, 255), + Color::rgb_bg(255, 255, 255) | Color::rgb_fg(0, 0, 0), + ], + Rows::new(1..), + )) + .modify( + Rows::new(1..).step_by(2), + BorderColor::new().left(Color::rgb_bg(0, 0, 0)), + ) + .modify( + Rows::new(2..).step_by(2), + BorderColor::new().left(Color::rgb_bg(255, 255, 255)), + ), + ) + }), + Box::new(|t: &mut Table| { + ( + 300, + t.with(Colorization::exact( + [ + Color::rgb_bg(128, 128, 255) | Color::rgb_fg(0, 0, 0), + Color::rgb_bg(200, 100, 150) | Color::rgb_fg(0, 0, 0), + ], + Rows::new(1..), + )) + .modify( + Rows::new(1..).step_by(2), + BorderColor::new().left(Color::rgb_bg(128, 128, 255)), + ) + .modify( + Rows::new(2..).step_by(2), + BorderColor::new().left(Color::rgb_bg(200, 100, 150)), + ), + ) + }), + ]; + + run_steps(&mut table, &steps); +} + +fn run_steps(initial_table: &mut Table, steps: &[Step]) { + let mut t: u64; + let mut table = initial_table; + for step in steps { + println!("{}", CLEAR); + println!("{}", table); + (t, table) = step(table); + sleep(Duration::from_millis(t)); + } + + println!("{}", CLEAR); + println!("{}", table); + sleep(Duration::from_millis(1000)); +} From db175a611814954852cf580aecd53809f3d895b1 Mon Sep 17 00:00:00 2001 From: Tristan Lepine Date: Sun, 10 Nov 2024 21:08:05 -0500 Subject: [PATCH 3/3] added required features to interactive example --- tabled/Cargo.toml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tabled/Cargo.toml b/tabled/Cargo.toml index 0fe12450..4a1c875b 100644 --- a/tabled/Cargo.toml +++ b/tabled/Cargo.toml @@ -303,6 +303,12 @@ name = "theme" path = "examples/theme.rs" required-features = ["derive", "std"] +[[example]] +name = "interactive" +path = "example/interactive.rs" +required-features = ["derive"] + + [features] default = ["derive", "macros"] std = ["papergrid/std"]