use std::io::IsTerminal; use crate::core; pub fn enabled() -> bool { std::env::var_os("NO_COLOR").is_none() && std::io::stdout().is_terminal() } fn style(text: &str, code: &str) -> String { if enabled() { format!("\x1b[{code}m{text}\x1b[0m") } else { text.to_string() } } pub fn error_prefix() { print!("{}: ", style("error", "1;31")); } pub fn warning_prefix() { print!("{}: ", style("warning", "1;33")); } pub fn info_prefix() { print!("{}: ", style("info", "36")); } pub fn red(text: &str) -> String { style(text, "31") } pub fn green(text: &str) -> String { style(text, "32") } pub fn cyan(text: &str) -> String { style(text, "36") } pub fn dim(text: &str) -> String { style(text, "2") } pub fn bold_cyan(text: &str) -> String { style(text, "1;36") } pub fn print_cell(text: &str, width: usize) { print!("{text}"); for _ in text.chars().count()..width { print!(" "); } } pub fn open_repo() -> Option { match crate::store::layout::discover(std::path::Path::new(".")) { Ok(root) => Some(root), Err(_) => { error_prefix(); println!("not a veins repository"); None } } } pub fn status_path(path: &[u8], cwd_rel: &str) -> String { let path = String::from_utf8_lossy(path); if cwd_rel.is_empty() { return path.into_owned(); } let current: Vec<&str> = cwd_rel .split('/') .filter(|value| !value.is_empty()) .collect(); let target: Vec<&str> = path.split('/').collect(); let mut common = 0; while common < current.len() && common < target.len() && current[common] == target[common] { common += 1; } let mut output = String::new(); for _ in common..current.len() { output.push_str("../"); } output.push_str(&target[common..].join("/")); output } pub fn join_args(parts: &[String]) -> String { parts.join(" ") } pub fn message_is_blank(parts: &[String]) -> bool { parts .iter() .flat_map(|part| part.bytes()) .all(|byte| matches!(byte, b' ' | b'\t' | b'\n' | b'\r')) } pub fn pager_output(root: &std::path::Path, text: &str, pager: bool) { if pager && core::stdout_is_terminal() { core::run_pager(root, text); } else { print!("{text}"); } }