File: print_partial_lines.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 (51 lines) | stat: -rw-r--r-- 1,215 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
43
44
45
46
47
48
49
50
51
//! Partial lines can be printed, and the progress bar does not overwrite them.

use std::io::{self, Write};
use std::thread::sleep;
use std::time::Duration;

struct Model {
    i: usize,
    legal: bool,
}

impl nutmeg::Model for Model {
    fn render(&mut self, _width: usize) -> String {
        assert!(self.legal);
        format!("progress: {}", self.i)
    }
}

fn zz() {
    sleep(Duration::from_millis(300));
}

fn main() -> io::Result<()> {
    let options = nutmeg::Options::default();
    let model = Model { i: 0, legal: true };
    let mut view = nutmeg::View::new(model, options);
    for i in 1..=5 {
        view.update(|model| model.i += 1);
        zz();
        write!(view, "partial output {i}... ")?;
        zz();
        view.update(|model| model.i += 1);
        zz();
        write!(view, "more... ")?;
        zz();
        view.update(|model| model.i += 1);
        zz();
        write!(view, "more... ")?;
        zz();
        view.update(|model| model.i += 1);
        zz();
        writeln!(view, "done!")?;
        view.update(|model| model.i += 1);
        zz();
        for _ in 0..4 {
            view.update(|model| model.i += 1);
            zz();
        }
    }
    Ok(())
}