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
use crate::pretty::ShellAnnotation; use pretty::{Render, RenderAnnotated}; use std::io; use termcolor::WriteColor; pub struct TermColored<'a, W> { color_stack: Vec<ShellAnnotation>, upstream: &'a mut W, } impl<'a, W> TermColored<'a, W> { pub fn new(upstream: &'a mut W) -> TermColored<'a, W> { TermColored { color_stack: Vec::new(), upstream, } } } impl<'a, W> Render for TermColored<'a, W> where W: io::Write, { type Error = io::Error; fn write_str(&mut self, s: &str) -> io::Result<usize> { self.upstream.write(s.as_bytes()) } fn write_str_all(&mut self, s: &str) -> io::Result<()> { self.upstream.write_all(s.as_bytes()) } } impl<'a, W> RenderAnnotated<ShellAnnotation> for TermColored<'a, W> where W: WriteColor, { fn push_annotation(&mut self, ann: &ShellAnnotation) -> Result<(), Self::Error> { self.color_stack.push(*ann); self.upstream.set_color(&(*ann).into()) } fn pop_annotation(&mut self) -> Result<(), Self::Error> { self.color_stack.pop(); match self.color_stack.last() { Some(previous) => self.upstream.set_color(&(*previous).into()), None => self.upstream.reset(), } } }