Trait nutmeg::Model

source ·
pub trait Model {
    // Required method
    fn render(&mut self, width: usize) -> String;

    // Provided method
    fn final_message(&mut self) -> String { ... }
}
Expand description

An application-defined type that holds whatever state is relevant to the progress bar, and that can render it into one or more lines of text.

Required Methods§

source

fn render(&mut self, width: usize) -> String

Render this model into a string to draw on the console.

This is called by the View when it wants to repaint the screen after View::update was called.

Future versions of this library may call this function from a different thread.

The width argument advises the model rendering code of the width of the terminal. The render implementation may make us of this to, for example, draw a full-width progress bar, or to selectively truncate sections within the line.

The model may also ignore the width parameter and return a string of any width, in which case it will be truncated to fit on the screen.

The rendered version may contain ANSI escape sequences for coloring, etc, but should not move the cursor.

Lines are separarated by \n. If there is a final \n it is ignored.

Example
struct Model { i: usize, total: usize }

impl nutmeg::Model for Model {
    fn render(&mut self, _width: usize) -> String {
        format!("phase {}/{}", self.i, self.total)
    }
}

Provided Methods§

source

fn final_message(&mut self) -> String

Optionally render a final message when the view is finished.

For example this could be used to print the amount of work done after the work is complete.

By default this prints nothing.

The final message may contain ANSI styling and may be multiple lines, but it should not have a final newline, unless a trailing blank line is desired.

This is called by View::finish or when the view is dropped. The final message is not printed when the view is abandoned by View::abandon.

Implementors§

source§

impl Model for LinearModel

source§

impl Model for StringPair

source§

impl Model for UnboundedModel

source§

impl<T> Model for Twhere T: Display,

Blanket implementation of Model for Display.

self is converted to a display string without regard for the terminal width.

This allows direct use of e.g. a String or integer as a model for very basic progress indications.

use nutmeg::{Options, View};

let view = View::new(0, Options::default());
view.update(|model| *model += 1);
source§

impl<T, R> Model for BasicModel<T, R>where R: FnMut(&mut T) -> String,