From 5226ba963c8f5837b322ae307b3471a330bb7dd7 Mon Sep 17 00:00:00 2001 From: Kevin Robert Stravers Date: Wed, 27 Jul 2016 11:29:27 +0200 Subject: [PATCH] ls: Implement the '-a' (all) flag The all flag did not cull/remove the directory entries starting with a dot. The help message indicates it should. The implementation checks if the string starts with a dot whilst also using '-a' to determine whether a DirEntry is to be printed. --- src/ls/ls.rs | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/ls/ls.rs b/src/ls/ls.rs index 86cd40b47..4450cb871 100644 --- a/src/ls/ls.rs +++ b/src/ls/ls.rs @@ -154,9 +154,23 @@ fn max(lhs: usize, rhs: usize) -> usize { } } +fn should_cull_dot(file_name: &DirEntry, view_all: bool) -> bool { + let file_name = file_name.file_name(); + file_name.to_str().map_or(false, |x| { + if view_all { + false + } else if x.chars().next().unwrap() == '.' { + true + } else { + false + } + }) +} + fn enter_directory(contents: ReadDir, options: &getopts::Matches) { let contents = contents.collect::>(); let (mut max_links, mut max_size) = (1, 1); + let culling_dot = options.opt_present("a"); for entry in &contents { let entry = match *entry { Err(ref err) => { @@ -165,6 +179,9 @@ fn enter_directory(contents: ReadDir, options: &getopts::Matches) { } Ok(ref en) => en, }; + if should_cull_dot(&entry, culling_dot) { + continue; + } let (links, size) = display_dir_entry_size(entry, options); max_links = max(links, max_links); max_size = max(size, max_size); @@ -178,7 +195,9 @@ fn enter_directory(contents: ReadDir, options: &getopts::Matches) { } Ok(ref en) => en, }; - + if should_cull_dot(&entry, culling_dot) { + continue; + } // Currently have a DirEntry that we can believe in. display_dir_entry(entry, options, max_links, max_size); }