nu_source/
term_colored.rs1use crate::pretty::ShellAnnotation;
2use pretty::{Render, RenderAnnotated};
3use std::io;
4use termcolor::WriteColor;
5
6pub struct TermColored<'a, W> {
7 color_stack: Vec<ShellAnnotation>,
8 upstream: &'a mut W,
9}
10
11impl<'a, W> TermColored<'a, W> {
12 pub fn new(upstream: &'a mut W) -> TermColored<'a, W> {
13 TermColored {
14 color_stack: Vec::new(),
15 upstream,
16 }
17 }
18}
19
20impl<'a, W> Render for TermColored<'a, W>
21where
22 W: io::Write,
23{
24 type Error = io::Error;
25
26 fn write_str(&mut self, s: &str) -> io::Result<usize> {
27 self.upstream.write(s.as_bytes())
28 }
29
30 fn write_str_all(&mut self, s: &str) -> io::Result<()> {
31 self.upstream.write_all(s.as_bytes())
32 }
33}
34
35impl<'a, W> RenderAnnotated<ShellAnnotation> for TermColored<'a, W>
36where
37 W: WriteColor,
38{
39 fn push_annotation(&mut self, ann: &ShellAnnotation) -> Result<(), Self::Error> {
40 self.color_stack.push(*ann);
41 self.upstream.set_color(&(*ann).into())
42 }
43
44 fn pop_annotation(&mut self) -> Result<(), Self::Error> {
45 self.color_stack.pop();
46 match self.color_stack.last() {
47 Some(previous) => self.upstream.set_color(&(*previous).into()),
48 None => self.upstream.reset(),
49 }
50 }
51}