Skip to content

Commit

Permalink
Improved Cache Command Output
Browse files Browse the repository at this point in the history
  • Loading branch information
Redfire75369 committed Nov 25, 2023
1 parent 38c4177 commit 5321174
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 27 deletions.
16 changes: 16 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ license.workspace = true
authors = ["Redfire <[email protected]>"]

[dependencies]
humansize = "2.1.3"
rustyline = "12.0.0"
rustyline-derive = "0.9.0"

Expand Down
21 changes: 2 additions & 19 deletions cli/src/commands/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -34,21 +35,3 @@ fn cache_size(folder: &Path) -> io::Result<u64> {
}
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)
}
}
5 changes: 1 addition & 4 deletions modules/src/path/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@ const DELIMITER: &str = ":\0";
#[js_fn]
fn join(#[ion(varargs)] segments: Vec<String>) -> String {
let mut path = PathBuf::new();
for segment in segments {
path.push(segment);
}

path.extend(segments);
String::from(path.to_str().unwrap())
}

Expand Down
8 changes: 4 additions & 4 deletions runtime/src/cache/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ pub struct Cache {

impl Cache {
pub fn new() -> Option<Cache> {
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 }
})
}

Expand Down

0 comments on commit 5321174

Please sign in to comment.