Skip to main content

Region

Struct Region 

Source
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

Source

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.

Source

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");
Source

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);
Source

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();
Source

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"));
Source

pub fn refresh(&self)

Redraw now rather than at the next frame, after something changed that should show straight away.

Source

pub fn snapshot(&self) -> Block

What the region shows right now: anything logged, then the view. Most useful with headless regions in tests.

Source

pub fn finish(self)

Stop drawing: clear the region away, or leave its last frame if it’s kept. Dropping a Region does the same.

Trait Implementations§

Source§

impl Debug for Region

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Drop for Region

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

Auto Trait Implementations§

§

impl !RefUnwindSafe for Region

§

impl !UnwindSafe for Region

§

impl Forget for Region

§

impl Freeze for Region

§

impl Move for Region

§

impl Send for Region

§

impl Sync for Region

§

impl Unpin for Region

§

impl UnsafeUnpin for Region

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.