this repo has no description
at main 62 lines 1.5 kB view raw
1use std::sync::OnceLock; 2 3static NO_COLOR: OnceLock<bool> = OnceLock::new(); 4 5/// Initialize the color configuration based on CLI flag and environment. 6/// Should be called once at startup. 7pub fn init(cli_no_color: bool) { 8 let no_color = cli_no_color || should_disable_color_from_env(); 9 NO_COLOR.set(no_color).ok(); 10} 11 12/// Returns true if colors should be disabled. 13pub fn no_color() -> bool { 14 *NO_COLOR.get().unwrap_or(&false) 15} 16 17/// Returns true if colors are enabled. 18pub fn color_enabled() -> bool { 19 !no_color() 20} 21 22/// Check environment variables to determine if color should be disabled. 23fn should_disable_color_from_env() -> bool { 24 // NO_COLOR standard: https://no-color.org/ 25 // If NO_COLOR exists (with any value), disable color 26 if std::env::var("NO_COLOR").is_ok() { 27 return true; 28 } 29 30 // Generic CI detection 31 if std::env::var("CI").is_ok_and(|v| v == "true") { 32 return true; 33 } 34 35 // GitHub actions 36 if std::env::var("GITHUB_ACTIONS").is_ok_and(|v| v == "true") { 37 return true; 38 } 39 40 // GitLab CI 41 if std::env::var("GITLAB_CI").is_ok() { 42 return true; 43 } 44 45 // Travis CI 46 if std::env::var("TRAVIS").is_ok_and(|v| v == "true") { 47 return true; 48 } 49 50 // Jenkins 51 if std::env::var("JENKINS_URL").is_ok() { 52 return true; 53 } 54 55 // Check if output is not a TTY (piped) 56 // This is a common convention for disabling colors 57 if std::env::var("TERM").is_ok_and(|v| v == "dumb") { 58 return true; 59 } 60 61 false 62}