render_tree/
document.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
use crate::stylesheet::WriteStyle;
use crate::Stylesheet;
use crate::{Combine, Render};
use std::io;
use termcolor::{ColorChoice, StandardStream, WriteColor};

#[derive(Debug, Clone)]
pub enum Node {
    Text(String),
    OpenSection(&'static str),
    CloseSection,
    Newline,
}

/// The `Document` is the root node in a render tree.
///
/// The [`tree!`] macro produces a `Document`, and you can also build
/// one manually.
///
/// ```
/// use render_tree::prelude::*;
/// use render_tree::Render;
///
/// fn main() -> std::io::Result<()> {
///     let document = Document::empty()
///         // You can add a `Line` to a document, with nested content
///         .add(Line(
///             // Strings implement `Render`
///             "Hello"
///         ))
///         .add(Line(
///             1.add(".").add(10)
///         ))
///         .add(Section("code", |doc|
///             doc.add("[E").add(1000).add("]")
///         ));
///
///     assert_eq!(document.to_string()?, "Hello\n1.10\n[E1000]");
///
///     Ok(())
/// }
/// ```
///
/// The above example is equivalent to this use of the [`tree!`] macro:
///
/// ```
/// #[macro_use]
/// extern crate render_tree;
/// use render_tree::prelude::*;
///
/// fn main() -> std::io::Result<()> {
///     let document = tree! {
///         <Line as { "Hello" }>
///         <Line as {
///             {1} "." {10}
///         }>
///         <Section name="code" as {
///             "[E" {1000} "]"
///         }>
///     };
///
///     assert_eq!(document.to_string()?, "Hello\n1.10\n[E1000]");
///
///     Ok(())
/// }
/// ```
#[derive(Debug, Clone)]
pub struct Document {
    // Make the inner tree optional so it's free to create empty documents
    tree: Option<Vec<Node>>,
}

impl Document {
    pub fn empty() -> Document {
        Document { tree: None }
    }

    pub fn with(renderable: impl Render) -> Document {
        renderable.render(Document::empty())
    }

    pub(crate) fn tree(&self) -> Option<&[Node]> {
        match &self.tree {
            None => None,
            Some(vec) => Some(&vec[..]),
        }
    }

    fn initialize_tree(&mut self) -> &mut Vec<Node> {
        if self.tree.is_none() {
            self.tree = Some(vec![]);
        }

        match &mut self.tree {
            Some(value) => value,
            None => unreachable!(),
        }
    }

    pub fn add(self, renderable: impl Render) -> Document {
        renderable.render(self)
    }

    pub(crate) fn add_node(mut self, node: Node) -> Document {
        self.initialize_tree().push(node);
        self
    }

    pub(crate) fn extend_nodes(mut self, other: Vec<Node>) -> Document {
        if other.len() > 0 {
            let tree = self.initialize_tree();

            for item in other {
                tree.push(item)
            }
        }

        self
    }

    pub(crate) fn extend(self, fragment: Document) -> Document {
        match (&self.tree, &fragment.tree) {
            (Some(_), Some(_)) => self.extend_nodes(fragment.tree.unwrap()),
            (Some(_), None) => self,
            (None, Some(_)) => fragment,
            (None, None) => self,
        }
    }

    pub fn write(self) -> io::Result<()> {
        let mut writer = StandardStream::stdout(ColorChoice::Always);

        self.write_with(&mut writer, &Stylesheet::new())
    }

    pub fn to_string(self) -> io::Result<String> {
        let mut writer = ::termcolor::Buffer::no_color();
        let stylesheet = Stylesheet::new();

        self.write_with(&mut writer, &stylesheet)?;

        Ok(String::from_utf8_lossy(writer.as_slice()).into())
    }

    pub fn write_styled(self, stylesheet: &Stylesheet) -> io::Result<()> {
        let mut writer = StandardStream::stdout(ColorChoice::Always);

        self.write_with(&mut writer, stylesheet)
    }

    pub fn write_with(
        self,
        writer: &mut impl WriteColor,
        stylesheet: &Stylesheet,
    ) -> io::Result<()> {
        let mut nesting = vec![];

        writer.reset()?;

        let tree = match self.tree {
            None => return Ok(()),
            Some(nodes) => nodes,
        };

        for item in tree {
            match item {
                Node::Text(string) => {
                    if string.len() != 0 {
                        let style = stylesheet.get(&nesting);

                        match style {
                            None => writer.reset()?,
                            Some(style) => writer.set_style(&style)?,
                        }

                        write!(writer, "{}", string)?;
                    }
                }
                Node::OpenSection(section) => nesting.push(section),
                Node::CloseSection => {
                    nesting.pop().expect("unbalanced push/pop");
                }
                Node::Newline => {
                    writer.reset()?;
                    write!(writer, "\n")?;
                }
            }
        }

        Ok(())
    }
}

pub fn add<Left: Render, Right: Render>(left: Left, right: Right) -> Combine<Left, Right> {
    Combine { left, right }
}