]> git.proxmox.com Git - rustc.git/blob - vendor/snapbox/src/report/color.rs
New upstream version 1.70.0+dfsg2
[rustc.git] / vendor / snapbox / src / report / color.rs
1 #[derive(Copy, Clone, Debug)]
2 #[allow(dead_code)]
3 pub struct Palette {
4 pub(crate) info: styled::Style,
5 pub(crate) warn: styled::Style,
6 pub(crate) error: styled::Style,
7 pub(crate) hint: styled::Style,
8 pub(crate) expected: styled::Style,
9 pub(crate) actual: styled::Style,
10 }
11
12 impl Palette {
13 #[cfg(feature = "color")]
14 pub fn always() -> Self {
15 Self {
16 info: styled::Style(yansi::Style::new(yansi::Color::Green)),
17 warn: styled::Style(yansi::Style::new(yansi::Color::Yellow)),
18 error: styled::Style(yansi::Style::new(yansi::Color::Red)),
19 hint: styled::Style(yansi::Style::new(yansi::Color::Unset).dimmed()),
20 expected: styled::Style(yansi::Style::new(yansi::Color::Green).underline()),
21 actual: styled::Style(yansi::Style::new(yansi::Color::Red).underline()),
22 }
23 }
24
25 #[cfg(not(feature = "color"))]
26 pub fn always() -> Self {
27 Self::never()
28 }
29
30 pub fn never() -> Self {
31 Self {
32 info: Default::default(),
33 warn: Default::default(),
34 error: Default::default(),
35 hint: Default::default(),
36 expected: Default::default(),
37 actual: Default::default(),
38 }
39 }
40
41 pub fn auto() -> Self {
42 if is_colored() {
43 Self::always()
44 } else {
45 Self::never()
46 }
47 }
48
49 pub fn info<D: std::fmt::Display>(self, item: D) -> Styled<D> {
50 self.info.paint(item)
51 }
52
53 pub fn warn<D: std::fmt::Display>(self, item: D) -> Styled<D> {
54 self.warn.paint(item)
55 }
56
57 pub fn error<D: std::fmt::Display>(self, item: D) -> Styled<D> {
58 self.error.paint(item)
59 }
60
61 pub fn hint<D: std::fmt::Display>(self, item: D) -> Styled<D> {
62 self.hint.paint(item)
63 }
64
65 pub fn expected<D: std::fmt::Display>(self, item: D) -> Styled<D> {
66 self.expected.paint(item)
67 }
68
69 pub fn actual<D: std::fmt::Display>(self, item: D) -> Styled<D> {
70 self.actual.paint(item)
71 }
72 }
73
74 fn is_colored() -> bool {
75 #[cfg(feature = "color")]
76 {
77 concolor::get(concolor::Stream::Either).ansi_color()
78 }
79
80 #[cfg(not(feature = "color"))]
81 {
82 false
83 }
84 }
85
86 pub(crate) use styled::Style;
87 pub use styled::Styled;
88
89 #[cfg(feature = "color")]
90 mod styled {
91 #[derive(Copy, Clone, Debug, Default)]
92 pub(crate) struct Style(pub(crate) yansi::Style);
93
94 impl Style {
95 pub(crate) fn paint<T: std::fmt::Display>(self, item: T) -> Styled<T> {
96 Styled(self.0.paint(item))
97 }
98 }
99
100 pub struct Styled<D: std::fmt::Display>(yansi::Paint<D>);
101
102 impl<D: std::fmt::Display> std::fmt::Display for Styled<D> {
103 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
104 self.0.fmt(f)
105 }
106 }
107 }
108
109 #[cfg(not(feature = "color"))]
110 mod styled {
111 #[derive(Copy, Clone, Debug, Default)]
112 pub(crate) struct Style;
113
114 impl Style {
115 pub(crate) fn paint<T: std::fmt::Display>(self, item: T) -> Styled<T> {
116 Styled(item)
117 }
118 }
119
120 pub struct Styled<D: std::fmt::Display>(D);
121
122 impl<D: std::fmt::Display> std::fmt::Display for Styled<D> {
123 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
124 self.0.fmt(f)
125 }
126 }
127 }