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, &lt_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 string
  • dltc!("label", fixed_width = 6, align = 'L'):

you may want to consider the helper crate::lblscreen::DumbLineByLineScreen for coding a simple terminal / text-based “screen”

Implementations§

source§

impl DumbLineTemplate

source

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 line
  • max_width - the maximum width of the line
  • components - the template components of the line, which can be created using the macro dlt_comps!

also see DumbLineTemplate::new_fixed_width

source

pub fn new_fixed_width( fixed_width: u16, components: &[LineTempComp] ) -> DumbLineTemplate

the same as DumbLineTemplate::new but with fixed width

source

pub fn min_width(&self) -> u16

source

pub fn max_width(&self) -> u16

source

pub fn scan_for_keys(&self) -> HashSet<String>

a helper function that the help to scan for the keys involved in formatting the line

source

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();
source

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)

Trait Implementations§

source§

impl Debug for DumbLineTemplate

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

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>,

§

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>,

§

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.