use chrono::{DateTime, Utc};
use colored::*;
use terminal_size::{Width, terminal_size};
pub fn get_term_width() -> usize {
if let Some(width) = std::env::var("COLUMNS")
.ok()
.and_then(|c| c.parse::<usize>().ok())
{
return width.saturating_sub(2).max(40);
}
if let Some((Width(w), _)) = terminal_size() {
(w as usize).saturating_sub(2).max(40)
} else {
80
}
}
pub fn relative_time(timestamp: &str) -> String {
if let Ok(dt) = DateTime::parse_from_rfc3339(timestamp) {
let now = Utc::now();
let diff = now.signed_duration_since(dt);
if diff.num_seconds() < 60 {
"just now".to_string()
} else if diff.num_minutes() < 60 {
format!("{}m ago", diff.num_minutes())
} else if diff.num_hours() < 24 {
format!("{}h ago", diff.num_hours())
} else if diff.num_days() < 7 {
format!("{}d ago", diff.num_days())
} else {
dt.format("%Y-%m-%d").to_string()
}
} else {
timestamp.to_string()
}
}
pub fn success(msg: &str) {
println!("{} {}", "✅".green(), msg.bright_green());
}
pub fn error(msg: &str) {
eprintln!("{} {}", "❌".red().bold(), msg.bright_red());
}
pub fn info(msg: &str) {
println!("{} {}", "ℹ️ ".cyan(), msg.bright_cyan());
}
pub fn warn(msg: &str) {
println!("{} {}", "⚠️ ".yellow(), msg.bright_yellow());
}