Skip to main content

Live

Struct Live 

Source
pub struct Live { /* private fields */ }
Expand description

A list of steps redrawn in place as they run.

Steps can run one after another or at the same time: Step handles are cheap to clone and can be moved to other threads, and every active step animates on its own. Steps can have sub-steps (Step::step), and log prints a line above the display without disturbing it.

A step is pending (○) until it starts, then ends done (●), with a warning (▲) or failed (✕). It can also be skipped (–), cancelled (⊘), or left waiting on someone (◌), and a finished step can start again.

Beyond spinners and timings, a Live display does three things on its own:

  • It learns. Each step’s duration is remembered between runs (see remember_as). Next time, a step that would only have had a spinner gets a progress bar and an estimate: usually 42s · 18s left, or slower than usual.
  • It notices when you look away. In terminals that report window focus, animation slows while you’re in another window. If a run of 10 seconds or more finishes while you’re away, you get a desktop notification (notify_after), and when you come back, the steps that finished meanwhile twinkle once so you can see them.
  • It stays out of the way of typing. Keys pressed during the run don’t scribble over the display; they’re handed back to the shell when it ends, where the system allows it. Linux 6.2 and later doesn’t by default (dev.tty.legacy_tiocsti), so there they’re lost.

It draws on stdout, or stderr after set_target. When that isn’t a terminal it does none of this and prints one plain line per finished step, so CI logs stay readable. With Output::Json every change is a JSON event instead, and with Output::Quiet only failures print.

Implementations§

Source§

impl Live

Source

pub fn new(title: impl Into<String>, subject: impl Into<String>) -> Self

A live display headed title subject, e.g. deploy my-app → production: what’s being done, and to what.

Nothing is drawn until the first step or log, so everything chosen with the builder methods (fps, remember_as, spinner and the rest) holds from the first frame. Its clock starts here.

Examples found in repository?
examples/deploy.rs (line 15)
11fn main() {
12    let fail = std::env::args().any(|a| a == "--fail");
13
14    // declaring every step up front shows the whole plan from the first frame
15    let live = Live::new("deploy", "my-app → production");
16    let build = live.step("Build");
17    let upload = live.step("Upload");
18    let release = live.step("Release");
19
20    build.start();
21    for file in ["main.rs", "config.rs", "assets"] {
22        build.detail(format!("compiling {file}"));
23        sleep(Duration::from_millis(400));
24    }
25    build.done();
26
27    upload.start();
28    for i in 1..=40 {
29        let f = i as f32 / 40.0;
30        upload.progress(f, format!("{:.1} / 12.0 MB", f * 12.0));
31        sleep(Duration::from_millis(40));
32    }
33    upload.done();
34
35    release.start();
36    sleep(Duration::from_millis(800));
37    if fail {
38        release.fail("the health check timed out after 30s");
39        live.finish_with_card(Card::failure("release failed").kv("logs", "my-app logs --since 5m"));
40        std::process::exit(1);
41    }
42    release.done();
43
44    let took = format!("in {:.1}s", live.elapsed().as_secs_f32());
45    live.finish_with_card(
46        Card::success("deployed")
47            .aside(&took)
48            .kv("url", "https://my-app.example.com"),
49    );
50}
More examples
Hide additional examples
examples/parallel.rs (line 18)
12fn main() {
13    tinsel::install_panic_hook();
14    if std::env::args().any(|a| a == "--json") {
15        tinsel::set_output(tinsel::Output::Json);
16    }
17
18    let live = Live::new("release", "v2.1.0");
19    let build = live.step("Build");
20    // sub-steps are drawn under their parent, and fold away once they've all passed
21    let targets: Vec<Step> = ["linux-x64", "linux-arm64", "macos-arm64"]
22        .into_iter()
23        .map(|t| build.step(t))
24        .collect();
25    let publish = live.step("Publish");
26
27    build.start();
28    // Step handles are cheap to clone and can move to other threads
29    scope(|s| {
30        for (i, target) in targets.iter().enumerate() {
31            s.spawn(move || {
32                target.start();
33                for stage in ["compiling", "linking", "stripping"] {
34                    target.detail(stage);
35                    sleep(Duration::from_millis(350 + 150 * i as u64));
36                }
37                if i == 2 {
38                    // a line worth keeping, printed above the display without disturbing it
39                    target.log("signed with Developer ID");
40                }
41                target.done();
42            });
43        }
44    });
45    build.done();
46
47    publish.start();
48    for i in 1..=30 {
49        let f = i as f32 / 30.0;
50        publish.progress(f, format!("{} of 3 archives", (f * 3.0).ceil()));
51        sleep(Duration::from_millis(40));
52    }
53    publish.done();
54
55    live.finish_with_card(Card::success("released").kv("tag", "v2.1.0"));
56}
Source

pub fn headless(title: impl Into<String>, subject: impl Into<String>) -> Self

A display that never draws, on a clock that only moves when you call advance, for checking what a run looks like in tests. Take a snapshot whenever you want to see it. It keeps no history and sends no notifications.

use std::time::Duration;
use tinsel::Live;

tinsel::set_color_mode(tinsel::ColorMode::None);
let live = Live::headless("deploy", "my-app");
let build = live.step("Build");
build.start();
live.advance(Duration::from_millis(1200));
build.done();
assert!(live.snapshot().plain().contains("●   Build"));
assert!(live.snapshot().plain().ends_with("1.2s"));
Source

pub fn fps(self, fps: u32) -> Self

Draw this many frames a second, rather than fps for every display.

let live = tinsel::Live::new("deploy", "my-app").fps(120);
Source

pub fn no_focus(self) -> Self

Don’t listen for focus reports: no calmer animation while you’re away, no notification when the run finishes, and stdin is left alone, for a tool that hands stdin to something else. set_focus or the TINSEL_NO_FOCUS environment variable does the same for every display.

let live = tinsel::Live::new("deploy", "my-app").no_focus();
Source

pub fn step(&self, label: impl Into<String>) -> Step

Add a pending step. Declaring every step up front shows the whole plan from the start.

Examples found in repository?
examples/deploy.rs (line 16)
11fn main() {
12    let fail = std::env::args().any(|a| a == "--fail");
13
14    // declaring every step up front shows the whole plan from the first frame
15    let live = Live::new("deploy", "my-app → production");
16    let build = live.step("Build");
17    let upload = live.step("Upload");
18    let release = live.step("Release");
19
20    build.start();
21    for file in ["main.rs", "config.rs", "assets"] {
22        build.detail(format!("compiling {file}"));
23        sleep(Duration::from_millis(400));
24    }
25    build.done();
26
27    upload.start();
28    for i in 1..=40 {
29        let f = i as f32 / 40.0;
30        upload.progress(f, format!("{:.1} / 12.0 MB", f * 12.0));
31        sleep(Duration::from_millis(40));
32    }
33    upload.done();
34
35    release.start();
36    sleep(Duration::from_millis(800));
37    if fail {
38        release.fail("the health check timed out after 30s");
39        live.finish_with_card(Card::failure("release failed").kv("logs", "my-app logs --since 5m"));
40        std::process::exit(1);
41    }
42    release.done();
43
44    let took = format!("in {:.1}s", live.elapsed().as_secs_f32());
45    live.finish_with_card(
46        Card::success("deployed")
47            .aside(&took)
48            .kv("url", "https://my-app.example.com"),
49    );
50}
More examples
Hide additional examples
examples/parallel.rs (line 19)
12fn main() {
13    tinsel::install_panic_hook();
14    if std::env::args().any(|a| a == "--json") {
15        tinsel::set_output(tinsel::Output::Json);
16    }
17
18    let live = Live::new("release", "v2.1.0");
19    let build = live.step("Build");
20    // sub-steps are drawn under their parent, and fold away once they've all passed
21    let targets: Vec<Step> = ["linux-x64", "linux-arm64", "macos-arm64"]
22        .into_iter()
23        .map(|t| build.step(t))
24        .collect();
25    let publish = live.step("Publish");
26
27    build.start();
28    // Step handles are cheap to clone and can move to other threads
29    scope(|s| {
30        for (i, target) in targets.iter().enumerate() {
31            s.spawn(move || {
32                target.start();
33                for stage in ["compiling", "linking", "stripping"] {
34                    target.detail(stage);
35                    sleep(Duration::from_millis(350 + 150 * i as u64));
36                }
37                if i == 2 {
38                    // a line worth keeping, printed above the display without disturbing it
39                    target.log("signed with Developer ID");
40                }
41                target.done();
42            });
43        }
44    });
45    build.done();
46
47    publish.start();
48    for i in 1..=30 {
49        let f = i as f32 / 30.0;
50        publish.progress(f, format!("{} of 3 archives", (f * 3.0).ceil()));
51        sleep(Duration::from_millis(40));
52    }
53    publish.done();
54
55    live.finish_with_card(Card::success("released").kv("tag", "v2.1.0"));
56}
Source

pub fn log(&self, text: impl AsRef<str>)

Print a line above the display, which carries on drawing underneath it: for output that’s worth keeping, like a warning from a compiler or a URL. Safe to call from any thread. In plain output it’s an ordinary line; as JSON, a "log" event.

let live = tinsel::Live::new("build", "my-app");
live.log("using cached dependencies from yesterday");
Source

pub fn spinner(self, spinner: Spinner) -> Self

Choose how running steps animate (default: Spinner::Sparkle).

Source

pub fn bar(self, bar: BarStyle) -> Self

Choose how progress bars and estimates are drawn (default: BarStyle::Line).

use tinsel::{BarStyle, Live};

let live = Live::new("render", "intro.mp4").bar(BarStyle::Waveform);
Source

pub fn remember_as(self, name: impl Into<String>) -> Self

The name step timings are remembered under. Defaults to the program’s own name, which is right unless several programs share a name or one program wants separate histories. Steps are told apart by this name, the display’s title, and their label.

Timings live in $XDG_CACHE_HOME/tinsel (or ~/.cache/tinsel), one small file per name. Only runs drawn on a terminal are recorded, and only steps that succeed. timings shows what’s been learned.

Source

pub fn no_history(self) -> Self

Don’t remember step timings, or show estimates from earlier runs. set_history or the TINSEL_NO_HISTORY environment variable does the same for every display.

Source

pub fn notify_after(self, after: Duration) -> Self

Send a desktop notification when the run finishes, if it took at least this long and the terminal window wasn’t focused at the time (default: 10 seconds).

Source

pub fn no_notify(self) -> Self

Never send a notification when the run finishes.

Source

pub fn elapsed(&self) -> Duration

How long since the display started, for a closing “done in 10.2s”.

Examples found in repository?
examples/deploy.rs (line 44)
11fn main() {
12    let fail = std::env::args().any(|a| a == "--fail");
13
14    // declaring every step up front shows the whole plan from the first frame
15    let live = Live::new("deploy", "my-app → production");
16    let build = live.step("Build");
17    let upload = live.step("Upload");
18    let release = live.step("Release");
19
20    build.start();
21    for file in ["main.rs", "config.rs", "assets"] {
22        build.detail(format!("compiling {file}"));
23        sleep(Duration::from_millis(400));
24    }
25    build.done();
26
27    upload.start();
28    for i in 1..=40 {
29        let f = i as f32 / 40.0;
30        upload.progress(f, format!("{:.1} / 12.0 MB", f * 12.0));
31        sleep(Duration::from_millis(40));
32    }
33    upload.done();
34
35    release.start();
36    sleep(Duration::from_millis(800));
37    if fail {
38        release.fail("the health check timed out after 30s");
39        live.finish_with_card(Card::failure("release failed").kv("logs", "my-app logs --since 5m"));
40        std::process::exit(1);
41    }
42    release.done();
43
44    let took = format!("in {:.1}s", live.elapsed().as_secs_f32());
45    live.finish_with_card(
46        Card::success("deployed")
47            .aside(&took)
48            .kv("url", "https://my-app.example.com"),
49    );
50}
Source

pub fn advance(&self, by: Duration)

Move a headless display’s clock on. Does nothing to a display on real time.

Source

pub fn snapshot(&self) -> Block

The display as it looks right now, settled (no twinkles mid-way), with the header and anything logged. Most useful with headless displays in tests.

Source

pub fn timeline(&self) -> Block

When each step ran, as bars on one time axis, so you can see which steps overlapped and where the time went.

  Build                 ━━━━━━━━━━━━━━━━━━              2.3s
    linux-arm64         ━━━━━━━━━━━━━━                  1.8s
    linux-x64           ━━━━━━━━━━━━━━━━━━              2.3s
  Upload                                  ━━━━━━━━━━━━  1.6s
                        ──────────────────────────────
                        0s                          3.9s
Source

pub fn finish_with_timeline(self)

Finish with a timeline under the steps.

Source

pub fn finish(self)

Stop drawing and leave the final state on screen. Dropping a Live does the same.

Source

pub fn finish_with_card(self, card: Card)

Finish with a summary Card under the steps.

use tinsel::{Card, Live};

let live = Live::new("deploy", "my-app");
let build = live.step("Build");
build.start();
build.done();
live.finish_with_card(Card::success("deployed").kv("url", "https://my-app.example.com"));
Examples found in repository?
examples/deploy.rs (line 39)
11fn main() {
12    let fail = std::env::args().any(|a| a == "--fail");
13
14    // declaring every step up front shows the whole plan from the first frame
15    let live = Live::new("deploy", "my-app → production");
16    let build = live.step("Build");
17    let upload = live.step("Upload");
18    let release = live.step("Release");
19
20    build.start();
21    for file in ["main.rs", "config.rs", "assets"] {
22        build.detail(format!("compiling {file}"));
23        sleep(Duration::from_millis(400));
24    }
25    build.done();
26
27    upload.start();
28    for i in 1..=40 {
29        let f = i as f32 / 40.0;
30        upload.progress(f, format!("{:.1} / 12.0 MB", f * 12.0));
31        sleep(Duration::from_millis(40));
32    }
33    upload.done();
34
35    release.start();
36    sleep(Duration::from_millis(800));
37    if fail {
38        release.fail("the health check timed out after 30s");
39        live.finish_with_card(Card::failure("release failed").kv("logs", "my-app logs --since 5m"));
40        std::process::exit(1);
41    }
42    release.done();
43
44    let took = format!("in {:.1}s", live.elapsed().as_secs_f32());
45    live.finish_with_card(
46        Card::success("deployed")
47            .aside(&took)
48            .kv("url", "https://my-app.example.com"),
49    );
50}
More examples
Hide additional examples
examples/parallel.rs (line 55)
12fn main() {
13    tinsel::install_panic_hook();
14    if std::env::args().any(|a| a == "--json") {
15        tinsel::set_output(tinsel::Output::Json);
16    }
17
18    let live = Live::new("release", "v2.1.0");
19    let build = live.step("Build");
20    // sub-steps are drawn under their parent, and fold away once they've all passed
21    let targets: Vec<Step> = ["linux-x64", "linux-arm64", "macos-arm64"]
22        .into_iter()
23        .map(|t| build.step(t))
24        .collect();
25    let publish = live.step("Publish");
26
27    build.start();
28    // Step handles are cheap to clone and can move to other threads
29    scope(|s| {
30        for (i, target) in targets.iter().enumerate() {
31            s.spawn(move || {
32                target.start();
33                for stage in ["compiling", "linking", "stripping"] {
34                    target.detail(stage);
35                    sleep(Duration::from_millis(350 + 150 * i as u64));
36                }
37                if i == 2 {
38                    // a line worth keeping, printed above the display without disturbing it
39                    target.log("signed with Developer ID");
40                }
41                target.done();
42            });
43        }
44    });
45    build.done();
46
47    publish.start();
48    for i in 1..=30 {
49        let f = i as f32 / 30.0;
50        publish.progress(f, format!("{} of 3 archives", (f * 3.0).ceil()));
51        sleep(Duration::from_millis(40));
52    }
53    publish.done();
54
55    live.finish_with_card(Card::success("released").kv("tag", "v2.1.0"));
56}

Trait Implementations§

Source§

impl Debug for Live

Source§

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

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

impl Drop for Live

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 !Freeze for Live

§

impl Forget for Live

§

impl Move for Live

§

impl RefUnwindSafe for Live

§

impl Send for Live

§

impl Sync for Live

§

impl Unpin for Live

§

impl UnsafeUnpin for Live

§

impl UnwindSafe for Live

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.