summaryrefslogtreecommitdiff
path: root/patches/use-termcolor.patch
blob: 9831f7d85b1ace937a34e60a2a86e92cb067f375 (plain)
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
--- a/Android.bp	2023-11-07 14:29:55.304929829 +0100
+++ b/Android.bp	2023-11-07 14:30:05.916987569 +0100
@@ -16,7 +16,7 @@
         "regex",
     ],
     rustlibs: [
-        "libanstyle",
+        "libtermcolor",
         "libitertools",
         "libpredicates_core",
         "libregex",
@@ -47,7 +47,7 @@
         "regex",
     ],
     rustlibs: [
-        "libanstyle",
+        "libtermcolor",
         "libitertools",
         "libpredicates_core",
         "libpredicates_tree",
--- a/src/color.rs	2006-07-24 03:21:28.000000000 +0200
+++ b/src/color.rs	2023-11-07 14:26:51.131915579 +0100
@@ -1,17 +1,29 @@
-#[derive(Copy, Clone, Debug, Default)]
+use std::io::Write;
+use termcolor::{Color, ColorSpec, WriteColor};
+
+#[derive(Clone, Debug, Default)]
 pub(crate) struct Palette {
-    description: anstyle::Style,
-    var: anstyle::Style,
-    expected: anstyle::Style,
+    description: ColorSpec,
+    var: ColorSpec,
+    expected: ColorSpec,
 }
 
 impl Palette {
     pub(crate) fn new(alternate: bool) -> Self {
         if alternate && cfg!(feature = "color") {
             Self {
-                description: anstyle::AnsiColor::Blue.on_default() | anstyle::Effects::BOLD,
-                var: anstyle::AnsiColor::Red.on_default() | anstyle::Effects::BOLD,
-                expected: anstyle::AnsiColor::Green.on_default() | anstyle::Effects::BOLD,
+                description: ColorSpec::new()
+                    .set_fg(Some(Color::Blue))
+                    .set_bold(true)
+                    .clone(),
+                var: ColorSpec::new()
+                    .set_fg(Some(Color::Red))
+                    .set_bold(true)
+                    .clone(),
+                expected: ColorSpec::new()
+                    .set_fg(Some(Color::Green))
+                    .set_bold(true)
+                    .clone(),
             }
         } else {
             Self::plain()
@@ -26,27 +38,27 @@
         }
     }
 
-    pub(crate) fn description<D: std::fmt::Display>(self, display: D) -> Styled<D> {
-        Styled::new(display, self.description)
+    pub(crate) fn description<D: std::fmt::Display>(&self, display: D) -> Styled<D> {
+        Styled::new(display, self.description.clone())
     }
 
-    pub(crate) fn var<D: std::fmt::Display>(self, display: D) -> Styled<D> {
-        Styled::new(display, self.var)
+    pub(crate) fn var<D: std::fmt::Display>(&self, display: D) -> Styled<D> {
+        Styled::new(display, self.var.clone())
     }
 
-    pub(crate) fn expected<D: std::fmt::Display>(self, display: D) -> Styled<D> {
-        Styled::new(display, self.expected)
+    pub(crate) fn expected<D: std::fmt::Display>(&self, display: D) -> Styled<D> {
+        Styled::new(display, self.expected.clone())
     }
 }
 
 #[derive(Debug)]
 pub(crate) struct Styled<D> {
     display: D,
-    style: anstyle::Style,
+    style: ColorSpec,
 }
 
 impl<D: std::fmt::Display> Styled<D> {
-    pub(crate) fn new(display: D, style: anstyle::Style) -> Self {
+    pub(crate) fn new(display: D, style: ColorSpec) -> Self {
         Self { display, style }
     }
 }
@@ -55,10 +67,11 @@
     #[inline]
     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
         if f.alternate() {
-            write!(f, "{}", self.style.render())?;
-            self.display.fmt(f)?;
-            write!(f, "{}", self.style.render_reset())?;
-            Ok(())
+            let mut buf = termcolor::Buffer::ansi();
+            buf.set_color(&self.style).unwrap();
+            write!(&mut buf, "{}", &self.display).unwrap();
+            buf.reset().unwrap();
+            write!(f, "{}", String::from_utf8(buf.into_inner()).unwrap())
         } else {
             self.display.fmt(f)
         }