Skip to content

Commit

Permalink
Add an example of DisplayModel
Browse files Browse the repository at this point in the history
Based on the earlier example
  • Loading branch information
sourcefrog committed Dec 14, 2023
1 parent 76514b7 commit 921b291
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
20 changes: 20 additions & 0 deletions examples/display_model.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//! Anything that implements Display can be wrapped in a DisplayModel.
use std::fs::read_dir;
use std::io;
use std::thread::sleep;
use std::time::Duration;

use nutmeg::models::DisplayModel;

fn main() -> io::Result<()> {
let options = nutmeg::Options::default();
let model = DisplayModel::new(String::new());
let view = nutmeg::View::new(model, options);
for p in read_dir(".")? {
let dir_entry = p?;
view.update(|DisplayModel(message)| *message = dir_entry.path().display().to_string());
sleep(Duration::from_millis(300));
}
Ok(())
}
7 changes: 7 additions & 0 deletions src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,13 @@ where
#[derive(Debug)]
pub struct DisplayModel<T: Display + Debug>(pub T);

impl<T: Display + Debug> DisplayModel<T> {
/// Construct a new model holding a value implementing `Display`.
pub fn new(value: T) -> DisplayModel<T> {
DisplayModel(value)
}
}

impl<T: Display + Debug> Model for DisplayModel<T> {
fn render(&mut self, _width: usize) -> String {
format!("{}", self.0)
Expand Down

0 comments on commit 921b291

Please sign in to comment.