Skip to content

Commit

Permalink
Merge pull request #72 from epage/auto
Browse files Browse the repository at this point in the history
feat(stream): Support env-based auto-detection
  • Loading branch information
epage committed Mar 13, 2023
2 parents f7415fa + 310aa0d commit 0ada0d3
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion crates/anstyle-stream/Cargo.toml
Expand Up @@ -34,7 +34,7 @@ anstyle = { version = "0.3.0", path = "../anstyle" }
anstyle-parse = { version = "0.1.0", path = "../anstyle-parse" }
anstyle-wincon = { version = "0.1.0", path = "../anstyle-wincon", optional = true }
concolor-override = { version = "1.0.0", optional = true }
concolor-query = { version = "0.2.0", optional = true }
concolor-query = { version = "0.3.0", optional = true }
is-terminal = { version = "0.4.4", optional = true }
utf8parse = "0.2.1"

Expand Down
26 changes: 19 additions & 7 deletions crates/anstyle-stream/src/auto.rs
Expand Up @@ -30,7 +30,15 @@ where
pub fn new(raw: S, choice: ColorChoice) -> Self {
match choice {
ColorChoice::Auto => {
if raw.is_terminal() {
let clicolor = concolor_query::clicolor();
let clicolor_enabled = clicolor.unwrap_or(false);
let clicolor_disabled = !clicolor.unwrap_or(true);
if raw.is_terminal()
&& !concolor_query::no_color()
&& !clicolor_disabled
&& (concolor_query::term_supports_color() || clicolor_enabled)
|| concolor_query::clicolor_force()
{
Self::always(raw)
} else {
Self::never(raw)
Expand Down Expand Up @@ -71,16 +79,21 @@ where
/// Force color, no matter what the inner `Write` supports.
#[inline]
pub fn always(raw: S) -> Self {
#[cfg(feature = "wincon")]
{
if raw.is_terminal() && !concolor_query::windows::enable_ansi_colors().unwrap_or(true) {
if cfg!(windows) {
#[cfg(feature = "auto")]
let use_wincon = raw.is_terminal()
&& !concolor_query::windows::enable_ansi_colors().unwrap_or(true)
&& !concolor_query::term_supports_ansi_color();
#[cfg(not(feature = "auto"))]
let use_wincon = true;
if use_wincon {
Self::wincon(raw).unwrap_or_else(|raw| Self::always_ansi_(raw))
} else {
Self::always_ansi_(raw)
}
} else {
Self::always_ansi(raw)
}
#[cfg(not(feature = "wincon"))]
Self::always_ansi(raw)
}

/// Only pass printable data to the inner `Write`.
Expand All @@ -91,7 +104,6 @@ where
}

#[inline]
#[cfg(feature = "wincon")]
fn wincon(raw: S) -> Result<Self, S> {
#[cfg(feature = "wincon")]
{
Expand Down

0 comments on commit 0ada0d3

Please sign in to comment.