use std::env;
pub struct TerminalDetector {
is_tty: bool,
is_ci: bool,
force_mode: Option<TerminalMode>,
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum TerminalMode {
Tty,
#[allow(dead_code)]
Plain,
}
impl TerminalDetector {
pub fn new() -> Self {
let is_tty = atty::is(atty::Stream::Stdout);
let is_ci = Self::detect_ci();
Self {
is_tty,
is_ci,
force_mode: None,
}
}
#[allow(dead_code)]
pub fn with_mode(mut self, mode: TerminalMode) -> Self {
self.force_mode = Some(mode);
self
}
pub fn is_tty(&self) -> bool {
if let Some(mode) = self.force_mode {
return mode == TerminalMode::Tty;
}
self.is_tty && !self.is_ci
}
#[allow(dead_code)]
pub fn is_ci_environment(&self) -> bool {
self.is_ci
}
fn detect_ci() -> bool {
env::var("CI").is_ok()
|| env::var("CONTINUOUS_INTEGRATION").is_ok()
|| env::var("GITHUB_ACTIONS").is_ok()
|| env::var("GITLAB_CI").is_ok()
|| env::var("TRAVIS").is_ok()
|| env::var("CIRCLECI").is_ok()
|| env::var("JENKINS_URL").is_ok()
|| env::var("TEAMCITY_VERSION").is_ok()
}
#[allow(dead_code)]
pub fn output_mode(&self) -> TerminalMode {
if self.is_tty() {
TerminalMode::Tty
} else {
TerminalMode::Plain
}
}
#[allow(dead_code)]
pub fn should_use_colors(&self) -> bool {
if !self.is_tty() {
return false;
}
if let Ok(val) = env::var("NO_COLOR") {
if !val.is_empty() {
return false;
}
}
if let Ok(val) = env::var("FORCE_COLOR") {
if !val.is_empty() && val != "0" {
return true;
}
}
if let Ok(term) = env::var("TERM") {
if term == "dumb" {
return false;
}
}
true
}
pub fn supports_unicode(&self) -> bool {
if !self.is_tty() {
return false;
}
if let Ok(lang) = env::var("LANG") {
if lang.to_lowercase().contains("utf-8") || lang.to_lowercase().contains("utf8") {
return true;
}
}
if let Ok(lc_all) = env::var("LC_ALL") {
if lc_all.to_lowercase().contains("utf-8") || lc_all.to_lowercase().contains("utf8") {
return true;
}
}
cfg!(not(windows)) || env::var("WT_SESSION").is_ok() }
}
impl Default for TerminalDetector {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_force_mode() {
let detector = TerminalDetector::new().with_mode(TerminalMode::Plain);
assert!(!detector.is_tty());
let detector = TerminalDetector::new().with_mode(TerminalMode::Tty);
assert!(detector.is_tty());
}
#[test]
fn test_output_mode() {
let detector = TerminalDetector::new().with_mode(TerminalMode::Tty);
assert_eq!(detector.output_mode(), TerminalMode::Tty);
let detector = TerminalDetector::new().with_mode(TerminalMode::Plain);
assert_eq!(detector.output_mode(), TerminalMode::Plain);
}
#[test]
fn test_terminal_mode_precedence() {
let detector = TerminalDetector::new().with_mode(TerminalMode::Tty);
assert!(detector.is_tty());
let detector = TerminalDetector::new().with_mode(TerminalMode::Plain);
assert!(!detector.is_tty());
}
#[test]
fn test_ci_detection() {
let original_ci = env::var("CI").ok();
let original_github = env::var("GITHUB_ACTIONS").ok();
env::set_var("CI", "true");
let detector = TerminalDetector::new();
assert!(detector.is_ci_environment());
env::remove_var("CI");
env::set_var("CI", "1");
let detector = TerminalDetector::new();
assert!(detector.is_ci_environment());
env::remove_var("CI");
env::set_var("GITHUB_ACTIONS", "true");
let detector = TerminalDetector::new();
assert!(detector.is_ci_environment());
env::remove_var("GITHUB_ACTIONS");
if let Some(val) = original_ci {
env::set_var("CI", val);
}
if let Some(val) = original_github {
env::set_var("GITHUB_ACTIONS", val);
}
}
#[test]
fn test_terminal_tty_detection() {
let detector = TerminalDetector::new().with_mode(TerminalMode::Plain);
assert!(!detector.is_tty());
let detector = TerminalDetector::new().with_mode(TerminalMode::Tty);
assert!(detector.is_tty());
}
}