pub fn format_size(bytes: u64) -> String {
const UNITS: &[&str] = &["B", "KB", "MB", "GB"];
let mut size = bytes as f64;
let mut unit_index = 0;
while size >= 1024.0 && unit_index < UNITS.len() - 1 {
size /= 1024.0;
unit_index += 1;
}
if unit_index == 0 {
format!("{} {}", size as u64, UNITS[unit_index])
} else {
format!("{:.1} {}", size, UNITS[unit_index])
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[allow(dead_code)]
pub enum SizeBadge {
Xs, S, M, L, Xl, Xxl, }
impl std::fmt::Display for SizeBadge {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let s = match self {
SizeBadge::Xs => "XS",
SizeBadge::S => "S",
SizeBadge::M => "M",
SizeBadge::L => "L",
SizeBadge::Xl => "XL",
SizeBadge::Xxl => "XXL",
};
write!(f, "{}", s)
}
}
#[allow(dead_code)]
pub struct SizeMark {
pub badge: SizeBadge,
pub human: String, pub tick: Option<char>, }
#[allow(dead_code)]
pub fn size_mark(bytes: u64) -> SizeMark {
let human = format_size(bytes);
if bytes <= 1_024 {
return SizeMark {
badge: SizeBadge::Xs, human,
tick: None,
};
}
let min_log = (1_024_f64).ln();
let max_log = (100_000_000_f64).ln();
let current_log = (bytes as f64).ln();
let position = ((current_log - min_log) / (max_log - min_log)).clamp(0.0, 1.0);
let ticks = ['▁', '▂', '▃', '▄', '▅', '▆', '▇'];
let tick_index = (position * 6.0).round() as usize;
let badge = match bytes {
0..=1_024 => SizeBadge::Xs,
1_025..=8_192 => SizeBadge::S,
8_193..=65_536 => SizeBadge::M,
65_537..=524_288 => SizeBadge::L,
524_289..=4_194_304 => SizeBadge::Xl,
_ => SizeBadge::Xxl,
};
SizeMark {
badge,
human,
tick: Some(ticks[tick_index.min(6)]),
}
}
pub fn loc_category(loc: usize) -> &'static str {
match loc {
0..=10 => "XS",
11..=120 => "S",
121..=300 => "M",
301..=600 => "L",
601..=1000 => "XL",
_ => "XXL",
}
}
pub fn loc_to_bar(loc: usize, max_loc_in_dir: usize, bar_width: usize) -> String {
if loc < 10 {
return format!("[{}]", "·".repeat(bar_width));
}
let capped_loc = loc.min(1000);
let capped_max = max_loc_in_dir.clamp(1, 1000);
let norm = capped_loc as f64 / capped_max as f64;
let filled_cells = (norm * bar_width as f64).ceil() as usize;
let num_filled = filled_cells.max(1).min(bar_width);
let num_empty = bar_width - num_filled;
format!("[{}{}]", "█".repeat(num_filled), "·".repeat(num_empty))
}
pub fn format_loc_display(loc: usize) -> String {
if loc >= 1000 {
"1k+".to_string()
} else {
loc.to_string()
}
}
pub fn is_global_outlier(loc: usize, threshold: usize) -> bool {
loc >= threshold
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_format_size() {
assert_eq!(format_size(0), "0 B");
assert_eq!(format_size(100), "100 B");
assert_eq!(format_size(1024), "1.0 KB");
assert_eq!(format_size(1536), "1.5 KB");
assert_eq!(format_size(1048576), "1.0 MB");
assert_eq!(format_size(1073741824), "1.0 GB");
}
#[test]
fn test_loc_to_bar() {
assert_eq!(loc_to_bar(5, 100, 10), "[··········]");
assert_eq!(loc_to_bar(0, 100, 10), "[··········]");
assert_eq!(loc_to_bar(100, 100, 10), "[██████████]"); assert_eq!(loc_to_bar(50, 100, 10), "[█████·····]"); assert_eq!(loc_to_bar(25, 100, 10), "[███·······]");
assert_eq!(loc_to_bar(2000, 2000, 10), "[██████████]"); assert_eq!(loc_to_bar(500, 2000, 10), "[█████·····]"); }
#[test]
fn test_loc_category() {
assert_eq!(loc_category(5), "XS");
assert_eq!(loc_category(50), "S");
assert_eq!(loc_category(150), "M");
assert_eq!(loc_category(400), "L");
assert_eq!(loc_category(800), "XL");
assert_eq!(loc_category(1500), "XXL");
}
#[test]
fn test_format_loc_display() {
assert_eq!(format_loc_display(100), "100");
assert_eq!(format_loc_display(999), "999");
assert_eq!(format_loc_display(1000), "1k+");
assert_eq!(format_loc_display(5000), "1k+");
}
}