diff --git a/Cargo.lock b/Cargo.lock index b1f2fab8..1403b5d6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -42,7 +42,6 @@ dependencies = [ "anstyle-wincon 2.0.0", "colorchoice 1.0.0", "criterion", - "is-terminal", "owo-colors", "proptest", "strip-ansi-escapes", diff --git a/crates/anstream/Cargo.toml b/crates/anstream/Cargo.toml index 5afb595b..f4522619 100644 --- a/crates/anstream/Cargo.toml +++ b/crates/anstream/Cargo.toml @@ -26,7 +26,7 @@ pre-release-replacements = [ [features] default = ["auto", "wincon"] -auto = ["dep:anstyle-query", "dep:colorchoice", "dep:is-terminal"] +auto = ["dep:anstyle-query", "dep:colorchoice"] wincon = ["dep:anstyle-wincon"] [dependencies] @@ -34,7 +34,6 @@ anstyle = { version = "1.0.0", path = "../anstyle" } anstyle-parse = { version = "0.2.0", path = "../anstyle-parse" } colorchoice = { version = "1.0.0", path = "../colorchoice", optional = true } anstyle-query = { version = "1.0.0", path = "../anstyle-query", optional = true } -is-terminal = { version = "0.4.4", optional = true } utf8parse = "0.2.1" [target.'cfg(windows)'.dependencies] diff --git a/crates/anstream/src/auto.rs b/crates/anstream/src/auto.rs index de1b822a..969a8da9 100644 --- a/crates/anstream/src/auto.rs +++ b/crates/anstream/src/auto.rs @@ -1,5 +1,6 @@ #[cfg(feature = "auto")] use crate::ColorChoice; +use crate::IsTerminal; use crate::Lockable; use crate::RawStream; use crate::StripStream; @@ -161,7 +162,7 @@ fn choice(raw: &dyn RawStream) -> ColorChoice { } #[cfg(feature = "auto")] -impl is_terminal::IsTerminal for AutoStream +impl IsTerminal for AutoStream where S: RawStream, { diff --git a/crates/anstream/src/buffer.rs b/crates/anstream/src/buffer.rs index 1ba0bffd..e635687c 100644 --- a/crates/anstream/src/buffer.rs +++ b/crates/anstream/src/buffer.rs @@ -1,3 +1,5 @@ +use crate::IsTerminal; + /// In-memory [`RawStream`][crate::RawStream] #[derive(Clone, Default, Debug, PartialEq, Eq)] pub struct Buffer(Vec); @@ -26,8 +28,7 @@ impl AsRef<[u8]> for Buffer { } } -#[cfg(feature = "auto")] -impl is_terminal::IsTerminal for Buffer { +impl IsTerminal for Buffer { #[inline] fn is_terminal(&self) -> bool { false diff --git a/crates/anstream/src/is_terminal.rs b/crates/anstream/src/is_terminal.rs new file mode 100644 index 00000000..840f851d --- /dev/null +++ b/crates/anstream/src/is_terminal.rs @@ -0,0 +1,70 @@ +pub trait IsTerminal { + fn is_terminal(&self) -> bool; +} + +impl IsTerminal for std::io::Stdout { + #[inline] + fn is_terminal(&self) -> bool { + std::io::IsTerminal::is_terminal(self) + } +} + +impl IsTerminal for std::io::StdoutLock<'static> { + #[inline] + fn is_terminal(&self) -> bool { + std::io::IsTerminal::is_terminal(self) + } +} + +impl IsTerminal for std::io::Stderr { + #[inline] + fn is_terminal(&self) -> bool { + std::io::IsTerminal::is_terminal(self) + } +} + +impl IsTerminal for std::io::StderrLock<'static> { + #[inline] + fn is_terminal(&self) -> bool { + std::io::IsTerminal::is_terminal(self) + } +} + +#[cfg(all(windows, feature = "wincon"))] +impl IsTerminal for anstyle_wincon::Console { + #[inline] + fn is_terminal(&self) -> bool { + self.is_terminal() + } +} + +#[cfg(all(windows, feature = "wincon"))] +impl IsTerminal for anstyle_wincon::Console> { + #[inline] + fn is_terminal(&self) -> bool { + self.is_terminal() + } +} + +#[cfg(all(windows, feature = "wincon"))] +impl IsTerminal for anstyle_wincon::Console { + #[inline] + fn is_terminal(&self) -> bool { + self.is_terminal() + } +} + +#[cfg(all(windows, feature = "wincon"))] +impl IsTerminal for anstyle_wincon::Console> { + #[inline] + fn is_terminal(&self) -> bool { + self.is_terminal() + } +} + +impl IsTerminal for std::fs::File { + #[inline] + fn is_terminal(&self) -> bool { + std::io::IsTerminal::is_terminal(self) + } +} diff --git a/crates/anstream/src/lib.rs b/crates/anstream/src/lib.rs index 582fa522..1799c16b 100644 --- a/crates/anstream/src/lib.rs +++ b/crates/anstream/src/lib.rs @@ -39,6 +39,7 @@ mod buffer; #[macro_use] mod macros; mod auto; +mod is_terminal; mod lockable; mod raw; mod strip; @@ -46,6 +47,7 @@ mod strip; mod wincon; pub use auto::AutoStream; +pub use is_terminal::IsTerminal; pub use lockable::Lockable; pub use raw::RawStream; pub use strip::StripStream; diff --git a/crates/anstream/src/raw.rs b/crates/anstream/src/raw.rs index ecf7c685..4a54b400 100644 --- a/crates/anstream/src/raw.rs +++ b/crates/anstream/src/raw.rs @@ -1,19 +1,13 @@ -/// Required functionality for underlying [`std::io::Write`] for adaptation -#[cfg(not(any(feature = "auto", all(windows, feature = "wincon"))))] -pub trait RawStream: std::io::Write + private::Sealed {} - -/// Required functionality for underlying [`std::io::Write`] for adaptation -#[cfg(all(feature = "auto", not(all(windows, feature = "wincon"))))] -pub trait RawStream: std::io::Write + is_terminal::IsTerminal + private::Sealed {} +use crate::IsTerminal; /// Required functionality for underlying [`std::io::Write`] for adaptation -#[cfg(all(not(feature = "auto"), all(windows, feature = "wincon")))] -pub trait RawStream: std::io::Write + anstyle_wincon::WinconStream + private::Sealed {} +#[cfg(not(all(windows, feature = "wincon")))] +pub trait RawStream: std::io::Write + IsTerminal + private::Sealed {} /// Required functionality for underlying [`std::io::Write`] for adaptation -#[cfg(all(feature = "auto", all(windows, feature = "wincon")))] +#[cfg(all(windows, feature = "wincon"))] pub trait RawStream: - std::io::Write + is_terminal::IsTerminal + anstyle_wincon::WinconStream + private::Sealed + std::io::Write + IsTerminal + anstyle_wincon::WinconStream + private::Sealed { } diff --git a/crates/anstream/src/strip.rs b/crates/anstream/src/strip.rs index 0dd01749..a5b80987 100644 --- a/crates/anstream/src/strip.rs +++ b/crates/anstream/src/strip.rs @@ -1,4 +1,5 @@ use crate::adapter::StripBytes; +use crate::IsTerminal; use crate::Lockable; use crate::RawStream; @@ -29,14 +30,12 @@ where } #[inline] - #[cfg(feature = "auto")] pub fn is_terminal(&self) -> bool { self.raw.is_terminal() } } -#[cfg(feature = "auto")] -impl is_terminal::IsTerminal for StripStream +impl IsTerminal for StripStream where S: RawStream, { diff --git a/crates/anstream/src/wincon.rs b/crates/anstream/src/wincon.rs index e29b974d..8983db86 100644 --- a/crates/anstream/src/wincon.rs +++ b/crates/anstream/src/wincon.rs @@ -1,4 +1,5 @@ use crate::adapter::WinconBytes; +use crate::IsTerminal; use crate::Lockable; use crate::RawStream; @@ -36,7 +37,6 @@ where } #[inline] - #[cfg(feature = "auto")] pub fn is_terminal(&self) -> bool { // HACK: We can't get the console's stream to check but if there is a console, it likely is // a terminal @@ -44,8 +44,7 @@ where } } -#[cfg(feature = "auto")] -impl is_terminal::IsTerminal for WinconStream +impl IsTerminal for WinconStream where S: RawStream, {