File: multiline.rs

package info (click to toggle)
rust-nutmeg 0.1.4-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 352 kB
  • sloc: makefile: 2
file content (42 lines) | stat: -rw-r--r-- 1,031 bytes parent folder | download
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
//! Multi-line progress.

use std::thread::sleep;
use std::time::{Duration, Instant};

use yansi::Paint;

struct Model {
    i: usize,
    start: Instant,
}

impl nutmeg::Model for Model {
    fn render(&mut self, _width: usize) -> String {
        let long_text = self.i.to_string().repeat(40 - self.i);
        format!(
            "  count: {}\n    bar: {}\nelapsed: {:.1}s\n  blink: {}\n   long: {}",
            self.i,
            "*".repeat(self.i),
            self.start.elapsed().as_secs_f32(),
            if self.i % 2 == 0 {
                Paint::red("XXX")
            } else {
                Paint::yellow("XXX")
            },
            long_text,
        )
    }
}

fn main() {
    let options = nutmeg::Options::default().update_interval(Duration::from_millis(50));
    let model = Model {
        i: 0,
        start: Instant::now(),
    };
    let view = nutmeg::View::new(model, options);
    for _i in 1..=40 {
        view.update(|state| state.i += 1);
        sleep(Duration::from_millis(200));
    }
}