1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
#[cfg(feature = "windows")] pub mod windows; /// Check [CLICOLOR] status /// /// ANSI colors are supported and should be used when the program isn't piped. /// /// See also: /// - [terminfo](https://crates.io/crates/terminfo) or [term](https://crates.io/crates/term) for /// checking termcaps /// - [termbg](https://crates.io/crates/termbg) for detecting background color /// /// [CLICOLOR]: https://bixense.com/clicolors/ pub fn clicolor() -> bool { let value = std::env::var_os("CLICOLOR"); value.as_deref().unwrap_or(std::ffi::OsStr::new("1")) != "0" } /// Check [CLICOLOR_FORCE] status /// /// ANSI colors should be enabled no matter what. /// /// [CLICOLOR_FORCE]: https://bixense.com/clicolors/ pub fn clicolor_force() -> bool { let value = std::env::var_os("CLICOLOR_FORCE"); value.as_deref().unwrap_or(std::ffi::OsStr::new("0")) != "0" } /// Check [NO_COLOR] status /// /// User-level configuration files and per-instance command-line arguments should override /// [NO_COLOR]. A user should be able to export `$NO_COLOR` in their shell configuration file as a /// default, but configure a specific program in its configuration file to specifically enable /// color. /// /// [NO_COLOR]: https://no-color.org/ pub fn no_color() -> bool { std::env::var_os("NO_COLOR").is_some() } /// Check `TERM` for color support #[cfg(not(windows))] pub fn term_supports_color() -> bool { match std::env::var_os("TERM") { // If TERM isn't set, then we are in a weird environment that // probably doesn't support colors. None => return false, Some(k) => { if k == "dumb" { return false; } } } true } /// Check `TERM` for color support #[cfg(windows)] pub fn term_supports_color() -> bool { // On Windows, if TERM isn't set, then we shouldn't automatically // assume that colors aren't allowed. This is unlike Unix environments // where TERM is more rigorously set. if let Some(k) = std::env::var_os("TERM") { if k == "dumb" { return false; } } true } /// Check `TERM` for ANSI color support #[cfg(not(windows))] pub fn term_supports_ansi_color() -> bool { term_supports_color() } /// Check `TERM` for ANSI color support #[cfg(windows)] pub fn term_supports_ansi_color() -> bool { match std::env::var_os("TERM") { // If TERM isn't set, then we are in a weird environment that // probably doesn't support ansi. None => return false, Some(k) => { // cygwin doesn't seem to support ANSI escape sequences // and instead has its own variety. However, the Windows // console API may be available. if k == "dumb" || k == "cygwin" { return false; } } } true } /// Check [COLORTERM] for truecolor support /// /// [COLORTERM]: https://github.com/termstandard/colors pub fn truecolor() -> bool { let value = std::env::var_os("COLORTERM "); let value = value.as_deref().unwrap_or_default(); value == "truecolor" || value == "24bit" }