use std::io::IsTerminal;
#[allow(clippy::disallowed_methods)] pub fn should_show_progress() -> bool {
match std::env::var("CARGO_TERM_PROGRESS_WHEN")
.as_deref()
.unwrap_or("auto")
{
"never" => false,
"always" => true,
"auto" => {
std::io::stdout().is_terminal()
}
_ => {
std::io::stdout().is_terminal()
}
}
}
#[cfg(test)]
mod tests {
use std::env;
use super::*;
fn with_env_var<F, R>(key: &str, value: Option<&str>, test_fn: F) -> R
where
F: FnOnce() -> R,
{
let original = env::var(key).ok();
match value {
Some(val) => unsafe { env::set_var(key, val) },
None => unsafe { env::remove_var(key) },
}
let result = test_fn();
match original {
Some(val) => unsafe { env::set_var(key, &val) },
None => unsafe { env::remove_var(key) },
}
result
}
#[test]
fn test_should_show_progress_default() {
with_env_var("CARGO_TERM_PROGRESS_WHEN", None, || {
let _ = should_show_progress();
});
}
#[test]
fn test_should_show_progress_never() {
with_env_var("CARGO_TERM_PROGRESS_WHEN", Some("never"), || {
assert!(
!should_show_progress(),
"should return false when set to 'never'"
);
});
}
#[test]
fn test_should_show_progress_always() {
with_env_var("CARGO_TERM_PROGRESS_WHEN", Some("always"), || {
assert!(
should_show_progress(),
"should return true when set to 'always'"
);
});
}
#[test]
fn test_should_show_progress_auto() {
with_env_var("CARGO_TERM_PROGRESS_WHEN", Some("auto"), || {
let _ = should_show_progress();
});
}
#[test]
fn test_should_show_progress_unknown_value() {
with_env_var("CARGO_TERM_PROGRESS_WHEN", Some("unknown_value"), || {
let _ = should_show_progress();
});
}
#[test]
fn test_should_show_progress_empty_string() {
with_env_var("CARGO_TERM_PROGRESS_WHEN", Some(""), || {
let _ = should_show_progress();
});
}
}