mod cache;
mod disk_use;
mod error;
mod scanner;
pub mod tui;
pub use disk_use::DiskUse;
pub use error::DiskUseError;
pub use scanner::DirStat;
use std::{env, path::PathBuf};
pub fn format_size(bytes: u64, human_readable: bool) -> String {
if !human_readable {
return format!("{} bytes", bytes);
}
const UNITS: &[&str] = &["B", "KB", "MB", "GB", "TB"];
const THRESHOLD: f64 = 1024.0;
if bytes == 0 {
return "0 B".to_string();
}
let mut size = bytes as f64;
let mut unit_index = 0;
while size >= THRESHOLD && unit_index < UNITS.len() - 1 {
size /= THRESHOLD;
unit_index += 1;
}
if unit_index == 0 {
format!("{} {}", bytes, UNITS[unit_index])
} else {
format!("{:.2} {}", size, UNITS[unit_index])
}
}
pub fn get_default_cache_path() -> PathBuf {
if let Ok(cache_dir) = env::var("ACME_DISK_USE_CACHE") {
let mut cache_dir = PathBuf::from(cache_dir);
cache_dir.push("cache.bin");
return cache_dir;
}
if let Ok(home) = env::var("HOME") {
let mut cache_dir = PathBuf::from(home);
cache_dir.push(".cache");
cache_dir.push("acme-disk-use");
cache_dir.push("cache.bin");
cache_dir
} else {
let mut cache_dir = PathBuf::from("./.cache/acme-disk-use");
std::fs::create_dir_all(&cache_dir).ok(); cache_dir.push("cache.bin");
cache_dir
}
}
pub mod logger;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_format_size_human_readable() {
assert_eq!(format_size(0, true), "0 B");
assert_eq!(format_size(512, true), "512 B");
assert_eq!(format_size(1024, true), "1.00 KB");
assert_eq!(format_size(1536, true), "1.50 KB");
assert_eq!(format_size(1024 * 1024, true), "1.00 MB");
assert_eq!(format_size(1024 * 1024 * 1024, true), "1.00 GB");
assert_eq!(format_size(1024_u64.pow(4), true), "1.00 TB");
assert_eq!(format_size(0, false), "0 bytes");
assert_eq!(format_size(1024, false), "1024 bytes");
assert_eq!(format_size(1234567, false), "1234567 bytes");
}
#[test]
fn test_default_cache_path() {
let default_path = get_default_cache_path();
assert!(default_path.to_string_lossy().ends_with("cache.bin"));
assert!(default_path.is_absolute() || default_path.ends_with("cache.bin"));
}
}