use colored::Colorize;
use is_terminal::IsTerminal;
use std::io;
#[must_use]
pub fn is_tty() -> bool {
io::stdout().is_terminal()
}
#[must_use]
pub fn stderr_is_tty() -> bool {
io::stderr().is_terminal()
}
#[must_use]
pub fn success(msg: &str) -> String {
if is_tty() {
format!("{} {}", "✅".green(), msg.green())
} else {
format!("[OK] {msg}")
}
}
#[must_use]
pub fn error(msg: &str) -> String {
if is_tty() {
format!("{} {}", "❌".red(), msg.red().bold())
} else {
format!("[ERROR] {msg}")
}
}
#[must_use]
pub fn warning(msg: &str) -> String {
if is_tty() {
format!("{} {}", "⚠️".yellow(), msg.yellow())
} else {
format!("[WARNING] {msg}")
}
}
#[must_use]
pub fn info(msg: &str) -> String {
if is_tty() {
format!("{} {}", "ℹ️".blue(), msg.blue())
} else {
format!("[INFO] {msg}")
}
}
#[must_use]
pub fn header(title: &str, width: usize) -> String {
if is_tty() {
format!("{}\n{}", title.bold().cyan(), "=".repeat(width).cyan())
} else {
format!("{title}\n{}", "=".repeat(width))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_is_tty_returns_bool() {
let _result = is_tty();
}
#[test]
fn test_stderr_is_tty_returns_bool() {
let _result = stderr_is_tty();
}
#[test]
fn test_success_format() {
let msg = success("test message");
assert!(msg.contains("test message"));
assert!(msg.contains("✅") || msg.contains("[OK]"));
}
#[test]
fn test_error_format() {
let msg = error("test error");
assert!(msg.contains("test error"));
assert!(msg.contains("❌") || msg.contains("[ERROR]"));
}
#[test]
fn test_warning_format() {
let msg = warning("test warning");
assert!(msg.contains("test warning"));
assert!(msg.contains("⚠️") || msg.contains("[WARNING]"));
}
#[test]
fn test_info_format() {
let msg = info("test info");
assert!(msg.contains("test info"));
assert!(msg.contains("ℹ️") || msg.contains("[INFO]"));
}
#[test]
fn test_header_format() {
let msg = header("Test Header", 20);
assert!(msg.contains("Test Header"));
assert!(msg.contains("===================="));
}
}