olson-sean-k / wax

Opinionated and portable globs that can be matched against paths and directory trees.
https://glob.guide
MIT License
112 stars 10 forks source link

Files with leading dots and wildcard globs #30

Closed andrenth closed 2 years ago

andrenth commented 2 years ago

Hello

Wax currently matches "hidden" files with a wildcard glob, which differs from traditional Unix shell globs:

$ mkdir test
$ touch test/.x
$ cat src/main.rs 
fn main() {
    let glob = wax::Glob::new("*").unwrap();
    for entry in glob.walk_with_behavior("test", 1) {
        println!("> {:?}", entry);
    }
}
$ cargo run -q
> Ok(WalkEntry { entry: DirEntry("test/.x"), matched: MatchedText { inner: Owned(OwnedText { matched: ".x", ranges: [Some((0, 2))] }) } })

Is this by design? Do you think a behavior field could be added to make it work like shell globs?

olson-sean-k commented 2 years ago

I don't plan to implement a walk behavior for this, but in version 0.5.0 this can be accomplished using negations.

For applications like shells that wish to filter in this way regardless of the given glob expression, the filter_tree and not functions can be used. For nominally hidden files on Unix, something like this should work while accepting arbitrary glob expressions:

let glob = Glob::new(expression).unwrap();
for entry in glob.walk(path).not(["**/.*/**"]).unwrap() {
    let entry = entry.unwrap();
    println!("> {:?}", entry);
}

This pattern can also be encoded explicitly in a glob expression. For example, the expression [!.]* matches a component that does not begin with .. This can be used in a tree expression like <[!.]*/>[!.]*, which matches any number of such components (like ** but rejecting paths where any component begins with .).

andrenth commented 2 years ago

Thanks, the solution using not does it for me.