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 } diff --git a/src/main.rs b/src/main.rs index 7383a64..f64e707 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,15 +5,15 @@ 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; 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( @@ -29,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") @@ -53,23 +53,29 @@ 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!("Ignoring bad value for number_of_lines"); + 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!("Ignoring bad value for depth"); + None + } } } else { None } }; - 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!"); + 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..2af7d3d 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -1,12 +1,9 @@ +use std::cmp::Ordering; use std::collections::HashMap; use std::collections::HashSet; -use std::cmp::Ordering; 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(); @@ -81,7 +78,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 9d0451a..9945604 100644 --- a/src/utils/platform.rs +++ b/src/utils/platform.rs @@ -6,58 +6,20 @@ 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() { - 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(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)), - None => None, - } + d.metadata().ok().map_or(None, |md| Some((md.len(), None))) }