summaryrefslogtreecommitdiff
path: root/vendor/anstyle-wincon/src/stream.rs
diff options
context:
space:
mode:
authorValentin Popov <valentin@popov.link>2024-01-08 00:21:28 +0300
committerValentin Popov <valentin@popov.link>2024-01-08 00:21:28 +0300
commit1b6a04ca5504955c571d1c97504fb45ea0befee4 (patch)
tree7579f518b23313e8a9748a88ab6173d5e030b227 /vendor/anstyle-wincon/src/stream.rs
parent5ecd8cf2cba827454317368b68571df0d13d7842 (diff)
downloadfparkan-1b6a04ca5504955c571d1c97504fb45ea0befee4.tar.xz
fparkan-1b6a04ca5504955c571d1c97504fb45ea0befee4.zip
Initial vendor packages
Signed-off-by: Valentin Popov <valentin@popov.link>
Diffstat (limited to 'vendor/anstyle-wincon/src/stream.rs')
-rw-r--r--vendor/anstyle-wincon/src/stream.rs178
1 files changed, 178 insertions, 0 deletions
diff --git a/vendor/anstyle-wincon/src/stream.rs b/vendor/anstyle-wincon/src/stream.rs
new file mode 100644
index 0000000..9f10108
--- /dev/null
+++ b/vendor/anstyle-wincon/src/stream.rs
@@ -0,0 +1,178 @@
+/// Extend `std::io::Write` with wincon styling
+pub trait WinconStream {
+ /// Write colored text to the stream
+ fn write_colored(
+ &mut self,
+ fg: Option<anstyle::AnsiColor>,
+ bg: Option<anstyle::AnsiColor>,
+ data: &[u8],
+ ) -> std::io::Result<usize>;
+}
+
+impl WinconStream for Box<dyn std::io::Write> {
+ fn write_colored(
+ &mut self,
+ fg: Option<anstyle::AnsiColor>,
+ bg: Option<anstyle::AnsiColor>,
+ data: &[u8],
+ ) -> std::io::Result<usize> {
+ crate::ansi::write_colored(self, fg, bg, data)
+ }
+}
+
+impl WinconStream for &'_ mut Box<dyn std::io::Write> {
+ fn write_colored(
+ &mut self,
+ fg: Option<anstyle::AnsiColor>,
+ bg: Option<anstyle::AnsiColor>,
+ data: &[u8],
+ ) -> std::io::Result<usize> {
+ (**self).write_colored(fg, bg, data)
+ }
+}
+
+impl WinconStream for std::fs::File {
+ fn write_colored(
+ &mut self,
+ fg: Option<anstyle::AnsiColor>,
+ bg: Option<anstyle::AnsiColor>,
+ data: &[u8],
+ ) -> std::io::Result<usize> {
+ crate::ansi::write_colored(self, fg, bg, data)
+ }
+}
+
+impl WinconStream for &'_ mut std::fs::File {
+ fn write_colored(
+ &mut self,
+ fg: Option<anstyle::AnsiColor>,
+ bg: Option<anstyle::AnsiColor>,
+ data: &[u8],
+ ) -> std::io::Result<usize> {
+ (**self).write_colored(fg, bg, data)
+ }
+}
+
+impl WinconStream for Vec<u8> {
+ fn write_colored(
+ &mut self,
+ fg: Option<anstyle::AnsiColor>,
+ bg: Option<anstyle::AnsiColor>,
+ data: &[u8],
+ ) -> std::io::Result<usize> {
+ crate::ansi::write_colored(self, fg, bg, data)
+ }
+}
+
+impl WinconStream for &'_ mut Vec<u8> {
+ fn write_colored(
+ &mut self,
+ fg: Option<anstyle::AnsiColor>,
+ bg: Option<anstyle::AnsiColor>,
+ data: &[u8],
+ ) -> std::io::Result<usize> {
+ (**self).write_colored(fg, bg, data)
+ }
+}
+
+impl WinconStream for std::io::Stdout {
+ fn write_colored(
+ &mut self,
+ fg: Option<anstyle::AnsiColor>,
+ bg: Option<anstyle::AnsiColor>,
+ data: &[u8],
+ ) -> std::io::Result<usize> {
+ // Ensure exclusive access
+ self.lock().write_colored(fg, bg, data)
+ }
+}
+
+impl WinconStream for std::io::Stderr {
+ fn write_colored(
+ &mut self,
+ fg: Option<anstyle::AnsiColor>,
+ bg: Option<anstyle::AnsiColor>,
+ data: &[u8],
+ ) -> std::io::Result<usize> {
+ // Ensure exclusive access
+ self.lock().write_colored(fg, bg, data)
+ }
+}
+
+#[cfg(not(windows))]
+mod platform {
+ use super::*;
+
+ impl WinconStream for std::io::StdoutLock<'_> {
+ fn write_colored(
+ &mut self,
+ fg: Option<anstyle::AnsiColor>,
+ bg: Option<anstyle::AnsiColor>,
+ data: &[u8],
+ ) -> std::io::Result<usize> {
+ crate::ansi::write_colored(self, fg, bg, data)
+ }
+ }
+
+ impl WinconStream for std::io::StderrLock<'_> {
+ fn write_colored(
+ &mut self,
+ fg: Option<anstyle::AnsiColor>,
+ bg: Option<anstyle::AnsiColor>,
+ data: &[u8],
+ ) -> std::io::Result<usize> {
+ crate::ansi::write_colored(self, fg, bg, data)
+ }
+ }
+}
+
+#[cfg(windows)]
+mod platform {
+ use super::*;
+
+ impl WinconStream for std::io::StdoutLock<'_> {
+ fn write_colored(
+ &mut self,
+ fg: Option<anstyle::AnsiColor>,
+ bg: Option<anstyle::AnsiColor>,
+ data: &[u8],
+ ) -> std::io::Result<usize> {
+ let initial = crate::windows::stdout_initial_colors();
+ crate::windows::write_colored(self, fg, bg, data, initial)
+ }
+ }
+
+ impl WinconStream for std::io::StderrLock<'_> {
+ fn write_colored(
+ &mut self,
+ fg: Option<anstyle::AnsiColor>,
+ bg: Option<anstyle::AnsiColor>,
+ data: &[u8],
+ ) -> std::io::Result<usize> {
+ let initial = crate::windows::stderr_initial_colors();
+ crate::windows::write_colored(self, fg, bg, data, initial)
+ }
+ }
+}
+
+impl WinconStream for &'_ mut std::io::StdoutLock<'_> {
+ fn write_colored(
+ &mut self,
+ fg: Option<anstyle::AnsiColor>,
+ bg: Option<anstyle::AnsiColor>,
+ data: &[u8],
+ ) -> std::io::Result<usize> {
+ (**self).write_colored(fg, bg, data)
+ }
+}
+
+impl WinconStream for &'_ mut std::io::StderrLock<'_> {
+ fn write_colored(
+ &mut self,
+ fg: Option<anstyle::AnsiColor>,
+ bg: Option<anstyle::AnsiColor>,
+ data: &[u8],
+ ) -> std::io::Result<usize> {
+ (**self).write_colored(fg, bg, data)
+ }
+}