diff --git a/yazi-shared/src/bytes.rs b/yazi-shared/src/bytes.rs index aedcb47f..7c899c1c 100644 --- a/yazi-shared/src/bytes.rs +++ b/yazi-shared/src/bytes.rs @@ -9,3 +9,19 @@ impl BytesExt for [u8] { Some((left, &right[sep.len()..])) } } + +pub fn human_bytes(n: u64) -> String { + const KB: f64 = 1024.0; + const MB: f64 = KB * 1024.0; + const GB: f64 = MB * 1024.0; + let x = n as f64; + if x >= GB { + format!("{:.2} GB", x / GB) + } else if x >= MB { + format!("{:.2} MB", x / MB) + } else if x >= KB { + format!("{:.2} KB", x / KB) + } else { + format!("{n} B") + } +} diff --git a/yazi-shared/src/url/buf.rs b/yazi-shared/src/url/buf.rs index 13e4a4c0..e72ee1d4 100644 --- a/yazi-shared/src/url/buf.rs +++ b/yazi-shared/src/url/buf.rs @@ -339,7 +339,7 @@ mod tests { for (path, expected) in cases { let path: UrlBuf = path.parse()?; - assert_eq!(path.parent().map(|u| format!("{:?}", u)).as_deref(), expected); + assert_eq!(path.parent().map(|u| format!("{u:?}")).as_deref(), expected); } Ok(())