Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat/rgb-color #437

Merged
merged 3 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions tabled/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,12 @@ name = "theme"
path = "examples/theme.rs"
required-features = ["derive", "std"]

[[example]]
name = "interactive"
path = "example/interactive.rs"
required-features = ["derive"]
Comment on lines +306 to +309
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking you're new in rust,
you had figured it pretty fast 😄

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's been misspelled "examples/interactive.rs"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah crap, since you've already merged it, do I commit on my branch and create a new pull request with the changes?

I was thinking you're new in rust

Lol, this is the first library I'm actually contributing to, thanks for the help and patience:). The things you wrote was actually my reference.



[features]
default = ["derive", "macros"]
std = ["papergrid/std"]
Expand Down
135 changes: 135 additions & 0 deletions tabled/examples/interactive.rs
Original file line number Diff line number Diff line change
@@ -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<dyn Fn(&mut Table) -> (u64, &mut Table)>;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh kind of hard to understand right?)

Probably will need to refactor it afterall

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. I could put the steps in its own struct to try and make it a little bit cleaner if that's what you meant.

Like:

struct StepResult {
  // could have `Duration` directly here instead of `u64` milliseconds
  duration: u64,
  table: &mut Table,
}

struct Step {
  action: Box<dyn Fn(&mut Table) -> StepResult>
}

Not sure if this is syntactically correct, I haven't tried it.


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<Step> = 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));
}
55 changes: 55 additions & 0 deletions tabled/src/settings/color/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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"
)
)
}
}
Loading