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
use std::fmt;
pub use std::fmt::Write;
pub use std::fmt::{Result, Formatter, Display, Debug};


pub mod colors {
    use std::fmt::{self, Write};

    pub trait Color {
        fn color<W: Write>(w: &mut W) -> fmt::Result;
        fn display<W: Write, D: fmt::Display>(w: &mut W, v: D) -> fmt::Result;
        fn debug<W: Write, D: fmt::Debug>(w: &mut W, v: D) -> fmt::Result;
    }

    pub struct Red;

    impl Color for Red {
        #[inline]
        fn color<W: Write>(w: &mut W) -> fmt::Result {
            write!(w, "\x1b[31m")
        }

        #[inline]
        fn display<W: Write, D: fmt::Display>(w: &mut W, v: D) -> fmt::Result {
            write!(w, "\x1b[31m{}\x1b[0m", v)
        }

        #[inline]
        fn debug<W: Write, D: fmt::Debug>(w: &mut W, v: D) -> fmt::Result {
            write!(w, "\x1b[31m{:?}\x1b[0m", v)
        }
    }

    pub struct Green;

    impl Color for Green {
        #[inline]
        fn color<W: Write>(w: &mut W) -> fmt::Result {
            write!(w, "\x1b[32m")
        }

        #[inline]
        fn display<W: Write, D: fmt::Display>(w: &mut W, v: D) -> fmt::Result {
            write!(w, "\x1b[32m{}\x1b[0m", v)
        }

        #[inline]
        fn debug<W: Write, D: fmt::Debug>(w: &mut W, v: D) -> fmt::Result {
            write!(w, "\x1b[32m{:?}\x1b[0m", v)
        }
    }

    pub struct Yellow;

    impl Color for Yellow {
        #[inline]
        fn color<W: Write>(w: &mut W) -> fmt::Result {
            write!(w, "\x1b[33m")
        }

        #[inline]
        fn display<W: Write, D: fmt::Display>(w: &mut W, v: D) -> fmt::Result {
            write!(w, "\x1b[33m{}\x1b[0m", v)
        }

        #[inline]
        fn debug<W: Write, D: fmt::Debug>(w: &mut W, v: D) -> fmt::Result {
            write!(w, "\x1b[33m{:?}\x1b[0m", v)
        }
    }
}
use self::colors::*;


pub struct DetailFormatter<'a, 'b> {
    w: &'a mut fmt::Formatter<'b>,
    scoped: bool,
    in_group: bool,
    fresh_group: bool,
}

impl<'a, 'b> DetailFormatter<'a, 'b> {
    pub fn new(w: &'a mut fmt::Formatter<'b>, scoped: bool) -> DetailFormatter<'a, 'b> {
        DetailFormatter {
            w,
            scoped,
            in_group: false,
            fresh_group: true,
        }
    }

    #[inline]
    pub fn start_group(&mut self) {
        self.in_group = true;
        self.fresh_group = true;
    }

    #[inline]
    pub fn end_group(&mut self) -> fmt::Result {
        self.in_group = false;
        if !self.fresh_group {
            write!(self, "]")?
        }
        Ok(())
    }

    #[inline]
    pub fn start(&mut self) -> fmt::Result {
        if !self.scoped {
            write!(self, "\x1b[90m")
        } else {
            Ok(())
        }
    }

    #[inline]
    pub fn end(&mut self) -> fmt::Result {
        if !self.scoped {
            write!(self, "\x1b[0m")
        } else {
            Ok(())
        }
    }

    #[inline]
    pub fn id<D: fmt::Display>(&mut self, v: D) -> fmt::Result {
        if self.scoped {
            write!(self, "\x1b[32m#{}\x1b[0m, ", v)
        } else {
            write!(self, "#{}, ", v)
        }
    }

    #[inline]
    pub fn color<C: Color>(&mut self) -> fmt::Result {
        if self.scoped {
            C::color(self)
        } else {
            Ok(())
        }
    }

    #[inline]
    pub fn display<C: Color, D: fmt::Display>(&mut self, v: D) -> fmt::Result {
        self.push_into_group()?;
        if self.scoped {
            C::display(self, v)
        } else {
            write!(self, "{}", v)
        }
    }

    #[inline]
    pub fn debug<C: Color, D: fmt::Debug>(&mut self, v: D) -> fmt::Result {
        self.push_into_group()?;
        if self.scoped {
            C::debug(self, v)
        } else {
            write!(self, "{:?}", v)
        }
    }

    pub fn opt_debug<C: Color, D: fmt::Debug>(&mut self, v: &Option<D>) -> fmt::Result {
        if let Some(v) = &v {
            self.debug::<C, _>(v)?;
        }
        Ok(())
    }

    #[inline]
    pub fn clear(&mut self) -> fmt::Result {
        if self.scoped {
            write!(self, "\x1b[0m")
        } else {
            Ok(())
        }
    }

    fn push_into_group(&mut self) -> fmt::Result {
        if self.in_group {
            if !self.fresh_group {
                write!(self, " / ")?;
            } else {
                write!(self, " [")?;
                self.fresh_group = false;
            }
        }
        Ok(())
    }

    #[inline]
    pub fn child<D: fmt::Display>(&mut self, c: D) -> fmt::Result {
        if self.scoped {
            // if child is unscoped, draw as grey as well
            write!(self, "\n\t\x1b[33m{}\x1b[0m", c)
        } else {
            write!(self, "\n\t\x1b[90m{}\x1b[0m", c)
        }
    }
}

impl<'a, 'b> fmt::Write for DetailFormatter<'a, 'b> {
    fn write_str(&mut self, s: &str) -> fmt::Result {
        self.w.write_str(s)
    }
}