From 5646a2893a98a2675dfd09979891ff1d4354b958 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Thu, 31 Aug 2023 14:09:17 -0500 Subject: [PATCH] refactor(wincon): Pull out platform-specific code --- crates/anstyle-wincon/src/stream.rs | 52 ++++++---------------------- crates/anstyle-wincon/src/windows.rs | 40 +++++++++++++++++++++ 2 files changed, 50 insertions(+), 42 deletions(-) diff --git a/crates/anstyle-wincon/src/stream.rs b/crates/anstyle-wincon/src/stream.rs index d1d232b0..3460772a 100644 --- a/crates/anstyle-wincon/src/stream.rs +++ b/crates/anstyle-wincon/src/stream.rs @@ -106,7 +106,7 @@ impl WinconStream for std::fs::File { /// Write colored text to the screen #[derive(Clone, Debug)] pub(crate) enum ConsoleState { - Wincon(WinconAdapter), + Wincon(StdioAdapter), Pass(PassThroughAdapter), } @@ -116,7 +116,7 @@ impl ConsoleState { ) -> std::io::Result { let adapter = match stream.get_colors() { Ok((Some(initial_fg), Some(initial_bg))) => { - Self::Wincon(WinconAdapter::new(initial_fg, initial_bg)) + Self::Wincon(StdioAdapter::new(initial_fg, initial_bg)) } Ok(_) => Self::Pass(PassThroughAdapter), Err(err) => { @@ -162,33 +162,8 @@ impl ConsoleState { pub(crate) struct PassThroughAdapter; impl PassThroughAdapter { - fn apply( - &mut self, - stream: &mut S, - fg: Option, - bg: Option, - ) -> std::io::Result<()> { - stream.set_colors(fg, bg)?; - Ok(()) - } -} - -#[derive(Clone, Debug)] -pub(crate) struct WinconAdapter { - initial_fg: anstyle::AnsiColor, - initial_bg: anstyle::AnsiColor, - last_fg: anstyle::AnsiColor, - last_bg: anstyle::AnsiColor, -} - -impl WinconAdapter { - fn new(initial_fg: anstyle::AnsiColor, initial_bg: anstyle::AnsiColor) -> Self { - Self { - initial_fg, - initial_bg, - last_fg: initial_fg, - last_bg: initial_bg, - } + fn new(_initial_fg: anstyle::AnsiColor, _initial_bg: anstyle::AnsiColor) -> Self { + Self } fn apply( @@ -197,23 +172,16 @@ impl WinconAdapter { fg: Option, bg: Option, ) -> std::io::Result<()> { - let fg = fg.unwrap_or(self.initial_fg); - let bg = bg.unwrap_or(self.initial_bg); - if fg == self.last_fg && bg == self.last_bg { - return Ok(()); - } - - // Ensure everything is written with the last set of colors before applying the next set - stream.flush()?; - - stream.set_colors(Some(fg), Some(bg))?; - self.last_fg = fg; - self.last_bg = bg; - + stream.set_colors(fg, bg)?; Ok(()) } } +#[cfg(windows)] +use crate::windows::WinconAdapter as StdioAdapter; +#[cfg(not(windows))] +use PassThroughAdapter as StdioAdapter; + #[cfg(windows)] mod wincon { use std::os::windows::io::AsHandle; diff --git a/crates/anstyle-wincon/src/windows.rs b/crates/anstyle-wincon/src/windows.rs index 188a8e54..47d4091c 100644 --- a/crates/anstyle-wincon/src/windows.rs +++ b/crates/anstyle-wincon/src/windows.rs @@ -22,6 +22,46 @@ pub fn get_colors( Ok((fg, bg)) } +#[derive(Clone, Debug)] +pub(crate) struct WinconAdapter { + initial_fg: anstyle::AnsiColor, + initial_bg: anstyle::AnsiColor, + last_fg: anstyle::AnsiColor, + last_bg: anstyle::AnsiColor, +} + +impl WinconAdapter { + pub(crate) fn new(initial_fg: anstyle::AnsiColor, initial_bg: anstyle::AnsiColor) -> Self { + Self { + initial_fg, + initial_bg, + last_fg: initial_fg, + last_bg: initial_bg, + } + } + + pub(crate) fn apply( + &mut self, + stream: &mut S, + fg: Option, + bg: Option, + ) -> std::io::Result<()> { + let fg = fg.unwrap_or(self.initial_fg); + let bg = bg.unwrap_or(self.initial_bg); + if fg == self.last_fg && bg == self.last_bg { + return Ok(()); + } + + // Ensure everything is written with the last set of colors before applying the next set + stream.flush()?; + + stream.set_colors(Some(fg), Some(bg))?; + self.last_fg = fg; + self.last_bg = bg; + + Ok(()) + } +} mod inner { use windows_sys::Win32::System::Console::CONSOLE_CHARACTER_ATTRIBUTES; use windows_sys::Win32::System::Console::CONSOLE_SCREEN_BUFFER_INFO;