Struct DumbLineByLineScreen

Source
pub struct DumbLineByLineScreen { /* private fields */ }
Expand description

A simple terminal / text-based “screen” update helper, which relies on DumbLineTemplate to format each “screen” lines

For example:

use std::collections::HashMap;
use rusty_dumb_tools::prelude::*;
let mut lbl_demo_screen = {
    /// template for the line that ends up like "|     ... wait ... loading 100% ...    |"
    let mut comps = dlt_comps![
        dltc!("description", align = 'C').set_truncate_indicator("..."),
        " |"
    ];
    let temp1 = DumbLineTemplate::new_fixed_width(40, &comps);

    /// template for the line that ends up like "| ........ |>>>>>>>>>>>>>>>>>>>>: 100% |"
    let mut comps = dlt_comps![
        "| ",
        ".".repeat(8),
        " |",
        dltc!("progress-bar"),
        ": ",
        dltc!("progress%", fixed_width = 4, align = 'R'),
        " |"
    ];
    let temp2 = DumbLineTemplate::new_fixed_width(40, &comps);

    let settings = LBLScreenSettings {
        top_line: Some("-".repeat(40)),  // the top line of the "screen"
        bottom_line: Some("-".repeat(40)),  // the bottom line of the "screen"
        ..LBLScreenSettings::default()
    };
    DumbLineByLineScreen::new(vec![temp1, temp2], settings)
};
println!("The following is the \"screen\":");
lbl_demo_screen.init();

// setup a map of values for the "screen"
let mut state = HashMap::<&str, String>::new();
let mut progress_done_percent = 100;
let progress_percent = format!("{}%", progress_done_percent);
let description = format!("... wait ... loading {} ...", progress_done_percent);
let progress_bar = ">".repeat(progress_done_percent / 5 as usize);
state.insert("description", description);
state.insert("progress-bar", progress_bar);
state.insert("progress%", progress_percent);

lbl_demo_screen.refresh(&state);  // update the "screen" according to the mapped values

hence, the above code will show:

The following is the "screen":
----------------------------------------
|     ... wait ... loading 100% ...    |
| ........ |>>>>>>>>>>>>>>>>>>>>: 100% |
----------------------------------------

Please refer to DumbLineTemplate for more details on the line formatting of the different lines of the “screen”; for a fuller sample code, please refer to the “calculator” sub-demo of crate::demo::run_demo

Implementations§

Source§

impl DumbLineByLineScreen

Source

pub fn new( line_temps: Vec<DumbLineTemplate>, settings: LBLScreenSettings, ) -> Self

must call DumbLineByLineScreen::init after instantiation; note that printing will start at the current cursor position (likely should be start of a line); as long as the cursor position is not changed externally, DumbLineByLineScreen will know where to update which “screen” lines when DumbLineByLineScreen::refresh / DumbLineByLineScreen::refresh_for_keys is called

Source

pub fn init(&mut self)

call it once after instantiation; before call, make sure the cursor is positioned at the top of the screen

Source

pub fn refresh<T: LBLScreenMapValueTrait>(&self, value_mapper: &T) -> usize

refresh the screen; since lines are cached, refresh will not reprint any lines not changed; nevertheless, for a bit better performance, you can use DumbLineByLineScreen::refresh_for_keys to refresh only the lines that are affected by the given keys

e.g.

let mut state = HashMap::<&str, String>::new();
...
lbl_demo_screen.refresh(&state);

It returns the number of lines updated

Source

pub fn refresh_ex<T: Display, F: Fn(&str) -> Option<(T, u16)>>( &self, map_value_fn: F, ) -> usize

the same as DumbLineByLineScreen::refresh but with a ‘value mapper’ function

Source

pub fn refresh_for_keys<T: LBLScreenMapValueTrait>( &self, keys: &[&str], value_mapper: &T, ) -> usize

refresh the screen assuming only the values of the given keys changed; it will be a bit faster, but in general, simply useDumbLineByLineScreen::refresh to refresh the whole “screen”

Source

pub fn refresh_for_keys_ex<T: Display, F: Fn(&str) -> Option<(T, u16)>>( &self, keys: &[&str], map_value_fn: F, ) -> usize

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.