use crate::{
BLINKING,
BOLD,
RESET,
};
#[cfg(unix)]
use std::os::fd::AsFd;
use std::{
cell::{
self as std_cell,
Cell,
},
env,
ffi::{
OsStr,
OsString,
},
fmt as std_fmt,
io::IsTerminal,
sync::{
Mutex,
MutexGuard,
},
};
#[derive(Clone)]
#[derive(Copy)]
#[derive(Debug)]
#[derive(Eq, PartialEq)]
#[repr(i32)]
pub enum ColourPolicy {
Auto = 0,
Force = 1,
Inhibit = 2,
}
impl ColourPolicy {
pub const fn as_str(self) -> &'static str {
match self {
Self::Auto => "auto",
Self::Force => "force",
Self::Inhibit => "inhibit",
}
}
}
impl std_fmt::Display for ColourPolicy {
fn fmt(
&self,
f : &mut std_fmt::Formatter<'_>,
) -> std_fmt::Result {
f.write_str(self.as_str())
}
}
const TRUEY : &[&str] = &["1", "always", "force", "on", "true", "yes"];
const FALSEY : &[&str] = &["0", "false", "never", "no", "none", "off"];
struct Environment {
determined : bool,
policy : ColourPolicy,
term_is_dumb : bool,
}
struct Cache(std_cell::UnsafeCell<Environment>);
unsafe impl Sync for Cache {
}
static CACHE : Cache = Cache(std_cell::UnsafeCell::new(Environment {
determined : false,
policy : ColourPolicy::Auto,
term_is_dumb : false,
}));
static CACHE_GATE : Mutex<()> = Mutex::new(());
thread_local! {
static CACHE_HELD : Cell<bool> = const { Cell::new(false) };
}
fn ensure(env : &mut Environment) {
if !env.determined {
env.policy = policy_from_environment();
env.term_is_dumb = term_is_dumb_from_environment();
env.determined = true;
}
}
fn eq_ignore_ascii(
value : &OsStr,
candidate : &str,
) -> bool {
match value.to_str() {
Some(text) => text.eq_ignore_ascii_case(candidate),
None => false,
}
}
fn is_listed(
value : &OsStr,
candidates : &[&str],
) -> bool {
candidates.iter().any(|candidate| eq_ignore_ascii(value, candidate))
}
fn lock_gate() -> MutexGuard<'static, ()> {
CACHE_GATE.lock().unwrap_or_else(|poisoned| poisoned.into_inner())
}
fn nonempty(name : &str) -> Option<OsString> {
let value = env::var_os(name)?;
if value.is_empty() {
None
} else {
Some(value)
}
}
fn policy_from_environment() -> ColourPolicy {
if let Some(value) = nonempty("WOAD_COLOUR").or_else(|| nonempty("WOAD_COLOR")) {
if is_listed(&value, TRUEY) {
return ColourPolicy::Force;
}
if is_listed(&value, FALSEY) {
return ColourPolicy::Inhibit;
}
return ColourPolicy::Auto;
}
if nonempty("NO_COLOR").is_some() {
return ColourPolicy::Inhibit;
}
if let Some(value) = nonempty("CLICOLOR_FORCE") {
if value != OsStr::new("0") {
return ColourPolicy::Force;
}
}
if let Some(value) = nonempty("FORCE_COLOR") {
if value != OsStr::new("0") && !eq_ignore_ascii(&value, "false") {
return ColourPolicy::Force;
}
}
if let Some(value) = nonempty("CLICOLOR") {
if value == OsStr::new("0") {
return ColourPolicy::Inhibit;
}
}
ColourPolicy::Auto
}
fn supports_for(
env : &mut Environment,
is_terminal : bool,
) -> bool {
ensure(env);
match env.policy {
ColourPolicy::Auto => is_terminal && !env.term_is_dumb,
ColourPolicy::Force => true,
ColourPolicy::Inhibit => false,
}
}
fn supports_when_attached(is_terminal : bool) -> bool {
with_environment(|env| supports_for(env, is_terminal))
}
fn term_is_dumb_from_environment() -> bool {
match nonempty("TERM") {
Some(value) => eq_ignore_ascii(&value, "dumb"),
None => false,
}
}
fn with_environment<R>(body : impl FnOnce(&mut Environment) -> R) -> R {
struct ClearCacheHeld;
impl Drop for ClearCacheHeld {
fn drop(&mut self) {
CACHE_HELD.set(false);
}
}
if CACHE_HELD.get() {
body(unsafe { &mut *CACHE.0.get() })
} else {
let _guard = lock_gate();
CACHE_HELD.set(true);
let _reset = ClearCacheHeld;
body(unsafe { &mut *CACHE.0.get() })
}
}
pub fn blinking_for(target : &impl IsTerminal) -> &'static str {
seq_for(BLINKING, target)
}
pub fn bold_for(target : &impl IsTerminal) -> &'static str {
seq_for(BOLD, target)
}
pub fn colour_policy() -> ColourPolicy {
with_environment(|env| {
ensure(env);
env.policy
})
}
pub fn console_supports_ansi() -> bool {
true
}
#[cfg(unix)]
pub fn fd_is_tty(fd : impl AsFd) -> bool {
is_tty(&fd.as_fd())
}
#[cfg(unix)]
pub fn fd_supports_colour(fd : impl AsFd) -> bool {
supports_colour(&fd.as_fd())
}
pub fn is_tty<W>(writer : &W) -> bool
where
W : IsTerminal + ?Sized,
{
writer.is_terminal()
}
pub fn refresh() {
with_environment(|env| {
env.determined = false;
});
}
pub fn reset_for(target : &impl IsTerminal) -> &'static str {
seq_for(RESET, target)
}
pub fn seq_for<'seq, W>(
seq : &'seq str,
writer : &W,
) -> &'seq str
where
W : IsTerminal + ?Sized,
{
if seq.is_empty() || !supports_colour(writer) {
""
} else {
seq
}
}
pub fn seq_for_absent(seq : &str) -> &str {
if seq.is_empty() || !supports_colour_absent() {
""
} else {
seq
}
}
#[cfg(unix)]
pub fn seq_for_fd(
seq : &str,
fd : impl AsFd,
) -> &str {
seq_for(seq, &fd.as_fd())
}
pub fn supports_colour<W>(writer : &W) -> bool
where
W : IsTerminal + ?Sized,
{
supports_when_attached(writer.is_terminal())
}
pub fn supports_colour_absent() -> bool {
supports_when_attached(false)
}
#[cfg(all(test, unix))]
mod tests {
#![allow(non_snake_case)]
use std::{
ffi as std_ffi,
fs::{
File,
OpenOptions,
},
io as std_io,
os::{
fd::FromRawFd,
unix::{
ffi::OsStrExt,
fs::OpenOptionsExt,
},
},
sync::OnceLock,
};
use super::*;
const MANAGED_VARS : &[&str] = &[
"CLICOLOR",
"CLICOLOR_FORCE",
"FORCE_COLOR",
"NO_COLOR",
"TERM",
"WOAD_COLOR",
"WOAD_COLOUR",
];
struct SavedVar {
name : &'static str,
value : Option<OsString>,
}
fn capture_managed() -> Vec<SavedVar> {
MANAGED_VARS
.iter()
.map(|name| {
SavedVar {
name,
value : env::var_os(name),
}
})
.collect()
}
fn apply_saved(saved : &[SavedVar]) {
for item in saved {
match &item.value {
Some(value) => unsafe { env::set_var(item.name, value) },
None => unsafe { env::remove_var(item.name) },
}
}
}
fn assign(
name : &str,
value : &str,
) {
unsafe { env::set_var(name, value) };
}
fn clear_managed() {
for name in MANAGED_VARS {
unsafe { env::remove_var(name) };
}
}
fn with_managed<R>(
assignments : &[(&str, &str)],
body : impl FnOnce() -> R,
) -> R {
with_environment(|_| {
let saved = capture_managed();
clear_managed();
for (name, value) in assignments {
assign(name, value);
}
refresh();
let result = body();
apply_saved(&saved);
refresh();
result
})
}
struct Endpoints {
terminal : File,
_master : File,
detached : File,
}
fn endpoints() -> &'static Endpoints {
static ENDPOINTS : OnceLock<Endpoints> = OnceLock::new();
ENDPOINTS.get_or_init(|| {
let (terminal, master) = open_pty_slave();
let detached = File::open("/dev/null").expect("open /dev/null");
assert!(terminal.is_terminal());
assert!(!detached.is_terminal());
Endpoints {
terminal,
_master : master,
detached,
}
})
}
fn open_pty_slave() -> (File, File) {
#[cfg(any(target_os = "android", target_os = "linux"))]
const O_NOCTTY : i32 = 0x100;
#[cfg(target_os = "macos")]
const O_NOCTTY : i32 = 0x2_0000;
#[cfg(not(any(target_os = "android", target_os = "linux", target_os = "macos")))]
const O_NOCTTY : i32 = 0x8000;
const O_RDWR : i32 = 2;
extern "C" {
fn close(fd : i32) -> i32;
fn grantpt(fd : i32) -> i32;
fn posix_openpt(flags : i32) -> i32;
fn ptsname(fd : i32) -> *mut std_ffi::c_char;
fn unlockpt(fd : i32) -> i32;
}
unsafe {
let master = posix_openpt(O_RDWR | O_NOCTTY);
assert!(master >= 0, "posix_openpt: {}", std_io::Error::last_os_error());
if grantpt(master) != 0 || unlockpt(master) != 0 {
let err = std_io::Error::last_os_error();
close(master);
panic!("grantpt/unlockpt: {err}");
}
let name = ptsname(master);
assert!(!name.is_null(), "ptsname");
let path = std_ffi::OsStr::from_bytes(std_ffi::CStr::from_ptr(name).to_bytes());
let slave = match OpenOptions::new()
.read(true)
.write(true)
.custom_flags(O_NOCTTY)
.open(path)
{
Ok(file) => file,
Err(err) => {
close(master);
panic!("open slave: {err}");
},
};
(slave, File::from_raw_fd(master))
}
}
fn writer(terminal : bool) -> &'static File {
let endpoints = endpoints();
if terminal {
&endpoints.terminal
} else {
&endpoints.detached
}
}
fn assert_row(
row : u32,
assignments : &[(&str, &str)],
terminal : bool,
policy : ColourPolicy,
colour : bool,
) {
with_managed(assignments, || {
let attached = writer(terminal);
assert_eq!(is_tty(attached), terminal, "row {row} attachment");
assert_eq!(colour_policy(), policy, "row {row} policy");
assert_eq!(supports_colour(attached), colour, "row {row} colour");
let seq = seq_for(crate::FG_RED, attached);
if colour {
assert_eq!(seq, crate::FG_RED, "row {row} sequence");
} else {
assert_eq!(seq, "", "row {row} sequence");
}
});
}
#[test]
fn TEST_ROW_1_NONE_TERMINAL() {
assert_row(1, &[], true, ColourPolicy::Auto, true);
}
#[test]
fn TEST_ROW_2_NONE_DETACHED() {
assert_row(2, &[], false, ColourPolicy::Auto, false);
}
#[test]
fn TEST_ROW_3_TERM_DUMB() {
assert_row(3, &[("TERM", "dumb")], true, ColourPolicy::Auto, false);
}
#[test]
fn TEST_ROW_4_TERM_DUMB_UPPER() {
assert_row(4, &[("TERM", "DUMB")], true, ColourPolicy::Auto, false);
}
#[test]
fn TEST_ROW_5_WOAD_COLOUR_1() {
assert_row(5, &[("WOAD_COLOUR", "1")], false, ColourPolicy::Force, true);
}
#[test]
fn TEST_ROW_6_WOAD_COLOR_ALWAYS() {
assert_row(6, &[("WOAD_COLOR", "always")], false, ColourPolicy::Force, true);
}
#[test]
fn TEST_ROW_7_WOAD_COLOUR_NEVER() {
assert_row(7, &[("WOAD_COLOUR", "never")], true, ColourPolicy::Inhibit, false);
}
#[test]
fn TEST_ROW_8_WOAD_COLOUR_AUTO_TERMINAL() {
assert_row(8, &[("WOAD_COLOUR", "auto")], true, ColourPolicy::Auto, true);
}
#[test]
fn TEST_ROW_9_WOAD_COLOUR_AUTO_DETACHED() {
assert_row(9, &[("WOAD_COLOUR", "auto")], false, ColourPolicy::Auto, false);
}
#[test]
fn TEST_ROW_10_WOAD_COLOUR_PURPLE() {
assert_row(10, &[("WOAD_COLOUR", "purple")], true, ColourPolicy::Auto, true);
}
#[test]
fn TEST_ROW_11_NO_COLOR_BEATS_CLICOLOR_FORCE() {
assert_row(
11,
&[("CLICOLOR_FORCE", "1"), ("NO_COLOR", "1")],
true,
ColourPolicy::Inhibit,
false,
);
}
#[test]
fn TEST_ROW_12_CLICOLOR_FORCE() {
assert_row(12, &[("CLICOLOR_FORCE", "1")], false, ColourPolicy::Force, true);
}
#[test]
fn TEST_ROW_13_CLICOLOR_FORCE_0_THEN_CLICOLOR_0() {
assert_row(
13,
&[("CLICOLOR", "0"), ("CLICOLOR_FORCE", "0")],
true,
ColourPolicy::Inhibit,
false,
);
}
#[test]
fn TEST_ROW_14_FORCE_COLOR() {
assert_row(14, &[("FORCE_COLOR", "1")], false, ColourPolicy::Force, true);
}
#[test]
fn TEST_ROW_15_FORCE_COLOR_FALSE_THEN_CLICOLOR_0() {
assert_row(
15,
&[("CLICOLOR", "0"), ("FORCE_COLOR", "false")],
true,
ColourPolicy::Inhibit,
false,
);
}
#[test]
fn TEST_ROW_16_FORCE_COLOR_FALSE_UPPER() {
assert_row(16, &[("FORCE_COLOR", "FALSE")], false, ColourPolicy::Auto, false);
}
#[test]
fn TEST_ROW_17_FORCE_COLOR_0() {
assert_row(17, &[("FORCE_COLOR", "0")], true, ColourPolicy::Auto, true);
}
#[test]
fn TEST_ROW_18_CLICOLOR_0() {
assert_row(18, &[("CLICOLOR", "0")], true, ColourPolicy::Inhibit, false);
}
#[test]
fn TEST_ROW_19_CLICOLOR_1() {
assert_row(19, &[("CLICOLOR", "1")], true, ColourPolicy::Auto, true);
}
#[test]
fn TEST_ROW_20_EMPTY_WOAD_COLOUR_FALLS_THROUGH() {
assert_row(
20,
&[("NO_COLOR", "1"), ("WOAD_COLOUR", "")],
true,
ColourPolicy::Inhibit,
false,
);
}
#[test]
fn TEST_ROW_21_EMPTY_NO_COLOR_IS_ABSENT() {
assert_row(21, &[("NO_COLOR", "")], true, ColourPolicy::Auto, true);
}
#[test]
fn TEST_ROW_22_REFRESH_DROPS_THE_CACHE() {
with_managed(&[], || {
assign("WOAD_COLOUR", "1");
refresh();
let attached = writer(true);
assert_eq!(colour_policy(), ColourPolicy::Force, "row 22 before refresh");
assert!(supports_colour(attached), "row 22 before refresh");
unsafe { env::remove_var("WOAD_COLOUR") };
assign("NO_COLOR", "1");
refresh();
assert_eq!(colour_policy(), ColourPolicy::Inhibit, "row 22 after refresh");
assert!(!supports_colour(attached), "row 22 after refresh");
assert_eq!(seq_for(crate::BOLD, attached), "");
assert!(is_tty(attached), "row 22 attachment stays a terminal");
});
}
#[test]
fn TEST_SEQ_FOR_EMPTY_IS_EMPTY() {
with_managed(&[("WOAD_COLOUR", "1")], || {
assert_eq!(seq_for("", writer(false)), "");
});
}
}