Struct rusty_dumb_tools::ltemp::DumbLineTemplate
source · pub struct DumbLineTemplate { /* private fields */ }
Expand description
a simple line template for formatting a line; it can be use for printing values as a line with some template.
example:
use rusty_dumb_tools::prelude::*;
use std::collections::HashMap;
// create the template components
let lt_comps = dlt_comps![
"| ",
dltc!("label", fixed_width = 6, align = 'L'),
" : ",
dltc!("value", align = 'R'),
" |"
];
// create the template
let ltemp = DumbLineTemplate::new_fixed_width(30, <_comps);
// format line1 from the template
let name = "Trevor Lee";
let map = HashMap::from([
("label", String::from("NAME")),
("value", name.to_string()),
]);
let line1 = ltemp.format(&map).unwrap();
// format line2 from the template
let map = HashMap::from([
("label", String::from("AGE")),
("value", String::from("<undisclosed>")),
]);
let line2 = ltemp.format(&map).unwrap();
assert_eq!(line1, "| NAME : Trevor Lee |");
assert_eq!(line2, "| AGE : <undisclosed> |");
notes:
"| "
: a fixed stringdltc!("label", fixed_width = 6, align = 'L')
:- a value-mapped component
- require a mapped value for key
label
when callingDumbLineTemplate::format
- also see the macros
dlt_comps!
anddltc!
you may want to consider the helper crate::lblscreen::DumbLineByLineScreen
for coding a simple terminal / text-based “screen”
Implementations§
source§impl DumbLineTemplate
impl DumbLineTemplate
sourcepub fn new(
min_width: u16,
max_width: u16,
components: &[LineTempComp]
) -> DumbLineTemplate
pub fn new( min_width: u16, max_width: u16, components: &[LineTempComp] ) -> DumbLineTemplate
please use the macro dlt_comps!
for construction of the components
min_width
- the minimum width of the linemax_width
- the maximum width of the linecomponents
- the template components of the line, which can be created using the macrodlt_comps!
also see DumbLineTemplate::new_fixed_width
sourcepub fn new_fixed_width(
fixed_width: u16,
components: &[LineTempComp]
) -> DumbLineTemplate
pub fn new_fixed_width( fixed_width: u16, components: &[LineTempComp] ) -> DumbLineTemplate
the same as DumbLineTemplate::new
but with fixed width
pub fn min_width(&self) -> u16
pub fn max_width(&self) -> u16
sourcepub fn scan_for_keys(&self) -> HashSet<String>
pub fn scan_for_keys(&self) -> HashSet<String>
a helper function that the help to scan for the keys involved in formatting the line
sourcepub fn format<T: LineTempCompMapValueTrait>(
&self,
value_mapper: &T
) -> Result<String, Box<dyn Error>>
pub fn format<T: LineTempCompMapValueTrait>( &self, value_mapper: &T ) -> Result<String, Box<dyn Error>>
based on the template and the input map of values, format and return a line;
for a more flexible way of formatting, try DumbLineTemplate::format_ex
e.g.
let map = HashMap::from([
...
]);
let line = ltemp.format(&map).unwrap();
sourcepub fn format_ex<T: Display, F: Fn(&str) -> Option<(T, u16)>>(
&self,
map_value_fn: F
) -> Result<String, Box<dyn Error>>
pub fn format_ex<T: Display, F: Fn(&str) -> Option<(T, u16)>>( &self, map_value_fn: F ) -> Result<String, Box<dyn Error>>
like format
but accept function that returns the mapped values; each mapped value is supposed to be a tuple of the value and its width
(note that for ASCII escaped string, the “visual” length can be different from the length of the string)