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
use crate::color::{spec_bold_color, spec_color};
use std::fmt;
use std::io::{Error, ErrorKind, Result, Write};
use termcolor::{Color, ColorSpec, WriteColor};

pub fn unpack_io_error(error: Error) -> (ErrorKind, String) {
    (error.kind(), error.to_string())
}

#[derive(Default)]
pub struct ColoredOuput {
    spec: ColorSpec,
    chunks: Vec<OutputChunk>,
}

impl ColoredOuput {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn chunks(&self) -> &Vec<OutputChunk> {
        &self.chunks
    }
}

impl Write for ColoredOuput {
    fn write(&mut self, buf: &[u8]) -> Result<usize> {
        let spec = &self.spec;
        let value = std::str::from_utf8(buf).unwrap();

        if let Some(chunk) = self.chunks.last_mut().filter(|chunk| &chunk.spec == spec) {
            chunk.value += value;
        } else {
            self.chunks.push(OutputChunk {
                spec: self.spec.clone(),
                value: String::from(value),
            })
        }

        Ok(buf.len())
    }

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

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

    fn set_color(&mut self, spec: &ColorSpec) -> Result<()> {
        self.spec = spec.clone();
        Ok(())
    }

    fn reset(&mut self) -> Result<()> {
        self.spec = ColorSpec::new();
        Ok(())
    }
}

#[derive(PartialEq, Clone)]
pub struct OutputChunk {
    pub spec: ColorSpec,
    pub value: String,
}

impl OutputChunk {
    pub fn plain(value: &str) -> Self {
        Self {
            spec: ColorSpec::new(),
            value: String::from(value),
        }
    }

    pub fn color(color: Color, value: &str) -> Self {
        Self {
            spec: spec_color(color),
            value: String::from(value),
        }
    }

    pub fn bold_color(color: Color, value: &str) -> Self {
        Self {
            spec: spec_bold_color(color),
            value: String::from(value),
        }
    }
}

impl fmt::Debug for OutputChunk {
    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(fmt, "OutputChunk::")?;

        match (self.spec.fg(), self.spec.bold()) {
            (None, _) => write!(fmt, "plain(")?,
            (Some(color), true) => write!(fmt, "bold_color(Color::{:?}, ", color)?,
            (Some(color), false) => write!(fmt, "color(Color::{:?}, ", color)?,
        }

        write!(fmt, "{:?})", self.value.replace("\n", "\\n"))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use ntest::*;

    #[test]
    fn unpack_io_error() {
        use super::*;

        assert_eq!(
            unpack_io_error(Error::new(ErrorKind::Other, "test")),
            (ErrorKind::Other, String::from("test"))
        );
    }

    mod colored_output {
        use super::*;

        #[test]
        fn supports_color() {
            assert_true!(ColoredOuput::new().supports_color());
        }

        #[test]
        fn write() {
            let mut output = ColoredOuput::new();

            write!(output, "a").unwrap();
            write!(output, "b").unwrap();
            output.set_color(&spec_color(Color::Red)).unwrap();
            write!(output, "c").unwrap();
            write!(output, "d").unwrap();
            output.set_color(&spec_bold_color(Color::Blue)).unwrap();
            write!(output, "e").unwrap();
            write!(output, "f").unwrap();
            output.reset().unwrap();
            write!(output, "g").unwrap();
            output.flush().unwrap();

            assert_eq!(
                output.chunks,
                &[
                    OutputChunk::plain("ab"),
                    OutputChunk::color(Color::Red, "cd"),
                    OutputChunk::bold_color(Color::Blue, "ef"),
                    OutputChunk::plain("g"),
                ]
            );
        }
    }

    mod output_chunk {
        use super::*;

        mod init {
            use super::*;

            #[test]
            fn plain() {
                let chunk = OutputChunk::plain("ab");
                assert_eq!(chunk.spec, ColorSpec::new());
                assert_eq!(chunk.value, "ab");
            }

            #[test]
            fn color() {
                let chunk = OutputChunk::color(Color::Red, "cd");
                assert_eq!(chunk.spec, spec_color(Color::Red));
                assert_eq!(chunk.value, "cd");
            }

            #[test]
            fn bold_color() {
                let chunk = OutputChunk::bold_color(Color::Blue, "ef");
                assert_eq!(chunk.spec, spec_bold_color(Color::Blue));
                assert_eq!(chunk.value, "ef");
            }
        }

        mod display {
            use super::*;

            #[test]
            fn plain() {
                assert_eq!(
                    format!("{:?}", OutputChunk::plain("a\nb")),
                    r#"OutputChunk::plain("a\\nb")"#
                );
            }

            #[test]
            fn color() {
                assert_eq!(
                    format!("{:?}", OutputChunk::color(Color::Red, "c\nd")),
                    r#"OutputChunk::color(Color::Red, "c\\nd")"#
                );
            }

            #[test]
            fn bold_color() {
                assert_eq!(
                    format!("{:?}", OutputChunk::bold_color(Color::Blue, "e\nf")),
                    r#"OutputChunk::bold_color(Color::Blue, "e\\nf")"#
                );
            }
        }
    }
}