anstyle_wincon/
stream.rs

1/// Extend `std::io::Write` with wincon styling
2pub trait WinconStream {
3    /// Write colored text to the stream
4    fn write_colored(
5        &mut self,
6        fg: Option<anstyle::AnsiColor>,
7        bg: Option<anstyle::AnsiColor>,
8        data: &[u8],
9    ) -> std::io::Result<usize>;
10}
11
12impl<T: WinconStream + ?Sized> WinconStream for &mut T {
13    fn write_colored(
14        &mut self,
15        fg: Option<anstyle::AnsiColor>,
16        bg: Option<anstyle::AnsiColor>,
17        data: &[u8],
18    ) -> std::io::Result<usize> {
19        (**self).write_colored(fg, bg, data)
20    }
21}
22
23impl<T: WinconStream + ?Sized> WinconStream for Box<T> {
24    fn write_colored(
25        &mut self,
26        fg: Option<anstyle::AnsiColor>,
27        bg: Option<anstyle::AnsiColor>,
28        data: &[u8],
29    ) -> std::io::Result<usize> {
30        (**self).write_colored(fg, bg, data)
31    }
32}
33
34impl WinconStream for dyn std::io::Write {
35    fn write_colored(
36        &mut self,
37        fg: Option<anstyle::AnsiColor>,
38        bg: Option<anstyle::AnsiColor>,
39        data: &[u8],
40    ) -> std::io::Result<usize> {
41        crate::ansi::write_colored(self, fg, bg, data)
42    }
43}
44
45impl WinconStream for dyn std::io::Write + Send {
46    fn write_colored(
47        &mut self,
48        fg: Option<anstyle::AnsiColor>,
49        bg: Option<anstyle::AnsiColor>,
50        data: &[u8],
51    ) -> std::io::Result<usize> {
52        crate::ansi::write_colored(self, fg, bg, data)
53    }
54}
55
56impl WinconStream for dyn std::io::Write + Send + Sync {
57    fn write_colored(
58        &mut self,
59        fg: Option<anstyle::AnsiColor>,
60        bg: Option<anstyle::AnsiColor>,
61        data: &[u8],
62    ) -> std::io::Result<usize> {
63        crate::ansi::write_colored(self, fg, bg, data)
64    }
65}
66
67impl WinconStream for std::fs::File {
68    fn write_colored(
69        &mut self,
70        fg: Option<anstyle::AnsiColor>,
71        bg: Option<anstyle::AnsiColor>,
72        data: &[u8],
73    ) -> std::io::Result<usize> {
74        crate::ansi::write_colored(self, fg, bg, data)
75    }
76}
77
78impl WinconStream for Vec<u8> {
79    fn write_colored(
80        &mut self,
81        fg: Option<anstyle::AnsiColor>,
82        bg: Option<anstyle::AnsiColor>,
83        data: &[u8],
84    ) -> std::io::Result<usize> {
85        crate::ansi::write_colored(self, fg, bg, data)
86    }
87}
88
89impl WinconStream for std::io::Stdout {
90    fn write_colored(
91        &mut self,
92        fg: Option<anstyle::AnsiColor>,
93        bg: Option<anstyle::AnsiColor>,
94        data: &[u8],
95    ) -> std::io::Result<usize> {
96        // Ensure exclusive access
97        self.lock().write_colored(fg, bg, data)
98    }
99}
100
101impl WinconStream for std::io::Stderr {
102    fn write_colored(
103        &mut self,
104        fg: Option<anstyle::AnsiColor>,
105        bg: Option<anstyle::AnsiColor>,
106        data: &[u8],
107    ) -> std::io::Result<usize> {
108        // Ensure exclusive access
109        self.lock().write_colored(fg, bg, data)
110    }
111}
112
113#[cfg(not(windows))]
114mod platform {
115    impl super::WinconStream for std::io::StdoutLock<'_> {
116        fn write_colored(
117            &mut self,
118            fg: Option<anstyle::AnsiColor>,
119            bg: Option<anstyle::AnsiColor>,
120            data: &[u8],
121        ) -> std::io::Result<usize> {
122            crate::ansi::write_colored(self, fg, bg, data)
123        }
124    }
125
126    impl super::WinconStream for std::io::StderrLock<'_> {
127        fn write_colored(
128            &mut self,
129            fg: Option<anstyle::AnsiColor>,
130            bg: Option<anstyle::AnsiColor>,
131            data: &[u8],
132        ) -> std::io::Result<usize> {
133            crate::ansi::write_colored(self, fg, bg, data)
134        }
135    }
136}
137
138#[cfg(windows)]
139mod platform {
140    impl super::WinconStream for std::io::StdoutLock<'_> {
141        fn write_colored(
142            &mut self,
143            fg: Option<anstyle::AnsiColor>,
144            bg: Option<anstyle::AnsiColor>,
145            data: &[u8],
146        ) -> std::io::Result<usize> {
147            let initial = crate::windows::stdout_initial_colors();
148            crate::windows::write_colored(self, fg, bg, data, initial)
149        }
150    }
151
152    impl super::WinconStream for std::io::StderrLock<'_> {
153        fn write_colored(
154            &mut self,
155            fg: Option<anstyle::AnsiColor>,
156            bg: Option<anstyle::AnsiColor>,
157            data: &[u8],
158        ) -> std::io::Result<usize> {
159            let initial = crate::windows::stderr_initial_colors();
160            crate::windows::write_colored(self, fg, bg, data, initial)
161        }
162    }
163}