Skip to content

Commit

Permalink
Paint errors with the blood of our enemies
Browse files Browse the repository at this point in the history
  • Loading branch information
cauebs committed Jul 19, 2018
1 parent c261c4d commit 56b44b3
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
1 change: 1 addition & 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ categories = ["command-line-interface", "command-line-utilities", "filesystem"]
structopt = "0.2.10"
walkdir = "2.1.4"
number_prefix = "0.2.8"
ansi_term = "0.11.0"
23 changes: 18 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
use std::fmt::Display;
use std::path::{Path, PathBuf};

extern crate ansi_term;
use ansi_term::Colour;

extern crate walkdir;
use walkdir::WalkDir;

Expand All @@ -10,14 +14,20 @@ use structopt::StructOpt;

extern crate number_prefix;

fn print_error(error: impl Display) {
let message = format!("{}", error);
let message = Colour::Red.paint(message);
eprintln!("{}", message);
}

fn recursive_size(path: &Path) -> u64 {
WalkDir::new(path)
.into_iter()
.filter_map(|entry| {
let path = match &entry {
Ok(entry) => entry.path(),
Err(error) => {
eprintln!("{}", error);
print_error(error);
return None;
}
};
Expand All @@ -26,7 +36,7 @@ fn recursive_size(path: &Path) -> u64 {
let size = match path.symlink_metadata() {
Ok(metadata) => metadata.len(),
Err(error) => {
eprintln!("{}", error);
print_error(error);
return None;
}
};
Expand Down Expand Up @@ -89,8 +99,8 @@ fn format_size(size: u64, binary: bool) -> String {
};

let formatted = match prefixed {
Standalone(s) => format!("{} B", s as u64),
Prefixed(p, s) => format!("{:.2} {}B", s, p),
Standalone(number) => format!("{} B", number as u64),
Prefixed(prefix, number) => format!("{:.2} {}B", number, prefix),
};

if binary {
Expand All @@ -101,6 +111,9 @@ fn format_size(size: u64, binary: bool) -> String {
}

fn main() {
#[cfg(windows)]
ansi_term::enable_ansi_support();

let opt = Opt::from_args();

let mut entries = Vec::new();
Expand Down Expand Up @@ -130,7 +143,7 @@ fn main() {
let percentage = 100.0 * entry.size as f64 / total_size as f64;
if let Some(p) = opt.minimum_percentage {
if percentage < p {
continue;
continue;
}
}

Expand Down

0 comments on commit 56b44b3

Please sign in to comment.