1 | use std::fmt::{self, Display}; |
2 | |
3 | use yansi_term::{Color::Fixed, Style as AnsiTermStyle}; |
4 | |
5 | use crate::formatter::style::{Style, StyleClass, Stylesheet}; |
6 | |
7 | struct AnsiTermStyleWrapper { |
8 | style: AnsiTermStyle, |
9 | } |
10 | |
11 | impl Style for AnsiTermStyleWrapper { |
12 | fn paint(&self, text: &str, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
13 | self.style.paint(input:text).fmt(f) |
14 | } |
15 | |
16 | fn paint_fn<'a>( |
17 | &self, |
18 | c: Box<dyn FnOnce(&mut fmt::Formatter<'_>) -> fmt::Result + 'a>, |
19 | f: &mut fmt::Formatter<'_>, |
20 | ) -> fmt::Result { |
21 | self.style.paint_fn(c).fmt(f) |
22 | } |
23 | |
24 | fn bold(&self) -> Box<dyn Style> { |
25 | Box::new(AnsiTermStyleWrapper { style: self.style }) |
26 | } |
27 | } |
28 | |
29 | pub struct AnsiTermStylesheet; |
30 | |
31 | impl Stylesheet for AnsiTermStylesheet { |
32 | fn get_style(&self, class: StyleClass) -> Box<dyn Style> { |
33 | let ansi_term_style: Style = match class { |
34 | StyleClass::Error => Fixed(9).bold(), |
35 | StyleClass::Warning => Fixed(11).bold(), |
36 | StyleClass::Info => Fixed(12).bold(), |
37 | StyleClass::Note => AnsiTermStyle::new().bold(), |
38 | StyleClass::Help => Fixed(14).bold(), |
39 | |
40 | StyleClass::LineNo => Fixed(12).bold(), |
41 | |
42 | StyleClass::Emphasis => AnsiTermStyle::new().bold(), |
43 | |
44 | StyleClass::None => AnsiTermStyle::new(), |
45 | }; |
46 | Box::new(AnsiTermStyleWrapper { |
47 | style: ansi_term_style, |
48 | }) |
49 | } |
50 | } |
51 | |