From b68c4507109660e70389ecc0cc014348fa412045 Mon Sep 17 00:00:00 2001 From: bootandy Date: Tue, 1 May 2018 13:55:19 +0100 Subject: [PATCH 1/8] use eprintln! --- src/main.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/main.rs b/src/main.rs index 7383a64..ba3db03 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,7 +5,6 @@ extern crate walkdir; use self::display::draw_it; use clap::{App, AppSettings, Arg}; -use std::io::{self, Write}; use utils::{find_big_ones, get_dir_tree, sort}; mod display; @@ -67,9 +66,7 @@ fn main() { if options.is_present("depth") && options.value_of("number_of_lines").unwrap() != DEFAULT_NUMBER_OF_LINES { - io::stderr() - .write(b"Use either -n for number of directories to show. Or -d for depth. Not both") - .expect("Error writing to stderr. Oh the irony!"); + eprintln!("Use either -n for number of directories to show. Or -d for depth. Not both"); return; } From c5830c5d00083a40c592fa52d2c7ad140d9c0fe2 Mon Sep 17 00:00:00 2001 From: bootandy Date: Tue, 1 May 2018 13:59:48 +0100 Subject: [PATCH 2/8] Stop using target_os just use target_family target_family unix covers linux and mac and wraps up the call for inodes in a common interface. --- src/utils/platform.rs | 36 ++---------------------------------- 1 file changed, 2 insertions(+), 34 deletions(-) diff --git a/src/utils/platform.rs b/src/utils/platform.rs index 9d0451a..c62c5c6 100644 --- a/src/utils/platform.rs +++ b/src/utils/platform.rs @@ -6,23 +6,7 @@ fn get_block_size() -> u64 { 512 } -#[cfg(target_os = "linux")] -pub fn get_metadata(d: &DirEntry, use_apparent_size: bool) -> Option<(u64, Option<(u64, u64)>)> { - use std::os::linux::fs::MetadataExt; - match d.metadata().ok() { - Some(md) => { - let inode = Some((md.st_ino(), md.st_dev())); - if use_apparent_size { - Some((md.len(), inode)) - } else { - Some((md.st_blocks() * get_block_size(), inode)) - } - } - None => None, - } -} - -#[cfg(target_os = "unix")] +#[cfg(target_family = "unix")] pub fn get_metadata(d: &DirEntry, use_apparent_size: bool) -> Option<(u64, Option<(u64, u64)>)> { use std::os::unix::fs::MetadataExt; match d.metadata().ok() { @@ -38,23 +22,7 @@ pub fn get_metadata(d: &DirEntry, use_apparent_size: bool) -> Option<(u64, Optio } } -#[cfg(target_os = "macos")] -pub fn get_metadata(d: &DirEntry, use_apparent_size: bool) -> Option<(u64, Option<(u64, u64)>)> { - use std::os::macos::fs::MetadataExt; - match d.metadata().ok() { - Some(md) => { - let inode = Some((md.st_ino(), md.st_dev())); - if use_apparent_size { - Some((md.len(), inode)) - } else { - Some((md.st_blocks() * get_block_size(), inode)) - } - } - None => None, - } -} - -#[cfg(not(any(target_os = "linux", target_os = "unix", target_os = "macos")))] +#[cfg(not(target_family = "unix"))] pub fn get_metadata(d: &DirEntry, _apparent: bool) -> Option<(u64, Option<(u64, u64)>)> { match d.metadata().ok() { Some(md) => Some((md.len(), None)), From 39db8b86fdd7532359ce4f09bc07f4c32e24688b Mon Sep 17 00:00:00 2001 From: bootandy Date: Tue, 1 May 2018 14:38:34 +0100 Subject: [PATCH 3/8] Replace simple match with map_or --- src/utils/platform.rs | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/src/utils/platform.rs b/src/utils/platform.rs index c62c5c6..3d710b0 100644 --- a/src/utils/platform.rs +++ b/src/utils/platform.rs @@ -9,23 +9,19 @@ fn get_block_size() -> u64 { #[cfg(target_family = "unix")] pub fn get_metadata(d: &DirEntry, use_apparent_size: bool) -> Option<(u64, Option<(u64, u64)>)> { use std::os::unix::fs::MetadataExt; - match d.metadata().ok() { - Some(md) => { - let inode = Some((md.ino(), md.dev())); - if use_apparent_size { - Some((md.len(), inode)) - } else { - Some((md.blocks() * get_block_size(), inode)) - } + d.metadata().ok().map_or(None, |md| { + let inode = Some((md.ino(), md.dev())); + if use_apparent_size { + Some((md.len(), inode)) + } else { + Some((md.blocks() * get_block_size(), inode)) } - None => None, - } + }) } #[cfg(not(target_family = "unix"))] pub fn get_metadata(d: &DirEntry, _apparent: bool) -> Option<(u64, Option<(u64, u64)>)> { - match d.metadata().ok() { - Some(md) => Some((md.len(), None)), - None => None, - } + d.metadata().ok().map_or(None, |md| { + Some((md.len(), None)) + }) } From 8c088a70269825b04528e188284e22dc70b6e2ae Mon Sep 17 00:00:00 2001 From: bootandy Date: Tue, 1 May 2018 16:14:27 +0100 Subject: [PATCH 4/8] Fix: Passing a string into -n will no longer panic fixes: #https://github.com/bootandy/dust/issues/16 --- src/main.rs | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/main.rs b/src/main.rs index ba3db03..bf8228a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,9 +10,10 @@ use utils::{find_big_ones, get_dir_tree, sort}; mod display; mod utils; -static DEFAULT_NUMBER_OF_LINES: &'static str = "20"; +static DEFAULT_NUMBER_OF_LINES: usize = 20; fn main() { + let def_num_str = DEFAULT_NUMBER_OF_LINES.to_string(); let options = App::new("Dust") .setting(AppSettings::TrailingVarArg) .arg( @@ -28,7 +29,7 @@ fn main() { .long("number-of-lines") .help("Number of lines of output to show") .takes_value(true) - .default_value(DEFAULT_NUMBER_OF_LINES), + .default_value(def_num_str.as_ref()), ) .arg( Arg::with_name("display_full_paths") @@ -52,21 +53,24 @@ fn main() { } }; - let number_of_lines = value_t!(options.value_of("number_of_lines"), usize).unwrap(); + let number_of_lines = match value_t!(options.value_of("number_of_lines"), usize) { + Ok(v) => v, + Err(_) => { eprintln!("Bad value for number_of_lines - ignoring"); DEFAULT_NUMBER_OF_LINES} + }; + let depth = { if options.is_present("depth") { match value_t!(options.value_of("depth"), u64) { Ok(v) => Some(v + 1), - Err(_) => None, + Err(_) => { eprintln!("Bad value for depth - ignoring"); None}, } } else { None } }; - if options.is_present("depth") - && options.value_of("number_of_lines").unwrap() != DEFAULT_NUMBER_OF_LINES + if options.is_present("depth") && number_of_lines != DEFAULT_NUMBER_OF_LINES { - eprintln!("Use either -n for number of directories to show. Or -d for depth. Not both"); + eprintln!("Use either -n or -d. Not both"); return; } From 2556885622940621f6167189822feb38569c10fb Mon Sep 17 00:00:00 2001 From: bootandy Date: Tue, 1 May 2018 16:16:04 +0100 Subject: [PATCH 5/8] Rust format --- src/main.rs | 13 +++++++++---- src/utils/mod.rs | 4 ++-- src/utils/platform.rs | 4 +--- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/main.rs b/src/main.rs index bf8228a..6e664fb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -55,21 +55,26 @@ fn main() { let number_of_lines = match value_t!(options.value_of("number_of_lines"), usize) { Ok(v) => v, - Err(_) => { eprintln!("Bad value for number_of_lines - ignoring"); DEFAULT_NUMBER_OF_LINES} + Err(_) => { + eprintln!("Bad value for number_of_lines - ignoring"); + DEFAULT_NUMBER_OF_LINES + } }; let depth = { if options.is_present("depth") { match value_t!(options.value_of("depth"), u64) { Ok(v) => Some(v + 1), - Err(_) => { eprintln!("Bad value for depth - ignoring"); None}, + Err(_) => { + eprintln!("Bad value for depth - ignoring"); + None + } } } else { None } }; - if options.is_present("depth") && number_of_lines != DEFAULT_NUMBER_OF_LINES - { + if options.is_present("depth") && number_of_lines != DEFAULT_NUMBER_OF_LINES { eprintln!("Use either -n or -d. Not both"); return; } diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 7f32a0a..36e7d67 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -1,6 +1,6 @@ +use std::cmp::Ordering; use std::collections::HashMap; use std::collections::HashSet; -use std::cmp::Ordering; use walkdir::WalkDir; @@ -81,7 +81,7 @@ fn examine_dir( } } } -pub fn compare_tuple(a :&(String, u64), b: &(String, u64)) -> Ordering { +pub fn compare_tuple(a: &(String, u64), b: &(String, u64)) -> Ordering { let result = b.1.cmp(&a.1); if result == Ordering::Equal { a.0.cmp(&b.0) diff --git a/src/utils/platform.rs b/src/utils/platform.rs index 3d710b0..9945604 100644 --- a/src/utils/platform.rs +++ b/src/utils/platform.rs @@ -21,7 +21,5 @@ pub fn get_metadata(d: &DirEntry, use_apparent_size: bool) -> Option<(u64, Optio #[cfg(not(target_family = "unix"))] pub fn get_metadata(d: &DirEntry, _apparent: bool) -> Option<(u64, Option<(u64, u64)>)> { - d.metadata().ok().map_or(None, |md| { - Some((md.len(), None)) - }) + d.metadata().ok().map_or(None, |md| Some((md.len(), None))) } From 25d1ee7b43aa0d38a9a5d7e83d39e573f89f7587 Mon Sep 17 00:00:00 2001 From: bootandy Date: Wed, 2 May 2018 00:17:40 +0100 Subject: [PATCH 6/8] Remove naked unwrap --- src/display.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/display.rs b/src/display.rs index 4a8a068..49131c7 100644 --- a/src/display.rs +++ b/src/display.rs @@ -141,8 +141,8 @@ pub fn format_string( indentation: &str, ) -> String { let printable_name = { - if short_paths && dir_name.contains('/') { - dir_name.split('/').last().unwrap() + if short_paths { + dir_name.split('/').last().unwrap_or(dir_name) } else { dir_name } From dba465a094d691280ed790b6392ce10e1738d839 Mon Sep 17 00:00:00 2001 From: bootandy Date: Wed, 2 May 2018 00:17:56 +0100 Subject: [PATCH 7/8] Tweak error message --- src/main.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index 6e664fb..f64e707 100644 --- a/src/main.rs +++ b/src/main.rs @@ -56,7 +56,7 @@ fn main() { let number_of_lines = match value_t!(options.value_of("number_of_lines"), usize) { Ok(v) => v, Err(_) => { - eprintln!("Bad value for number_of_lines - ignoring"); + eprintln!("Ignoring bad value for number_of_lines"); DEFAULT_NUMBER_OF_LINES } }; @@ -66,7 +66,7 @@ fn main() { match value_t!(options.value_of("depth"), u64) { Ok(v) => Some(v + 1), Err(_) => { - eprintln!("Bad value for depth - ignoring"); + eprintln!("Ignoring bad value for depth"); None } } From 3e9f09e3398881ab603b4170b81b9571bc83e187 Mon Sep 17 00:00:00 2001 From: bootandy Date: Wed, 2 May 2018 00:42:03 +0100 Subject: [PATCH 8/8] Remove unnecessary path & pathbuf code --- src/utils/mod.rs | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 36e7d67..2af7d3d 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -4,9 +4,6 @@ use std::collections::HashSet; use walkdir::WalkDir; -use std::path::Path; -use std::path::PathBuf; - mod platform; use self::platform::*; @@ -22,7 +19,7 @@ pub fn get_dir_tree( for b in filenames { let top_level_name = strip_end_slashes(b); examine_dir( - &Path::new(&top_level_name).to_path_buf(), + &top_level_name, apparent_size, &mut inodes, &mut data, @@ -42,7 +39,7 @@ fn strip_end_slashes(s: &str) -> String { } fn examine_dir( - top_dir: &PathBuf, + top_dir: &String, apparent_size: bool, inodes: &mut HashSet<(u64, u64)>, data: &mut HashMap, @@ -66,9 +63,9 @@ fn examine_dir( let mut e_path = e.path().to_path_buf(); loop { let path_name = e_path.to_string_lossy().to_string(); - let s = data.entry(path_name).or_insert(0); + let s = data.entry(path_name.clone()).or_insert(0); *s += size; - if e_path == *top_dir { + if path_name == *top_dir { break; } e_path.pop();