-
Notifications
You must be signed in to change notification settings - Fork 87
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
feat/rgb-color #437
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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)>; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)); | ||
} |
There was a problem hiding this comment.
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 😄
There was a problem hiding this comment.
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"
There was a problem hiding this comment.
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?
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.