2016-04-17 19:38:37 +00:00
|
|
|
|
use getopts;
|
2016-10-30 14:43:33 +00:00
|
|
|
|
use glob;
|
2016-04-17 19:38:37 +00:00
|
|
|
|
|
2017-06-27 00:13:50 +00:00
|
|
|
|
use fs::DotFilter;
|
2017-07-24 07:34:50 +00:00
|
|
|
|
use fs::filter::{FileFilter, SortField, SortCase, IgnorePatterns};
|
2016-04-17 19:38:37 +00:00
|
|
|
|
use options::misfire::Misfire;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl FileFilter {
|
|
|
|
|
|
|
|
|
|
/// Determines the set of file filter options to use, based on the user’s
|
|
|
|
|
/// command-line arguments.
|
|
|
|
|
pub fn deduce(matches: &getopts::Matches) -> Result<FileFilter, Misfire> {
|
|
|
|
|
Ok(FileFilter {
|
|
|
|
|
list_dirs_first: matches.opt_present("group-directories-first"),
|
|
|
|
|
reverse: matches.opt_present("reverse"),
|
2017-03-26 16:35:50 +00:00
|
|
|
|
sort_field: SortField::deduce(matches)?,
|
2017-06-29 11:07:46 +00:00
|
|
|
|
dot_filter: DotFilter::deduce(matches)?,
|
2017-03-26 16:35:50 +00:00
|
|
|
|
ignore_patterns: IgnorePatterns::deduce(matches)?,
|
2016-04-17 19:38:37 +00:00
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl Default for SortField {
|
|
|
|
|
fn default() -> SortField {
|
|
|
|
|
SortField::Name(SortCase::Sensitive)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl SortField {
|
|
|
|
|
|
|
|
|
|
/// Determine the sort field to use, based on the presence of a “sort”
|
|
|
|
|
/// argument. This will return `Err` if the option is there, but does not
|
|
|
|
|
/// correspond to a valid field.
|
|
|
|
|
fn deduce(matches: &getopts::Matches) -> Result<SortField, Misfire> {
|
2017-06-23 21:58:07 +00:00
|
|
|
|
|
|
|
|
|
const SORTS: &[&str] = &[ "name", "Name", "size", "extension",
|
|
|
|
|
"Extension", "modified", "accessed",
|
2017-06-29 13:50:39 +00:00
|
|
|
|
"created", "inode", "type", "none" ];
|
2017-06-23 21:58:07 +00:00
|
|
|
|
|
2016-04-17 19:38:37 +00:00
|
|
|
|
if let Some(word) = matches.opt_str("sort") {
|
|
|
|
|
match &*word {
|
|
|
|
|
"name" | "filename" => Ok(SortField::Name(SortCase::Sensitive)),
|
|
|
|
|
"Name" | "Filename" => Ok(SortField::Name(SortCase::Insensitive)),
|
|
|
|
|
"size" | "filesize" => Ok(SortField::Size),
|
|
|
|
|
"ext" | "extension" => Ok(SortField::Extension(SortCase::Sensitive)),
|
|
|
|
|
"Ext" | "Extension" => Ok(SortField::Extension(SortCase::Insensitive)),
|
|
|
|
|
"mod" | "modified" => Ok(SortField::ModifiedDate),
|
|
|
|
|
"acc" | "accessed" => Ok(SortField::AccessedDate),
|
|
|
|
|
"cr" | "created" => Ok(SortField::CreatedDate),
|
|
|
|
|
"inode" => Ok(SortField::FileInode),
|
2017-06-29 13:50:39 +00:00
|
|
|
|
"type" => Ok(SortField::FileType),
|
|
|
|
|
"none" => Ok(SortField::Unsorted),
|
2017-06-23 21:58:07 +00:00
|
|
|
|
field => Err(Misfire::bad_argument("sort", field, SORTS))
|
2016-04-17 19:38:37 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
Ok(SortField::default())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-10-30 14:43:33 +00:00
|
|
|
|
|
|
|
|
|
|
2017-06-26 22:28:10 +00:00
|
|
|
|
impl DotFilter {
|
2017-06-29 11:07:46 +00:00
|
|
|
|
pub fn deduce(matches: &getopts::Matches) -> Result<DotFilter, Misfire> {
|
|
|
|
|
let dots = match matches.opt_count("all") {
|
|
|
|
|
0 => return Ok(DotFilter::JustFiles),
|
2017-06-27 00:13:50 +00:00
|
|
|
|
1 => DotFilter::Dotfiles,
|
|
|
|
|
_ => DotFilter::DotfilesAndDots,
|
2017-06-29 11:07:46 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if matches.opt_present("tree") {
|
|
|
|
|
Err(Misfire::Useless("all --all", true, "tree"))
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
Ok(dots)
|
2017-06-26 22:48:55 +00:00
|
|
|
|
}
|
2017-06-26 22:28:10 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2016-10-30 14:43:33 +00:00
|
|
|
|
impl IgnorePatterns {
|
2017-07-24 07:34:50 +00:00
|
|
|
|
|
2016-10-30 14:43:33 +00:00
|
|
|
|
/// Determines the set of file filter options to use, based on the user’s
|
|
|
|
|
/// command-line arguments.
|
|
|
|
|
pub fn deduce(matches: &getopts::Matches) -> Result<IgnorePatterns, Misfire> {
|
|
|
|
|
let patterns = match matches.opt_str("ignore-glob") {
|
|
|
|
|
None => Ok(Vec::new()),
|
|
|
|
|
Some(is) => is.split('|').map(|a| glob::Pattern::new(a)).collect(),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Ok(IgnorePatterns {
|
2017-03-26 16:35:50 +00:00
|
|
|
|
patterns: patterns?,
|
2016-10-30 14:43:33 +00:00
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|