render_tree/
debug.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
use crate::stylesheet::WriteStyle;
use crate::Document;
use crate::{Node, PadItem};
use crate::{Style, Stylesheet};
use std::{fmt, io};
use termcolor::WriteColor;

struct DebugDocument<'a, C: WriteColor + 'a> {
    document: &'a Document,
    writer: &'a mut C,
    stylesheet: &'a Stylesheet,
    line_start: bool,
    nesting: Vec<&'static str>,
}

impl<'a, C: WriteColor + 'a> DebugDocument<'a, C> {
    fn write_document(mut self) -> io::Result<()> {
        let tree = match self.document.tree() {
            None => return Ok(()),
            Some(nodes) => nodes,
        };

        self.writer.reset()?;

        for item in tree.clone() {
            match item {
                Node::Text(string) => self.write_text(string)?,
                Node::OpenSection(section) => self.write_open_section(section)?,
                Node::CloseSection => self.write_close_section()?,
                Node::Newline => self.write_newline()?,
            }
        }

        write!(self.writer, "\n\n")?;

        Ok(())
    }

    fn write_text(&mut self, string: &str) -> io::Result<()> {
        if self.line_start {
            self.start_line()?;
            self.styled_write("|", "fg: black; weight: bold")?;
        }

        self.write(string)?;
        self.line_start = false;

        Ok(())
    }

    fn write_open_section(&mut self, section: &'static str) -> io::Result<()> {
        self.start_line()?;
        self.write("<")?;

        self.nesting.push(section);
        let style = self.stylesheet.get(&self.nesting[..]);

        self.styled_write(section, "fg: blue; weight: bold")?;

        if let Some(style) = style {
            if style.has_value() {
                self.write(" ")?;
                let debug_attributes = style.debug_attributes();
                let last = debug_attributes.len() - 1;

                for (i, (name, value)) in debug_attributes.iter().enumerate() {
                    self.styled_write(name, "fg: black; weight: bold")?;

                    if let Some(value) = value {
                        self.write("=")?;
                        self.styled_write(value, "fg: cyan; weight: dim")?;
                    }

                    if i != last {
                        self.write(" ")?;
                    }
                }

                self.styled_write(" ยง", style)?;
            }
        }

        self.writer.reset()?;
        write!(self.writer, ">")?;
        self.line_start = true;

        Ok(())
    }

    fn write_close_section(&mut self) -> io::Result<()> {
        let popped = self.nesting.pop().expect("unbalanced push/pop");
        self.start_line()?;
        write!(self.writer, "</")?;

        self.writer.set_style(&Style("fg: blue; weight: bold"))?;
        write!(self.writer, "{}", popped)?;
        self.writer.reset()?;
        write!(self.writer, ">")?;
        self.line_start = true;

        Ok(())
    }

    fn write_newline(&mut self) -> io::Result<()> {
        let writer = &mut self.writer;
        writer.reset()?;

        if self.line_start {
            write!(writer, "\n{}", PadItem("  ", self.nesting.len()))?;
        }

        write!(writer, "\\n",)?;
        self.line_start = true;

        Ok(())
    }

    fn start_line(&mut self) -> io::Result<()> {
        let pad = self.pad();
        write!(self.writer, "\n{}", pad)
    }

    fn styled_write(
        &mut self,
        value: impl fmt::Display,
        style: impl Into<Style>,
    ) -> io::Result<()> {
        self.writer.set_style(style)?;
        self.write(value)?;
        self.writer.reset()
    }

    fn write(&mut self, value: impl fmt::Display) -> io::Result<()> {
        write!(self.writer, "{}", value)
    }

    fn pad(&self) -> PadItem<&'static str> {
        PadItem(" ", self.nesting.len())
    }
}

impl Document {
    pub fn debug_write(
        &self,
        writer: &mut impl WriteColor,
        stylesheet: &Stylesheet,
    ) -> io::Result<()> {
        DebugDocument {
            document: self,
            writer,
            stylesheet,
            line_start: true,
            nesting: vec![],
        }.write_document()
    }
}