-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rs
137 lines (123 loc) · 3.72 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
use clap::error::ErrorKind;
use clap::{CommandFactory, Parser};
use filestats::dirutils;
use filestats::dirutils::FileSize;
use filestats::stats::Histogram;
use filestats::utils::format_bytes;
use std::process::exit;
use std::time::Instant;
#[derive(Parser, Debug)]
#[command(
name = "filestats",
about = "Utility that outputs colorful statistics about the size of your files",
author = "Max Marcon (https://github.com/maxmarcon/)",
version = "0.1.0"
)]
struct Filestats {
paths: Vec<String>,
#[arg(
long,
short = 'd',
help = "max depth to consider. 0 = do not recurse into subdirectories. Default: infinity"
)]
max_depth: Option<u32>,
#[arg(long, short, help = "shows verbose information about errors")]
verbose: bool,
#[arg(long, short, help = format!("how many paths will be visited in parallel, defaults to {}", dirutils::DEFAULT_PARALLEL_PATHS), value_parser = clap::value_parser!(u32).range(1..))]
parallelism: Option<u32>,
}
fn main() {
let args = Filestats::parse();
if args.paths.is_empty() {
Filestats::command()
.error(
ErrorKind::MissingRequiredArgument,
"You should specify at least one path!",
)
.exit();
}
if let Err(error_msg) = run(args) {
eprintln!("{error_msg}");
exit(1);
}
}
const SIZES: [u64; 3] = [1, 10, 100];
const EXP: [u32; 4] = [10, 20, 30, 40];
fn run(args: Filestats) -> Result<(), &'static str> {
let ceilings = EXP
.iter()
.flat_map(|&e| SIZES.map(|s| s * 2_u64.pow(e)))
.collect::<Vec<_>>();
let start = Instant::now();
let (mut min, mut max): (Option<FileSize>, Option<FileSize>) = (None, None);
let (hist, errors) = args
.paths
.into_iter()
.flat_map(|path| {
dirutils::visit(
std::path::Path::new(&path),
args.max_depth,
args.parallelism,
)
})
.enumerate()
.map(|(cnt, r)| {
if cnt % 10 == 0 {
print!("\rScanning {} files", cnt);
}
match r {
Err(ref error) => {
if args.verbose {
eprintln!("\n{}", error);
}
}
Ok(ref size_entry) => {
if max.is_none() || size_entry.size > max.as_ref().unwrap().size {
max = Some(size_entry.to_owned());
}
if min.is_none() || size_entry.size < min.as_ref().unwrap().size {
min = Some(size_entry.to_owned())
}
}
}
r
})
.fold(
(Histogram::new(&ceilings), 0),
|(mut hist, errors), r| match r {
Ok(size_entry) => {
hist.add(size_entry.size);
(hist, errors)
}
Err(_) => (hist, errors + 1),
},
);
print!("\r");
println!("{}", hist);
println!(
"Scanned {} files in {} seconds",
hist.count(),
start.elapsed().as_secs()
);
if let Some(avg_size) = hist.avg() {
println!("Average size: {} bytes", format_bytes(avg_size as u64))
}
if let Some(max) = max {
println!(
"Largest file at {} bytes: {:?}",
format_bytes(max.size),
max.path
);
}
if let Some(min) = min {
println!(
"Smallest file at {} bytes: {:?}",
format_bytes(min.size),
min.path
);
}
if errors > 0 {
println!("{} files could not be read", errors);
}
Ok(())
}