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
impl DumbLineByLineScreen
Sourcepub fn new(
line_temps: Vec<DumbLineTemplate>,
settings: LBLScreenSettings,
) -> Self
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
Sourcepub fn init(&mut self)
pub fn init(&mut self)
call it once after instantiation; before call, make sure the cursor is positioned at the top of the screen
Sourcepub fn refresh<T: LBLScreenMapValueTrait>(&self, value_mapper: &T) -> usize
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
Sourcepub fn refresh_ex<T: Display, F: Fn(&str) -> Option<(T, u16)>>(
&self,
map_value_fn: F,
) -> usize
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
Sourcepub fn refresh_for_keys<T: LBLScreenMapValueTrait>(
&self,
keys: &[&str],
value_mapper: &T,
) -> usize
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”