Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(exa PR) 1064: Add -A option for more compatibility with ls #43

Merged
merged 11 commits into from
Jul 30, 2023
70 changes: 40 additions & 30 deletions src/options/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,27 +150,32 @@ impl DotFilter {

/// Determines the dot filter based on how many `--all` options were
/// given: one will show dotfiles, but two will show `.` and `..` too.
/// --almost-all is equivalent to --all, included for compatibility with
/// `ls -A`.
///
/// It also checks for the `--tree` option in strict mode, because of a
/// special case where `--tree --all --all` won’t work: listing the
/// parent directory in tree mode would loop onto itself!
/// It also checks for the `--tree` option, because of a special case
/// where `--tree --all --all` won’t work: listing the parent directory
/// in tree mode would loop onto itself!
///
/// `--almost-all` binds stronger than multiple `--all` as we currently do not take the order
/// of arguments into account and it is the safer option (does not clash with `--tree`)
pub fn deduce(matches: &MatchedFlags<'_>) -> Result<Self, OptionsError> {
let count = matches.count(&flags::ALL);

if count == 0 {
Ok(Self::JustFiles)
}
else if count == 1 {
Ok(Self::Dotfiles)
}
else if matches.count(&flags::TREE) > 0 {
Err(OptionsError::TreeAllAll)
}
else if count >= 3 && matches.is_strict() {
Err(OptionsError::Conflict(&flags::ALL, &flags::ALL))
}
else {
Ok(Self::DotfilesAndDots)
let all_count = matches.count(&flags::ALL);
let has_almost_all = matches.has(&flags::ALMOST_ALL)?;

match (all_count, has_almost_all) {
(0, false) => Ok(Self::JustFiles),

// either a single --all or at least one --almost-all is given
(1, _) | (0, true) => Ok(Self::Dotfiles),
// more than one --all
(c, _) => if matches.count(&flags::TREE) > 0 {
Err(OptionsError::TreeAllAll)
} else if matches.is_strict() && c > 2 {
Err(OptionsError::Conflict(&flags::ALL, &flags::ALL))
} else {
Ok(Self::DotfilesAndDots)
},
}
}
}
Expand Down Expand Up @@ -231,7 +236,7 @@ mod test {
use crate::options::test::parse_for_test;
use crate::options::test::Strictnesses::*;

static TEST_ARGS: &[&Arg] = &[ &flags::SORT, &flags::ALL, &flags::TREE, &flags::IGNORE_GLOB, &flags::GIT_IGNORE ];
static TEST_ARGS: &[&Arg] = &[ &flags::SORT, &flags::ALL, &flags::ALMOST_ALL, &flags::TREE, &flags::IGNORE_GLOB, &flags::GIT_IGNORE ];
for result in parse_for_test($inputs.as_ref(), TEST_ARGS, $stricts, |mf| $type::deduce(mf)) {
assert_eq!(result, $result);
}
Expand All @@ -246,7 +251,7 @@ mod test {
test!(empty: SortField <- []; Both => Ok(SortField::default()));

// Sort field arguments
test!(one_arg: SortField <- ["--sort=mod"]; Both => Ok(SortField::ModifiedDate));
test!(one_arg: SortField <- ["--sort=mod"]; Both => Ok(SortField::ModifiedDate));
test!(one_long: SortField <- ["--sort=size"]; Both => Ok(SortField::Size));
test!(one_short: SortField <- ["-saccessed"]; Both => Ok(SortField::AccessedDate));
test!(lowercase: SortField <- ["--sort", "name"]; Both => Ok(SortField::Name(SortCase::AaBbCc)));
Expand Down Expand Up @@ -275,20 +280,25 @@ mod test {
use super::*;

// Default behaviour
test!(empty: DotFilter <- []; Both => Ok(DotFilter::JustFiles));
test!(empty: DotFilter <- []; Both => Ok(DotFilter::JustFiles));

// --all
test!(all: DotFilter <- ["--all"]; Both => Ok(DotFilter::Dotfiles));
test!(all_all: DotFilter <- ["--all", "-a"]; Both => Ok(DotFilter::DotfilesAndDots));
test!(all_all_2: DotFilter <- ["-aa"]; Both => Ok(DotFilter::DotfilesAndDots));
test!(all: DotFilter <- ["--all"]; Both => Ok(DotFilter::Dotfiles));
test!(all_all: DotFilter <- ["--all", "-a"]; Both => Ok(DotFilter::DotfilesAndDots));
test!(all_all_2: DotFilter <- ["-aa"]; Both => Ok(DotFilter::DotfilesAndDots));

test!(all_all_3: DotFilter <- ["-aaa"]; Last => Ok(DotFilter::DotfilesAndDots));
test!(all_all_4: DotFilter <- ["-aaa"]; Complain => Err(OptionsError::Conflict(&flags::ALL, &flags::ALL)));
test!(all_all_3: DotFilter <- ["-aaa"]; Last => Ok(DotFilter::DotfilesAndDots));
test!(all_all_4: DotFilter <- ["-aaa"]; Complain => Err(OptionsError::Conflict(&flags::ALL, &flags::ALL)));

// --all and --tree
test!(tree_a: DotFilter <- ["-Ta"]; Both => Ok(DotFilter::Dotfiles));
test!(tree_aa: DotFilter <- ["-Taa"]; Both => Err(OptionsError::TreeAllAll));
test!(tree_aaa: DotFilter <- ["-Taaa"]; Both => Err(OptionsError::TreeAllAll));
test!(tree_a: DotFilter <- ["-Ta"]; Both => Ok(DotFilter::Dotfiles));
test!(tree_aa: DotFilter <- ["-Taa"]; Both => Err(OptionsError::TreeAllAll));
test!(tree_aaa: DotFilter <- ["-Taaa"]; Both => Err(OptionsError::TreeAllAll));

// --almost-all
test!(almost_all: DotFilter <- ["--almost-all"]; Both => Ok(DotFilter::Dotfiles));
test!(almost_all_all: DotFilter <- ["-Aa"]; Both => Ok(DotFilter::Dotfiles));
test!(almost_all_all_2: DotFilter <- ["-Aaa"]; Both => Ok(DotFilter::DotfilesAndDots));
}


Expand Down
3 changes: 2 additions & 1 deletion src/options/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub static COLOUR_SCALE: Arg = Arg { short: None, long: "colour-scale", takes_va

// filtering and sorting options
pub static ALL: Arg = Arg { short: Some(b'a'), long: "all", takes_value: TakesValue::Forbidden };
pub static ALMOST_ALL: Arg = Arg { short: Some(b'A'), long: "almost-all", takes_value: TakesValue::Forbidden };
pub static LIST_DIRS: Arg = Arg { short: Some(b'd'), long: "list-dirs", takes_value: TakesValue::Forbidden };
pub static LEVEL: Arg = Arg { short: Some(b'L'), long: "level", takes_value: TakesValue::Necessary(None) };
pub static REVERSE: Arg = Arg { short: Some(b'r'), long: "reverse", takes_value: TakesValue::Forbidden };
Expand Down Expand Up @@ -77,7 +78,7 @@ pub static ALL_ARGS: Args = Args(&[
&ONE_LINE, &LONG, &GRID, &ACROSS, &RECURSE, &TREE, &CLASSIFY,
&COLOR, &COLOUR, &COLOR_SCALE, &COLOUR_SCALE,

&ALL, &LIST_DIRS, &LEVEL, &REVERSE, &SORT, &DIRS_FIRST,
&ALL, &ALMOST_ALL, &LIST_DIRS, &LEVEL, &REVERSE, &SORT, &DIRS_FIRST,
&IGNORE_GLOB, &GIT_IGNORE, &ONLY_DIRS,

&BINARY, &BYTES, &GROUP, &NUMERIC, &HEADER, &ICONS, &INODE, &LINKS, &MODIFIED, &CHANGED,
Expand Down