1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
|
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: String,
#[tabled(format("{}", self.category.join(",")))]
category: Vec<String>,
value: f64,
}
impl Item {
fn new(name: &str, category: &[&str], value: f64) -> Self {
Self {
name: name.to_owned(),
category: category.iter().map(ToString::to_string).collect(),
value,
}
}
}
fn main() {
let mut p = Pager::default();
p.append(|t| {
t.with(Style::blank());
});
p.append(|t| {
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),
]));
});
p.append(|t| {
t.with(Colorization::exact([Color::UNDERLINE], Rows::first()));
});
p.append(|t| {
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)),
);
});
p.append(|t| {
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)),
);
});
p.append(|t| {
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)),
);
});
let data = [
Item::new("Light Bulb", &["Household"], 3.67),
Item::new("Toothbrush", &["Household", "Bathroom"], 3.67),
Item::new("Tire", &["Vehicle"], 299.0),
];
let table = Table::new(data);
p.render(table);
}
type Step = Box<dyn Fn(&mut Table) -> u64>;
#[derive(Default)]
struct Pager {
pages: Vec<Step>,
}
impl Pager {
fn append<F>(&mut self, f: F)
where
F: Fn(&mut Table) + 'static,
{
self.append_timed(f, 400);
}
fn append_timed<F>(&mut self, f: F, time_ms: u64)
where
F: Fn(&mut Table) + 'static,
{
self.pages.push(step(f, time_ms));
}
fn render(&self, mut table: Table) {
run_steps(&mut table, &self.pages)
}
}
fn step<F>(f: F, delay_ms: u64) -> Step
where
F: Fn(&mut Table) + 'static,
{
Box::new(move |t| {
(f)(t);
delay_ms
})
}
fn run_steps(table: &mut Table, steps: &[Step]) {
const CLEAR: &str = "\u{1b}[2J";
let mut t: u64;
for step in steps {
println!("{}", CLEAR);
t = (step)(table);
println!("{}", table);
sleep(Duration::from_millis(t));
}
}
|