pub struct Region { /* private fields */ }Expand description
Any Block (a table, a tree, a few lines of status) redrawn in place at a steady rate
while work goes on, for displays a Live step list doesn’t fit. The view
is a closure that builds the block afresh each time from whatever state it shares.
When it finishes it’s cleared away, unless keep asks for the last frame
to stay. Lines logged while it runs print above it and stay.
use std::sync::{Arc, Mutex};
use tinsel::{Region, Table};
let done = Arc::new(Mutex::new(Vec::<String>::new()));
let shown = done.clone();
let region = Region::new(move || {
let done = shown.lock().unwrap();
let mut table = Table::new(["service", "status"]);
for name in done.iter() {
table = table.row([name.as_str(), "ready"]);
}
table.lines()
})
.keep();
for name in ["api", "web", "worker"] {
// … start it
done.lock().unwrap().push(name.to_string());
region.log(format!("started {name}"));
}
region.finish();On a terminal it redraws (on stdout, or the Target). Anywhere else it
prints logged lines as they come and nothing else while it runs, then the last frame once
if it’s kept. With Output::Json logged lines are "log" events
and a kept last frame is one "block" event; with Output::Quiet
nothing prints.
Implementations§
Source§impl Region
impl Region
Sourcepub fn new(view: impl FnMut() -> Block + Send + 'static) -> Self
pub fn new(view: impl FnMut() -> Block + Send + 'static) -> Self
Start a region drawing view, ten times a second unless fps says
otherwise. It starts straight away.
Sourcepub fn headless(view: impl FnMut() -> Block + Send + 'static) -> Self
pub fn headless(view: impl FnMut() -> Block + Send + 'static) -> Self
A region that never draws, for checking what it shows in tests: take a
snapshot whenever you like.
use tinsel::{Block, Region};
tinsel::set_color_mode(tinsel::ColorMode::None);
let region = Region::headless(|| Block::from(vec![" 3 of 5 ready".to_string()]));
region.log("api is up");
assert_eq!(region.snapshot().plain(), "api is up\n 3 of 5 ready");Sourcepub fn fps(self, fps: u32) -> Self
pub fn fps(self, fps: u32) -> Self
Redraw this many times a second (default 10), kept between 1 and 240. The view is called once a frame, so a slow one wants a lower rate.
let region = tinsel::Region::new(|| tinsel::pairs(&[("queue", "12 jobs")])).fps(4);Sourcepub fn keep(self) -> Self
pub fn keep(self) -> Self
Leave the last frame on screen when the region finishes, rather than clearing it away. Off a terminal, the last frame is printed once at the end.
let region = tinsel::Region::new(|| tinsel::pairs(&[("status", "ready")])).keep();Sourcepub fn log(&self, line: impl AsRef<str>)
pub fn log(&self, line: impl AsRef<str>)
Print a line above the region, which carries on drawing underneath it. The line is
printed as given, so style it as you like (a Tone line, say). Safe to
call from any thread.
let region = tinsel::Region::new(|| tinsel::pairs(&[("status", "starting")]));
region.log(tinsel::Tone::Success.line("api is up"));Sourcepub fn refresh(&self)
pub fn refresh(&self)
Redraw now rather than at the next frame, after something changed that should show straight away.