#[must_use]
pub fn no_color() -> bool {
std::env::var("NO_COLOR").is_ok()
}
#[must_use]
pub fn no_emoji() -> bool {
std::env::var("NO_EMOJI").is_ok()
}
#[must_use]
pub fn no_animation() -> bool {
std::env::var("PREFER_REDUCED_MOTION").is_ok()
}
pub trait Capabilities {
fn is_color_disabled(&self) -> bool;
fn is_emoji_disabled(&self) -> bool;
fn prefers_reduced_motion(&self) -> bool;
}
pub struct Env;
impl Capabilities for Env {
fn is_color_disabled(&self) -> bool {
no_color()
}
fn is_emoji_disabled(&self) -> bool {
no_emoji()
}
fn prefers_reduced_motion(&self) -> bool {
no_animation()
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct Settings {
pub no_color: bool,
pub no_emoji: bool,
pub no_animation: bool,
}
impl Settings {
#[must_use]
pub fn from_environment() -> Self {
Self {
no_color: no_color(),
no_emoji: no_emoji(),
no_animation: no_animation(),
}
}
}
impl Capabilities for Settings {
fn is_color_disabled(&self) -> bool {
self.no_color
}
fn is_emoji_disabled(&self) -> bool {
self.no_emoji
}
fn prefers_reduced_motion(&self) -> bool {
self.no_animation
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_no_color_default() {
let _ = no_color();
}
#[test]
fn test_no_emoji_default() {
let _ = no_emoji();
}
#[test]
fn test_no_animation_default() {
let _ = no_animation();
}
#[test]
fn test_terminal_settings_default() {
let settings = Settings::default();
assert!(!settings.is_color_disabled() || no_color());
}
#[test]
fn test_terminal_settings_from_environment() {
let settings = Settings::from_environment();
assert_eq!(settings.is_color_disabled(), no_color());
assert_eq!(settings.is_emoji_disabled(), no_emoji());
assert_eq!(settings.prefers_reduced_motion(), no_animation());
}
#[test]
fn test_default_terminal_trait() {
let terminal = Env;
let _ = terminal.is_color_disabled();
let _ = terminal.is_emoji_disabled();
let _ = terminal.prefers_reduced_motion();
}
}