Skip to content

Commit

Permalink
fix(deps): Replace atty with is_terminal (#10)
Browse files Browse the repository at this point in the history
Fixes: #9

BREAKING CHANGE: Exported stream types are no longer atty's getting re-exported.
  • Loading branch information
pacak committed Dec 15, 2022
1 parent 5481a51 commit edf565e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
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
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);
}
}

0 comments on commit edf565e

Please sign in to comment.