4 releases

0.2.2 Oct 3, 2020
0.2.1 Sep 8, 2019
0.2.0 Sep 8, 2019
0.1.0 Aug 25, 2019

#1098 in Encoding

MIT license

18KB
405 lines

ornament

ornament docs

A helper to create decorated text.

Keys features

  • Decoration language agnostic.
  • Easy to use builder.
  • Small and reusable API.

Details

See the documentation for more details and examples: https://docs.rs/ornament


lib.rs:

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

Dependencies

~0–295KB