Expand description
A helper to create decorated text.
§Examples
This example creates a Text
and renders it following a simple Markdown-like.
use ornament::Decorator;
#[derive(Clone, Debug, PartialEq)]
enum Face {
Default,
Emphasis,
Strong,
}
impl Default for Face {
fn default() -> Self {
Face::Default
}
}
let text = Decorator::with_text("Text can be with emphasis or even strong.")
.set(Face::Emphasis, 17..25)
.set(Face::Strong, 34..40)
.build();
let rendered = text.render(|tf| {
match tf.face {
Face::Default => tf.text.to_owned(),
Face::Emphasis => format!("_{}_", tf.text),
Face::Strong => format!("**{}**", tf.text),
}
});
assert_eq!(rendered, "Text can be with _emphasis_ or even **strong**.");
// In some cases it can be easier to build it part by part.
let other_text = Decorator::new()
.append("Text can be with ")
.set_face(Face::Emphasis)
.append("emphasis")
.reset_face()
.append(" or even ")
.set_face(Face::Strong)
.append("strong")
.reset_face()
.append(".")
.build();
assert_eq!(other_text, text);
// Both methods can be combined together.
let another_other_text = Decorator::new()
.append("Text can be with ")
.set_face(Face::Emphasis)
.append("emphasis")
.reset_face()
.append(" or even strong.")
.set(Face::Strong, 34..40)
.build();
assert_eq!(another_other_text, text);
Structs§
- Decorator
- A helper type to build a
Text
instance. - Text
- A decorated text. This is a collection of
TextFragment
. - Text
Fragment - A piece of a decorated text.