use core::fmt;
use core::sync::atomic::AtomicU8;
use core::sync::atomic::{AtomicPtr, Ordering};
#[repr(transparent)]
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Condition(
pub fn() -> bool
);
#[repr(transparent)]
pub struct AtomicCondition(AtomicPtr<()>);
#[allow(unused)]
#[repr(transparent)]
pub struct CachedBool(AtomicU8);
impl Condition {
pub const DEFAULT: Condition = Condition(Condition::os_support);
pub const ALWAYS: Condition = Condition(Condition::always);
pub const NEVER: Condition = Condition(Condition::never);
pub const fn from(f: fn() -> bool) -> Self {
Condition(f)
}
pub const fn cached(value: bool) -> Self {
match value {
true => Condition::ALWAYS,
false => Condition::NEVER,
}
}
pub const fn always() -> bool { true }
pub const fn never() -> bool { false }
pub fn os_support() -> bool {
crate::windows::cache_enable()
}
}
impl Default for Condition {
fn default() -> Self {
Condition::DEFAULT
}
}
impl core::ops::Deref for Condition {
type Target = fn() -> bool;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl AtomicCondition {
pub const DEFAULT: AtomicCondition = AtomicCondition::from(Condition::DEFAULT);
pub const fn from(value: Condition) -> Self {
AtomicCondition(AtomicPtr::new(value.0 as *mut ()))
}
pub fn store(&self, cond: Condition) {
self.0.store(cond.0 as *mut (), Ordering::Release)
}
pub fn read(&self) -> bool {
let condition = unsafe {
Condition(core::mem::transmute(self.0.load(Ordering::Acquire)))
};
condition()
}
}
#[allow(unused)]
impl CachedBool {
const TRUE: u8 = 1;
const UNINIT: u8 = 2;
const INITING: u8 = 3;
pub const fn new() -> Self {
CachedBool(AtomicU8::new(Self::UNINIT))
}
pub fn get_or_init(&self, f: impl FnOnce() -> bool) -> bool {
use core::sync::atomic::Ordering::*;
match self.0.compare_exchange(Self::UNINIT, Self::INITING, AcqRel, Relaxed) {
Ok(_) => {
let new_value = f();
self.0.store(new_value as u8 , Release);
new_value
}
Err(Self::INITING) => {
let mut value;
while { value = self.0.load(Acquire); value } == Self::INITING {
#[cfg(feature = "std")]
std::thread::yield_now();
}
value == Self::TRUE
},
Err(value) => value == Self::TRUE,
}
}
}
impl fmt::Debug for Condition {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if *self == Condition::DEFAULT {
f.write_str("Condition::DEFAULT")
} else if *self == Condition::ALWAYS {
f.write_str("Condition::ALWAYS")
} else if *self == Condition::NEVER {
f.write_str("Condition::NEVER")
} else {
f.debug_tuple("Condition").field(&self.0).finish()
}
}
}
macro_rules! conditions {
($feat:meta $($f:expr, $CACHED:ident: $cached:ident, $LIVE:ident: $live:ident),* $(,)?) => (
#[cfg($feat)]
#[cfg_attr(feature = "_nightly", doc(cfg($feat)))]
#[doc = concat!('`', stringify!($feat), "`.")]
impl Condition {
$(
#[doc = concat!('`', stringify!($f), "`.")]
#[doc = concat!("[`", stringify!($cached), "`](Condition::", stringify!($cached), ").")]
pub const $CACHED: Condition = Condition(Condition::$cached);
)*
$(
#[doc = concat!('`', stringify!($f), "`.")]
#[doc = concat!("[`", stringify!($CACHED), "`](Condition::", stringify!($CACHED), ")")]
#[doc = concat!("[`", stringify!($live), "`](Condition::", stringify!($live), ").")]
pub const $LIVE: Condition = Condition(Condition::$live);
)*
$(
#[doc = concat!('`', stringify!($f), "`.")]
#[doc = concat!("[`", stringify!($CACHED), "`](Condition::", stringify!($CACHED), ").")]
pub fn $cached() -> bool {
static IS_TTY: CachedBool = CachedBool::new();
IS_TTY.get_or_init(Condition::$live)
}
)*
$(
#[doc = concat!('`', stringify!($f), "`.")]
#[doc = concat!("[`", stringify!($LIVE), "`](Condition::", stringify!($LIVE), ").")]
pub fn $live() -> bool {
$f
}
)*
}
)
}
#[cfg(feature = "detect-tty")]
use is_terminal::is_terminal as is_tty;
conditions! { feature = "detect-tty"
is_tty(&std::io::stdout()),
STDOUT_IS_TTY: stdout_is_tty,
STDOUT_IS_TTY_LIVE: stdout_is_tty_live,
is_tty(&std::io::stderr()),
STDERR_IS_TTY: stderr_is_tty,
STDERR_IS_TTY_LIVE: stderr_is_tty_live,
is_tty(&std::io::stdin()),
STDIN_IS_TTY: stdin_is_tty,
STDIN_IS_TTY_LIVE: stdin_is_tty_live,
is_tty(&std::io::stdout()) && is_tty(&std::io::stderr()),
STDOUTERR_ARE_TTY: stdouterr_are_tty,
STDOUTERR_ARE_TTY_LIVE: stdouterr_are_tty_live,
}
#[cfg(feature = "detect-env")]
pub fn env_set_or(name: &str, default: bool) -> bool {
std::env::var_os(name).map_or(default, |v| v != "0")
}
conditions! { feature = "detect-env"
env_set_or("CLICOLOR_FORCE", false) || env_set_or("CLICOLOR", true),
CLICOLOR: clicolor,
CLICOLOR_LIVE: clicolor_live,
!env_set_or("NO_COLOR", false),
YES_COLOR: no_color,
YES_COLOR_LIVE: no_color_live,
}
conditions! { all(feature = "detect-env", feature = "detect-tty")
Condition::stdouterr_are_tty() && Condition::clicolor() && Condition::no_color(),
TTY_AND_COLOR: tty_and_color,
TTY_AND_COLOR_LIVE: tty_and_color_live,
}