From 53211746d6dfe4f3d9503cdab6ba46c84d1427e8 Mon Sep 17 00:00:00 2001 From: Redfire Date: Sat, 25 Nov 2023 10:26:45 +0800 Subject: [PATCH] Improved Cache Command Output --- Cargo.lock | 16 ++++++++++++++++ cli/Cargo.toml | 1 + cli/src/commands/cache.rs | 21 ++------------------- modules/src/path/path.rs | 5 +---- runtime/src/cache/cache.rs | 8 ++++---- 5 files changed, 24 insertions(+), 27 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3f2945c2..6cffc60b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -348,6 +348,7 @@ dependencies = [ "clap", "colored", "dunce", + "humansize", "ion", "modules", "mozjs", @@ -826,6 +827,15 @@ version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9" +[[package]] +name = "humansize" +version = "2.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6cb51c9a029ddc91b07a787f1d86b53ccfa49b0e86688c946ebe8d3555685dd7" +dependencies = [ + "libm", +] + [[package]] name = "hyper" version = "0.14.27" @@ -1058,6 +1068,12 @@ dependencies = [ "winapi", ] +[[package]] +name = "libm" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ec2a862134d2a7d32d7983ddcdd1c4923530833c9f2ea1a44fc5fa473989058" + [[package]] name = "libredox" version = "0.0.1" diff --git a/cli/Cargo.toml b/cli/Cargo.toml index 42f5c574..6c0fedd9 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -7,6 +7,7 @@ license.workspace = true authors = ["Redfire "] [dependencies] +humansize = "2.1.3" rustyline = "12.0.0" rustyline-derive = "0.9.0" diff --git a/cli/src/commands/cache.rs b/cli/src/commands/cache.rs index eaa4d54a..d3811477 100644 --- a/cli/src/commands/cache.rs +++ b/cli/src/commands/cache.rs @@ -8,13 +8,14 @@ use std::fs::{metadata, read_dir}; use std::io; use std::path::Path; +use humansize::{BINARY, SizeFormatter}; use runtime::cache::Cache; pub(crate) fn cache_statistics() { if let Some(cache) = Cache::new() { println!("Location: {}", cache.dir().display()); match cache_size(cache.dir()) { - Ok(size) => println!("Size: {}", format_size(size)), + Ok(size) => println!("Size: {}", SizeFormatter::new(size, BINARY)), Err(err) => eprintln!("Error while Calculating Size: {}", err), } } else { @@ -34,21 +35,3 @@ fn cache_size(folder: &Path) -> io::Result { } Ok(size) } - -const PREFIXES: [&str; 6] = ["", "Ki", "Mi", "Gi", "Ti", "Pi"]; - -fn format_size(size: u64) -> String { - if size >= 1024 { - let index: u32 = f64::log(size as f64, 1024.0).floor() as u32; - let s1 = size / 1024_u64.pow(index); - let s2 = (size - s1 * 1024_u64.pow(index)) / 1024_u64.pow(index - 1); - - if s2 != 0 { - format!("{} {}B, {} {}B", s1, PREFIXES[index as usize], s2, PREFIXES[index as usize - 1]) - } else { - format!("{} {}B", s1, PREFIXES[index as usize]) - } - } else { - format!("{} B", size) - } -} diff --git a/modules/src/path/path.rs b/modules/src/path/path.rs index 52a4ac8c..99950cf8 100644 --- a/modules/src/path/path.rs +++ b/modules/src/path/path.rs @@ -26,10 +26,7 @@ const DELIMITER: &str = ":\0"; #[js_fn] fn join(#[ion(varargs)] segments: Vec) -> String { let mut path = PathBuf::new(); - for segment in segments { - path.push(segment); - } - + path.extend(segments); String::from(path.to_str().unwrap()) } diff --git a/runtime/src/cache/cache.rs b/runtime/src/cache/cache.rs index 3463f9e8..df8e2533 100644 --- a/runtime/src/cache/cache.rs +++ b/runtime/src/cache/cache.rs @@ -28,10 +28,10 @@ pub struct Cache { impl Cache { pub fn new() -> Option { - home_dir().map(|path| { - let dir = path.join(".spiderfire/cache"); - let _ = create_dir_all(&dir); - Cache { dir } + home_dir().map(|mut path| { + path.extend([".spiderfire", "cache"]); + let _ = create_dir_all(&path); + Cache { dir: path } }) }