1mod cache;
7mod disk_use;
8mod error;
9mod scanner;
10pub mod tui;
11
12pub use disk_use::DiskUse;
14pub use error::DiskUseError;
15pub use scanner::DirStat;
16
17use std::{env, path::PathBuf};
18
19pub fn format_size(bytes: u64, human_readable: bool) -> String {
34 if !human_readable {
35 return format!("{} bytes", bytes);
36 }
37
38 const UNITS: &[&str] = &["B", "KB", "MB", "GB", "TB"];
39 const THRESHOLD: f64 = 1024.0;
40
41 if bytes == 0 {
42 return "0 B".to_string();
43 }
44
45 let mut size = bytes as f64;
46 let mut unit_index = 0;
47
48 while size >= THRESHOLD && unit_index < UNITS.len() - 1 {
49 size /= THRESHOLD;
50 unit_index += 1;
51 }
52
53 if unit_index == 0 {
54 format!("{} {}", bytes, UNITS[unit_index])
55 } else {
56 format!("{:.2} {}", size, UNITS[unit_index])
57 }
58}
59
60pub fn get_default_cache_path() -> PathBuf {
65 if let Ok(cache_dir) = env::var("ACME_DISK_USE_CACHE") {
66 let mut cache_dir = PathBuf::from(cache_dir);
67 cache_dir.push("cache.bin");
68 return cache_dir;
69 }
70
71 if let Ok(home) = env::var("HOME") {
73 let mut cache_dir = PathBuf::from(home);
74 cache_dir.push(".cache");
75 cache_dir.push("acme-disk-use");
76 cache_dir.push("cache.bin");
77 cache_dir
78 } else {
79 let mut cache_dir = PathBuf::from("./.cache/acme-disk-use");
81 std::fs::create_dir_all(&cache_dir).ok(); cache_dir.push("cache.bin");
83 cache_dir
84 }
85}
86
87pub mod logger;
89
90#[cfg(test)]
91mod tests {
92 use super::*;
93
94 #[test]
95 fn test_format_size_human_readable() {
96 assert_eq!(format_size(0, true), "0 B");
100 assert_eq!(format_size(512, true), "512 B");
101 assert_eq!(format_size(1024, true), "1.00 KB");
102 assert_eq!(format_size(1536, true), "1.50 KB");
103 assert_eq!(format_size(1024 * 1024, true), "1.00 MB");
104 assert_eq!(format_size(1024 * 1024 * 1024, true), "1.00 GB");
105 assert_eq!(format_size(1024_u64.pow(4), true), "1.00 TB");
106
107 assert_eq!(format_size(0, false), "0 bytes");
111 assert_eq!(format_size(1024, false), "1024 bytes");
112 assert_eq!(format_size(1234567, false), "1234567 bytes");
113 }
114
115 #[test]
116 fn test_default_cache_path() {
117 let default_path = get_default_cache_path();
121
122 assert!(default_path.to_string_lossy().ends_with("cache.bin"));
124
125 assert!(default_path.is_absolute() || default_path.ends_with("cache.bin"));
128 }
129}