use std::ffi::OsStr;
use crate::progress::ProgressMode;
use crate::ui::Painter;
pub const NO_HINTS_ENV: &str = "LYSOSOME_NO_HINTS";
pub const APP_URL: &str = "https://lysosome.dev";
pub fn enabled(
json: bool,
progress: ProgressMode,
stdout_tty: bool,
no_hints: Option<&OsStr>,
) -> bool {
let opted_out = no_hints.is_some_and(|v| !v.is_empty() && v != "0");
!json && progress != ProgressMode::Json && stdout_tty && !opted_out
}
pub fn enabled_now(json: bool, progress: ProgressMode) -> bool {
enabled(
json,
progress,
crate::ui::stdout_is_tty(),
std::env::var_os(NO_HINTS_ENV).as_deref(),
)
}
pub fn line(p: &Painter) -> String {
format!(
"\n {}\n",
p.dim(format!(
"Tip: the lysosome desktop app watches this for you: {APP_URL}"
))
)
}
#[cfg(test)]
mod tests {
use super::*;
use std::ffi::OsString;
#[test]
fn only_for_people_at_a_terminal() {
let auto = ProgressMode::Auto;
assert!(enabled(false, auto, true, None));
assert!(enabled(false, ProgressMode::Off, true, None));
assert!(!enabled(true, auto, true, None));
assert!(!enabled(false, ProgressMode::Json, true, None));
assert!(!enabled(false, auto, false, None));
}
#[test]
fn env_var_switches_it_off() {
let auto = ProgressMode::Auto;
let v = |s: &str| OsString::from(s);
assert!(!enabled(false, auto, true, Some(&v("1"))));
assert!(!enabled(false, auto, true, Some(&v("yes"))));
assert!(enabled(false, auto, true, Some(&v(""))));
assert!(enabled(false, auto, true, Some(&v("0"))));
}
#[test]
fn line_is_one_short_plain_line_without_color() {
let l = line(&Painter::new(false));
assert_eq!(l.trim().lines().count(), 1, "{l}");
assert!(l.contains(APP_URL), "{l}");
assert!(l.trim_matches('\n').chars().count() <= 80, "{l}");
assert!(!l.contains('\u{1b}'), "{l}");
assert!(!l.to_lowercase().contains("free"), "{l}");
}
}