Skip to content

Commit 0b1b74b

Browse files
committed
Add Size with a Display impl
1 parent f3f54e2 commit 0b1b74b

2 files changed

Lines changed: 38 additions & 4 deletions

File tree

src/crufty.rs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,49 @@
11
#![allow(dead_code)]
2+
use std::fmt;
23
use std::path::PathBuf;
34

45
use globset::{Glob, GlobSetBuilder};
56
use ignore::WalkBuilder;
67

78
pub mod cli;
89

10+
#[derive(Debug, PartialEq, Eq, Clone)]
11+
pub enum Size {
12+
UnknownSize,
13+
KnownSize(u64),
14+
}
15+
16+
impl fmt::Display for Size {
17+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
18+
match self {
19+
Size::UnknownSize => write!(f, "unknown"),
20+
Size::KnownSize(bytes) => {
21+
if *bytes < 1024 {
22+
write!(f, "{} B", bytes)
23+
} else if *bytes < 1024 * 1024 {
24+
write!(f, "{:.1} KB", *bytes as f64 / 1024.0)
25+
} else if *bytes < 1024 * 1024 * 1024 {
26+
write!(f, "{:.1} MB", *bytes as f64 / (1024.0 * 1024.0))
27+
} else {
28+
write!(f, "{:.1} GB", *bytes as f64 / (1024.0 * 1024.0 * 1024.0))
29+
}
30+
}
31+
}
32+
}
33+
}
34+
935
#[derive(Debug, PartialEq, Eq)]
1036
pub struct ArtifactCandidate {
1137
pub path: PathBuf,
12-
size: Option<u64>,
38+
pub size: Size,
1339
}
1440

1541
impl ArtifactCandidate {
1642
fn new(path: PathBuf) -> Self {
17-
ArtifactCandidate { path, size: None }
43+
ArtifactCandidate {
44+
path,
45+
size: Size::UnknownSize,
46+
}
1847
}
1948
}
2049

src/main.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use clap::Parser;
22
use console::{style, Term};
33
use crufty::cli::{Cli, Commands};
4-
use crufty::fetch_artifacts;
4+
use crufty::{fetch_artifacts, Size};
55
use std::env;
66
use std::io;
77
use std::process;
@@ -33,11 +33,16 @@ fn scan() -> io::Result<()> {
3333
for (i, artifact) in artifacts.iter().enumerate() {
3434
let rel_path =
3535
artifact.path.strip_prefix(&path).unwrap_or(&artifact.path);
36+
let size_display = match &artifact.size {
37+
Size::UnknownSize => style(format!("unknown")).yellow(),
38+
Size::KnownSize(_) => style(format!("{}", artifact.size)).green(),
39+
};
40+
3641
term.write_line(&format!(
3742
"[{}] {:<30} {}",
3843
i + 1,
3944
style(format!("./{}", rel_path.display())).bold(),
40-
style(format!("unknown size")).yellow()
45+
size_display
4146
))?;
4247
}
4348
Ok(())

0 commit comments

Comments
 (0)