pub struct Formatter { /* private fields */ }
Expand description
A Formatter
takes a bunch of key/value pairs and interpolates them into a
mimi format string.
Implementations§
source§impl Formatter
impl Formatter
sourcepub fn keys(&self) -> &HashSet<String>
pub fn keys(&self) -> &HashSet<String>
The names of all the variables in the format string.
Examples
let formatter: mimi::Formatter = "$foo $foo ${bar}baz".parse().unwrap();
let expected: HashSet<String> = hashset!["foo".into(), "bar".into()];
assert_eq!(formatter.keys(), &expected);
sourcepub fn ansi<'a, M: Index<&'a str, Output = String>>(
&'a self,
values: &M
) -> String
pub fn ansi<'a, M: Index<&'a str, Output = String>>(
&'a self,
values: &M
) -> String
Formats the given values using ANSI terminal codes.
sourcepub fn spans<'a, M: Index<&'a str, Output = String>>(
&'a self,
values: &M
) -> Box<dyn Iterator<Item = (String, Style)>>
pub fn spans<'a, M: Index<&'a str, Output = String>>(
&'a self,
values: &M
) -> Box<dyn Iterator<Item = (String, Style)>>
Yields the text of each leaf node under root
with variables
substituted by looking up in values
and using base
as the base of
each style. Combined with a function that can take a String
and a
Style
and format them, this gives you the ability to work with
arbitrary output formats.
Returns an iterator over (text, style) pairs. We do not guarantee that the representation is minimal (in that it’s possible for there to be adjacent pairs with identical styles).
Examples
use std::collections::HashMap;
let formatter: mimi::Formatter = "lit %[red]{fmt} $var".parse().unwrap();
let mut values = HashMap::new();
values.insert("var", "value".to_owned());
let spans: Vec<_> = formatter.spans(&values).collect();
let red = mimi::Style { foreground: Some(mimi::Color::Red), ..mimi::Style::default() };
assert_eq!(spans, vec![
("lit ".into(), mimi::Style::default()),
("fmt".into(), red),
// Note that the next two might get merge in some future version.
(" ".into(), mimi::Style::default()),
("value".into(), mimi::Style::default()),
]);