Skip to content

Commit

Permalink
Merge pull request #194 from epage/query
Browse files Browse the repository at this point in the history
fix(query)!: Update CLICOLOR_FORCE to check non-empty
  • Loading branch information
epage authored Jun 4, 2024
2 parents f9cb13d + 1d210c7 commit b922b24
Showing 1 changed file with 26 additions and 7 deletions.
33 changes: 26 additions & 7 deletions crates/anstyle-query/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,7 @@ pub fn clicolor() -> Option<bool> {
/// [CLICOLOR_FORCE]: https://bixense.com/clicolors/
#[inline]
pub fn clicolor_force() -> bool {
let value = std::env::var_os("CLICOLOR_FORCE");
value
.as_deref()
.unwrap_or_else(|| std::ffi::OsStr::new("0"))
!= "0"
non_empty(std::env::var_os("CLICOLOR_FORCE").as_deref())
}

/// Check [NO_COLOR] status
Expand All @@ -51,8 +47,7 @@ pub fn clicolor_force() -> bool {
/// [NO_COLOR]: https://no-color.org/
#[inline]
pub fn no_color() -> bool {
let value = std::env::var_os("NO_COLOR");
value.as_deref().unwrap_or_else(|| std::ffi::OsStr::new("")) != ""
non_empty(std::env::var_os("NO_COLOR").as_deref())
}

/// Check `TERM` for color support
Expand Down Expand Up @@ -139,3 +134,27 @@ pub fn is_ci() -> bool {
// - Woodpecker sets it to `woodpecker`
std::env::var_os("CI").is_some()
}

fn non_empty(var: Option<&std::ffi::OsStr>) -> bool {
!var.unwrap_or_default().is_empty()
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn non_empty_not_present() {
assert!(!non_empty(None));
}

#[test]
fn non_empty_empty() {
assert!(!non_empty(Some(std::ffi::OsStr::new(""))));
}

#[test]
fn non_empty_texty() {
assert!(non_empty(Some(std::ffi::OsStr::new("hello"))));
}
}

0 comments on commit b922b24

Please # to comment.