render_tree/stylesheet/
accumulator.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
use log::*;
use std::io;
use std::io::prelude::*;
use termcolor::ColorSpec;
use termcolor::WriteColor;

/// A facility for creating visually inspectable representations of colored output
/// so they can be easily tested.
///
/// A new color is represented as `{style}` and a reset is represented by `{/}`.
///
/// Attributes are printed in this order:
///
/// - Foreground color as `fg:Color`
/// - Background color as `bg:Color`
/// - Bold as `bold`
/// - Underline as `underline`
/// - Intense as `bright`
///
/// For example, the style "intense, bold red foreground" would be printed as:
///
/// ```text
/// {fg:Red bold intense}
/// ```
///
/// Since this implementation attempts to make it possible to faithfully
/// understand what real WriteColor implementations would do, it tries
/// to approximate the contract in the WriteColor trait: "Subsequent
/// writes to this write will use these settings until either reset is
/// called or new color settings are set.")
///
/// - If set_color is called with a style, `{...}` is emitted containing the
///   color attributes.
/// - If set_color is called with no style, `{/}` is emitted
/// - If reset is called, `{/}` is emitted
pub struct ColorAccumulator {
    buf: Vec<u8>,
    color: ColorSpec,
}

impl ColorAccumulator {
    pub fn new() -> ColorAccumulator {
        ColorAccumulator {
            buf: Vec::new(),
            color: ColorSpec::new(),
        }
    }

    pub fn to_string(self) -> String {
        String::from_utf8(self.buf).unwrap()
    }
}

impl io::Write for ColorAccumulator {
    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
        self.buf.extend(buf);
        Ok(buf.len())
    }

    fn flush(&mut self) -> io::Result<()> {
        Ok(())
    }
}

impl WriteColor for ColorAccumulator {
    fn supports_color(&self) -> bool {
        true
    }

    fn set_color(&mut self, spec: &ColorSpec) -> io::Result<()> {
        #![allow(unused_assignments)]

        if self.color == *spec {
            return Ok(());
        } else {
            trace!(
                "eq={:?} spec={:?} current={:?}",
                self.color == *spec,
                spec,
                self.color
            );

            self.color = spec.clone();
        }

        if spec.is_none() {
            write!(self, "{{/}}")?;
            return Ok(());
        } else {
            write!(self, "{{")?;
        }

        let mut first = true;

        fn write_first(first: bool, write: &mut ColorAccumulator) -> io::Result<bool> {
            if !first {
                write!(write, " ")?;
            }

            Ok(false)
        };

        if let Some(fg) = spec.fg() {
            first = write_first(first, self)?;
            write!(self, "fg:{:?}", fg)?;
        }

        if let Some(bg) = spec.bg() {
            first = write_first(first, self)?;
            write!(self, "bg:{:?}", bg)?;
        }

        if spec.bold() {
            first = write_first(first, self)?;
            write!(self, "bold")?;
        }

        if spec.underline() {
            first = write_first(first, self)?;
            write!(self, "underline")?;
        }

        if spec.intense() {
            first = write_first(first, self)?;
            write!(self, "bright")?;
        }

        write!(self, "}}")?;

        Ok(())
    }

    fn reset(&mut self) -> io::Result<()> {
        let color = self.color.clone();

        if color != ColorSpec::new() {
            write!(self, "{{/}}")?;
            self.color = ColorSpec::new();
        }

        Ok(())
    }
}