use std::time::Instant;
use std::path::PathBuf;
use dirs;
pub fn yes_no(flag: bool) -> &'static str {
if flag {
"✅"
} else {
"❌"
}
}
pub fn human_readable_size(bytes: u64) -> String {
if bytes < 1024 {
format!("{} B", bytes)
} else if bytes < 1024 * 1024 {
format!("{:.2} KB", bytes as f64 / 1024.0)
} else if bytes < 1024 * 1024 * 1024 {
format!("{:.2} MB", bytes as f64 / 1024.0 / 1024.0)
} else {
format!("{:.2} GB", bytes as f64 / 1024.0 / 1024.0 / 1024.0)
}
}
pub fn truncate(s: &str, len: usize) -> String {
if s.len() > len {
format!("{}...", &s[..len])
} else {
s.to_string()
}
}
pub fn hyperlink(text: &str, path: &str) -> String {
format!("\x1b]8;;file://{}\x07{}\x1b]8;;\x07", path, text)
}
pub fn calculate_search_duration(start_time: Instant) -> u64 {
let end_time = Instant::now();
end_time.duration_since(start_time).as_millis() as u64
}
pub fn get_downloads_path() -> Result<PathBuf, Box<dyn std::error::Error>> {
let download_dir = dirs::download_dir().ok_or("Failed to get downloads directory")?;
let export_dir = download_dir.join("pdfx_exports");
std::fs::create_dir_all(&export_dir)?;
Ok(export_dir)
}