feat: listing files only using '--only-files' flag

This commit is contained in:
Hulxv 2023-09-19 00:14:33 +03:00
parent 7e60ba16b7
commit 7edd62d31e
4 changed files with 18 additions and 3 deletions

View file

@ -41,6 +41,9 @@ pub struct FileFilter {
/// Whether to only show directories.
pub only_dirs: bool,
/// Whether to only show files.
pub only_files: bool,
/// Which invisible “dot” files to include when listing a directory.
///
/// Files starting with a single “.” are used to determine “system” or
@ -68,9 +71,18 @@ impl FileFilter {
pub fn filter_child_files(&self, files: &mut Vec<File<'_>>) {
files.retain(|f| ! self.ignore_patterns.is_ignored(&f.name));
if self.only_dirs {
files.retain(File::is_directory);
match (self.only_dirs, self.only_files) {
(true, false) => {
// On pass -'-only-dirs' flag only
files.retain(File::is_directory);
}
(false, true) => {
// On pass -'-only-files' flag only
files.retain(File::is_file)
}
_ => {}
}
}
/// Remove every file in the given vector that does *not* pass the

View file

@ -15,6 +15,7 @@ impl FileFilter {
list_dirs_first: matches.has(&flags::DIRS_FIRST)?,
reverse: matches.has(&flags::REVERSE)?,
only_dirs: matches.has(&flags::ONLY_DIRS)?,
only_files: matches.has(&flags::ONLY_FILES)?,
sort_field: SortField::deduce(matches)?,
dot_filter: DotFilter::deduce(matches)?,
ignore_patterns: IgnorePatterns::deduce(matches)?,

View file

@ -34,6 +34,7 @@ pub static IGNORE_GLOB: Arg = Arg { short: Some(b'I'), long: "ignore-glob", take
pub static GIT_IGNORE: Arg = Arg { short: None, long: "git-ignore", takes_value: TakesValue::Forbidden };
pub static DIRS_FIRST: Arg = Arg { short: None, long: "group-directories-first", takes_value: TakesValue::Forbidden };
pub static ONLY_DIRS: Arg = Arg { short: Some(b'D'), long: "only-dirs", takes_value: TakesValue::Forbidden };
pub static ONLY_FILES: Arg = Arg { short: Some(b'f'), long: "only-files", takes_value: TakesValue::Forbidden };
const SORTS: Values = &[ "name", "Name", "size", "extension",
"Extension", "modified", "changed", "accessed",
"created", "inode", "type", "none" ];
@ -83,7 +84,7 @@ pub static ALL_ARGS: Args = Args(&[
&COLOR, &COLOUR, &COLOR_SCALE, &COLOUR_SCALE, &WIDTH,
&ALL, &ALMOST_ALL, &LIST_DIRS, &LEVEL, &REVERSE, &SORT, &DIRS_FIRST,
&IGNORE_GLOB, &GIT_IGNORE, &ONLY_DIRS,
&IGNORE_GLOB, &GIT_IGNORE, &ONLY_DIRS, &ONLY_FILES,
&BINARY, &BYTES, &GROUP, &NUMERIC, &HEADER, &ICONS, &INODE, &LINKS, &MODIFIED, &CHANGED,
&BLOCKSIZE, &TIME, &ACCESSED, &CREATED, &TIME_STYLE, &HYPERLINK, &MOUNTS,

View file

@ -35,6 +35,7 @@ FILTERING AND SORTING OPTIONS
-s, --sort SORT_FIELD which field to sort by
--group-directories-first list directories before other files
-D, --only-dirs list only directories
-f, --only-files list only files
-I, --ignore-glob GLOBS glob patterns (pipe-separated) of files to ignore";
static GIT_FILTER_HELP: &str = " \