Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace atty with is_terminal #10

Merged
merged 1 commit into from
Dec 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
atty = "0.2.14"
is-terminal = "0.4.0"
is_ci = "1.1.1"
36 changes: 24 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,17 @@
//! ```
#![allow(clippy::bool_to_int_with_if)]

pub use atty::Stream;

use std::cell::UnsafeCell;
use std::env;
use std::sync::Once;

/// possible stream sources
#[derive(Clone, Copy, Debug)]
pub enum Stream {
Stdout,
Stderr,
}

fn env_force_color() -> usize {
if let Ok(force) = env::var("FORCE_COLOR") {
match force.as_ref() {
Expand Down Expand Up @@ -75,11 +80,19 @@ fn translate_level(level: usize) -> Option<ColorLevel> {
}
}

fn is_a_tty(stream: Stream) -> bool {
use is_terminal::*;
match stream {
Stream::Stdout => std::io::stdout().is_terminal(),
Stream::Stderr => std::io::stderr().is_terminal(),
}
}

fn supports_color(stream: Stream) -> usize {
let force_color = env_force_color();
if force_color > 0 {
force_color
} else if env_no_color() || !atty::is(stream) || as_str(&env::var("TERM")) == Ok("dumb") {
} else if env_no_color() || as_str(&env::var("TERM")) == Ok("dumb") || !is_a_tty(stream) {
0
} else if as_str(&env::var("COLORTERM")) == Ok("truecolor")
|| as_str(&env::var("TERM_PROGRAM")) == Ok("iTerm.app")
Expand Down Expand Up @@ -128,23 +141,22 @@ struct CacheCell(UnsafeCell<Option<ColorLevel>>);

unsafe impl Sync for CacheCell {}

static INIT: [Once; 3] = [Once::new(), Once::new(), Once::new()];
static ON_CACHE: [CacheCell; 3] = [
CacheCell(UnsafeCell::new(None)),
static INIT: [Once; 2] = [Once::new(), Once::new()];
static ON_CACHE: [CacheCell; 2] = [
CacheCell(UnsafeCell::new(None)),
CacheCell(UnsafeCell::new(None)),
];

macro_rules! assert_stream_in_bounds {
($($variant:ident)*) => {
$(
const _: () = [(); 3][Stream::$variant as usize];
const _: () = [(); 2][Stream::$variant as usize];
)*
};
}

// Compile-time assertion that the below indexing will never panic
assert_stream_in_bounds!(Stdout Stderr Stdin);
assert_stream_in_bounds!(Stdout Stderr);

/**
Returns a [ColorLevel] if a [Stream] supports terminal colors, caching the result to
Expand Down Expand Up @@ -197,7 +209,7 @@ mod tests {
let _test_guard = TEST_LOCK.lock().unwrap();
set_up();

assert_eq!(on(atty::Stream::Stdout), None);
assert_eq!(on(Stream::Stdout), None);
}

#[test]
Expand All @@ -213,10 +225,10 @@ mod tests {
has_256: false,
has_16m: false,
});
assert_eq!(on(atty::Stream::Stdout), expected);
assert_eq!(on(Stream::Stdout), expected);

env::set_var("CLICOLOR", "0");
assert_eq!(on(atty::Stream::Stdout), None);
assert_eq!(on(Stream::Stdout), None);
}

#[test]
Expand All @@ -233,6 +245,6 @@ mod tests {
has_256: false,
has_16m: false,
});
assert_eq!(on(atty::Stream::Stdout), expected);
assert_eq!(on(Stream::Stdout), expected);
}
}