render_tree/lib.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
//! This module defines a tree structure that you can use to build documents
//! to render with color.
//!
//! There are three kinds of nodes in the Render Tree:
//!
//! - Lines, which represent content followed by a newline
//! - Sections, which represent named content that can be targeted by
//! style rules.
//! - Components, which are parameterized chunks of the Render Tree
//!
//! The easiest way to build a render tree is using the `tree!` macro:
//!
//! ```
//! #[macro_use]
//! extern crate render_tree;
//! extern crate termcolor;
//! use render_tree::{Document, Line, RenderComponent};
//! use termcolor::StandardStream;
//!
//! fn main() -> std::io::Result<()> {
//! let world = "world";
//!
//! let document = tree! {
//! <Line as {
//! "Hello" {world}
//! }>
//!
//! <Line as {
//! "Goodbye" {world}
//! }>
//! };
//!
//! document.write()
//! }
//! ```
//!
//! This will print out:
//!
//! ```text
//! Hello world
//! Goodbye world
//!
//! ```
//!
//! You can use sections and stylesheets to colorize the output:
//!
//! ```
//! #[macro_use]
//! extern crate render_tree;
//! extern crate termcolor;
//! use render_tree::{Document, Line, RenderComponent, Section, Stylesheet};
//! use render_tree::prelude::*;
//! use termcolor::StandardStream;
//!
//! fn main() -> std::io::Result<()> {
//! let world = "world";
//!
//! let document = tree! {
//! <Line as {
//! <Section name="hello" as { "Hello" }>
//! {world}
//! }>
//!
//! <Line as {
//! <Section name="goodbye" as { "Goodbye"}>
//! {world}
//! }>
//! };
//!
//! let stylesheet = Stylesheet::new()
//! .add("hello", "fg: blue")
//! .add("goodbye", "fg: red");
//!
//! document.write_styled(&stylesheet)
//! }
//! ```
//!
//! This will print out:
//!
//! ```text
//! Hello world
//! Goodbye world
//!
//! ```
//!
//! with the word "Hello" colored blue and "Goodbye" colored red.
//!
//! You can nest sections, which can be targeted by style paths:
//!
//!
//! ```
//! #[macro_use]
//! extern crate render_tree;
//! extern crate termcolor;
//! use render_tree::{Document, Line, RenderComponent, Section, Stylesheet};
//! use render_tree::prelude::*;
//! use termcolor::StandardStream;
//!
//! fn main() -> std::io::Result<()> {
//! let world = "world";
//!
//! let document = tree! {
//! <Line as {
//! <Section name="hello-world" as {
//! <Section name="greeting" as { "Hello" }>
//! {world}
//! }>
//! }>
//!
//! <Line as {
//! "Some content in the middle here"
//! }>
//!
//! <Line as {
//! <Section name="goodbye-world" as {
//! <Section name="greeting" as { "Goodbye" }>
//! {world}
//! }>
//! }>
//! };
//!
//! let stylesheet = Stylesheet::new()
//! .add("** greeting", "weight: bold")
//! .add("hello-world greeting", "fg: red")
//! .add("goodbye-world greeting", "fg: blue");
//!
//! document.write_styled(&stylesheet)
//! }
//! ```
//!
//! This will print out:
//!
//! ```text
//! Hello world
//! Some content in the middle here
//! Goodbye world
//!
//! ```
//!
//! with the "Hello world" and "Goodbye world" bolded, the word "Hello" colored
//! red (and bolded, of course), and the word "Goodbye" colored red (and
//! bolded).
//!
//! Globs (`**`) in a rule are superseded by stars (`*`), which are supersede by
//! literal names. Globs match zero or more section names and stars match exactly
//! one section name.
//!
//! # Using without the `tree!` macro
//!
//! It's also easy to build a Render Tree without the macro. Repeating the previous
//! example without the macro and without the string DSL for the stylesheet:
//!
//! ```
//! #[macro_use]
//! extern crate render_tree;
//! extern crate termcolor;
//! use render_tree::{
//! Color,
//! Document,
//! Line,
//! Render,
//! RenderComponent,
//! Section,
//! Selector,
//! Segment,
//! Style,
//! Stylesheet
//! };
//! use termcolor::StandardStream;
//!
//! fn main() -> std::io::Result<()> {
//! let world = "world";
//!
//! let document = Document::with(
//! Line(
//! Section("hello-world", |doc|
//! doc.add(
//! Section("greeting", |doc| doc.add("Hello").add(world))
//! )
//! )
//! )
//! ).add(
//! Line(
//! "Some content in the middle here"
//! )
//! ).add(
//! Line(
//! Section("goodbye-world", |doc|
//! doc.add(
//! Section("greeting", |doc| doc.add("Goodbye").add(world))
//! )
//! )
//! )
//! );
//!
//! let stylesheet = Stylesheet::new()
//! .add(Selector::glob().add("greeting"), Style::new().bold())
//! .add(Selector::name("hello-world").add("greeting"), Style::new().fg(Color::Red))
//! .add(Selector::name("goodbye-world").add("greeting"), Style::new().fg(Color::Blue));
//!
//! document.write_styled(&stylesheet)
//! }
//! ```
#[macro_use]
pub mod macros;
mod component;
mod debug;
pub mod document;
mod helpers;
pub mod prelude;
mod render;
pub mod stylesheet;
pub(crate) mod utils;
pub use self::component::*;
pub use self::document::*;
pub use self::helpers::*;
pub use self::render::*;
pub use self::stylesheet::{Color, Segment, Selector, Style, Stylesheet};