Skip to content

Commit

Permalink
perf(anstream)!: Drop is-terminal dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Aug 23, 2023
1 parent 3b26d6b commit ee53d54
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 23 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions crates/anstream/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,14 @@ 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]
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]
Expand Down
3 changes: 2 additions & 1 deletion crates/anstream/src/auto.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#[cfg(feature = "auto")]
use crate::ColorChoice;
use crate::IsTerminal;
use crate::Lockable;
use crate::RawStream;
use crate::StripStream;
Expand Down Expand Up @@ -161,7 +162,7 @@ fn choice(raw: &dyn RawStream) -> ColorChoice {
}

#[cfg(feature = "auto")]
impl<S> is_terminal::IsTerminal for AutoStream<S>
impl<S> IsTerminal for AutoStream<S>
where
S: RawStream,
{
Expand Down
5 changes: 3 additions & 2 deletions crates/anstream/src/buffer.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use crate::IsTerminal;

/// In-memory [`RawStream`][crate::RawStream]
#[derive(Clone, Default, Debug, PartialEq, Eq)]
pub struct Buffer(Vec<u8>);
Expand Down Expand Up @@ -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
Expand Down
70 changes: 70 additions & 0 deletions crates/anstream/src/is_terminal.rs
Original file line number Diff line number Diff line change
@@ -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<std::io::Stdout> {
#[inline]
fn is_terminal(&self) -> bool {
self.is_terminal()
}
}

#[cfg(all(windows, feature = "wincon"))]
impl IsTerminal for anstyle_wincon::Console<std::io::StdoutLock<'static>> {
#[inline]
fn is_terminal(&self) -> bool {
self.is_terminal()
}
}

#[cfg(all(windows, feature = "wincon"))]
impl IsTerminal for anstyle_wincon::Console<std::io::Stderr> {
#[inline]
fn is_terminal(&self) -> bool {
self.is_terminal()
}
}

#[cfg(all(windows, feature = "wincon"))]
impl IsTerminal for anstyle_wincon::Console<std::io::StderrLock<'static>> {
#[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)
}
}
2 changes: 2 additions & 0 deletions crates/anstream/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,15 @@ mod buffer;
#[macro_use]
mod macros;
mod auto;
mod is_terminal;
mod lockable;
mod raw;
mod strip;
#[cfg(all(windows, feature = "wincon"))]
mod wincon;

pub use auto::AutoStream;
pub use is_terminal::IsTerminal;
pub use lockable::Lockable;
pub use raw::RawStream;
pub use strip::StripStream;
Expand Down
16 changes: 5 additions & 11 deletions crates/anstream/src/raw.rs
Original file line number Diff line number Diff line change
@@ -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
{
}

Expand Down
5 changes: 2 additions & 3 deletions crates/anstream/src/strip.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::adapter::StripBytes;
use crate::IsTerminal;
use crate::Lockable;
use crate::RawStream;

Expand Down Expand Up @@ -29,14 +30,12 @@ where
}

#[inline]
#[cfg(feature = "auto")]
pub fn is_terminal(&self) -> bool {
self.raw.is_terminal()
}
}

#[cfg(feature = "auto")]
impl<S> is_terminal::IsTerminal for StripStream<S>
impl<S> IsTerminal for StripStream<S>
where
S: RawStream,
{
Expand Down
5 changes: 2 additions & 3 deletions crates/anstream/src/wincon.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::adapter::WinconBytes;
use crate::IsTerminal;
use crate::Lockable;
use crate::RawStream;

Expand Down Expand Up @@ -36,16 +37,14 @@ 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
true
}
}

#[cfg(feature = "auto")]
impl<S> is_terminal::IsTerminal for WinconStream<S>
impl<S> IsTerminal for WinconStream<S>
where
S: RawStream,
{
Expand Down

0 comments on commit ee53d54

Please sign in to comment.