-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
4 changed files
with
100 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
//! This model repeatedly generates the same text: it's a counter that | ||
//! only shows the hundreds. | ||
//! | ||
//! Nutmeg avoids redrawing the bar on every update to avoid flickering | ||
//! (on terminals that don't handle this well themselves.) | ||
use std::thread::sleep; | ||
use std::time::Duration; | ||
|
||
struct Model { | ||
i: usize, | ||
} | ||
|
||
impl nutmeg::Model for Model { | ||
fn render(&mut self, _width: usize) -> String { | ||
format!("count: {}", self.i / 100) | ||
} | ||
} | ||
|
||
fn main() { | ||
let options = nutmeg::Options::default(); | ||
let view = nutmeg::View::new(Model { i: 0 }, options); | ||
for _i in 1..=5000 { | ||
view.update(|state| state.i += 1); | ||
sleep(Duration::from_millis(5)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
//! Test that Nutmeg avoids redrawing the same text repeatedly. | ||
use std::time::Duration; | ||
|
||
use nutmeg::{Destination, Options, View}; | ||
|
||
struct Hundreds(usize); | ||
|
||
impl nutmeg::Model for Hundreds { | ||
fn render(&mut self, _width: usize) -> String { | ||
format!("hundreds={}", self.0 / 100) | ||
} | ||
} | ||
|
||
#[test] | ||
fn identical_output_suppressed() { | ||
let options = Options::default() | ||
.destination(Destination::Capture) | ||
.update_interval(Duration::ZERO); | ||
let view = View::new(Hundreds(0), options); | ||
let output = view.captured_output(); | ||
|
||
for i in 0..200 { | ||
// We change the model, but not in a way that will change what's displayed. | ||
view.update(|model| model.0 = i); | ||
} | ||
view.abandon(); | ||
|
||
// No erasure commands, just a newline after the last painted view. | ||
assert_eq!( | ||
output.lock().as_str(), | ||
"\x1b[?7l\x1b[0Jhundreds=0\x1b[1G\x1b[?7l\x1b[0Jhundreds=1\n" | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters