use zfish::style::{Color, Style};
pub fn with_env_var<F, T>(name: &str, value: Option<&str>, f: F) -> T
where
F: FnOnce() -> T,
{
let original = std::env::var(name).ok();
unsafe {
if let Some(v) = value {
std::env::set_var(name, v);
} else {
std::env::remove_var(name);
}
}
let result = f();
unsafe {
match original {
Some(val) => std::env::set_var(name, val),
None => std::env::remove_var(name),
}
}
result
}
pub fn capture_stdout<F>(f: F) -> std::io::Result<String>
where
F: FnOnce() -> std::io::Result<()>,
{
f()?;
Ok("Output captured (placeholder)".to_string())
}
#[test]
fn test_basic_coloring() {
with_env_var("NO_COLOR", Some("1"), || {
with_env_var("COLORTERM", None, || {
let green_text = Color::Green.paint("Success");
let output = format!("{}", green_text);
assert_eq!(output, "Success", "NO_COLOR should produce plain text");
});
});
with_env_var("NO_COLOR", None, || {
with_env_var("COLORTERM", Some("truecolor"), || {
std::thread::sleep(std::time::Duration::from_millis(1));
let green_text = Color::Green.paint("Success");
let output = format!("{}", green_text);
assert!(
output == "\x1b[32mSuccess\x1b[0m" || output == "Success",
"Expected ANSI codes or plain text, got: {:?}",
output
);
});
});
}
#[test]
fn test_styling_combinations() {
with_env_var("NO_COLOR", None, || {
with_env_var("COLORTERM", Some("truecolor"), || {
std::thread::sleep(std::time::Duration::from_millis(1));
let styled_text = Color::Red.paint("Error").style(Style::Bold);
let output = format!("{}", styled_text);
assert!(
output == "\x1b[31;1mError\x1b[0m" || output == "Error",
"Expected ANSI codes or plain text, got: {:?}",
output
);
});
});
}
#[test]
#[ignore]
fn test_all_colors_display() {
with_env_var("COLORTERM", Some("1"), || {
with_env_var("NO_COLOR", None, || {
println!("Testing all colors on the terminal:");
println!("If colors are not displayed correctly, check your terminal settings.");
println!();
let colors = [
Color::Black,
Color::Red,
Color::Green,
Color::Yellow,
Color::Blue,
Color::Magenta,
Color::Cyan,
Color::White,
Color::BrightBlack,
Color::BrightRed,
Color::BrightGreen,
Color::BrightYellow,
Color::BrightBlue,
Color::BrightMagenta,
Color::BrightCyan,
Color::BrightWhite,
];
for color in &colors {
println!("{}", color.paint(format!("This is {:?} color", color)));
}
println!();
println!(
"Colors should appear as expected. If not, your terminal may not support ANSI colors."
);
});
});
}
#[test]
fn test_custom_256_coloring() {
with_env_var("NO_COLOR", Some("1"), || {
with_env_var("COLORTERM", None, || {
let custom_text = Color::Custom(100).paint("No Color");
let output = format!("{}", custom_text);
assert!(!output.is_empty(), "Output should not be empty");
});
});
with_env_var("NO_COLOR", None, || {
with_env_var("COLORTERM", Some("truecolor"), || {
std::thread::sleep(std::time::Duration::from_millis(1));
let custom_text = Color::Custom(196).paint("Custom Red");
let output = format!("{}", custom_text);
assert!(
output.contains("\x1b[38;5;196m") || output == "Custom Red",
"Expected either ANSI codes or plain text, got: {:?}",
output
);
let styled_custom = Color::Custom(46).paint("Custom Green").style(Style::Bold);
let output_styled = format!("{}", styled_custom);
assert!(
output_styled.contains("\x1b[38;5;46") || output_styled == "Custom Green",
"Expected either ANSI codes or plain text, got: {:?}",
output_styled
);
});
});
}
#[test]
#[ignore]
fn test_256_colors_display() {
with_env_var("COLORTERM", Some("1"), || {
with_env_var("NO_COLOR", None, || {
println!("Testing all 256 colors on the terminal:");
println!("If colors are not displayed correctly, check your terminal settings.");
println!();
for i in 0..=255 {
println!("{}", Color::Custom(i).paint(format!("Color {}", i)));
}
println!();
println!(
"All 256 colors should appear. If not, your terminal may not support ANSI 256 colors."
);
});
});
}