GithubHelp home page GithubHelp logo

More ways to anchor patterns about fd HOT 5 OPEN

g2p avatar g2p commented on June 19, 2024
More ways to anchor patterns

from fd.

Comments (5)

tavianator avatar tavianator commented on June 19, 2024

For a workaround on the current version of fd, I believe sed 's/\([][\\*?]\)/\\\1/g' is enough to escape a string for globs. So something like

tavianator@graphene $ touch '\*?[]'
tavianator@graphene $ fd -g "$(echo '\*?[]' | sed 's/\([][\\*?]\)/\\\1/g')"
\*?[]
tavianator@graphene $ find -name "$(echo '\*?[]' | sed 's/\([][\\*?]\)/\\\1/g')"
./\*?[]

You could make a shell function for it:

globesc() {
    printf '%s' "$1" | sed 's/\([][\\*?]\)/\\\1/g'
}

fd -g "$(globesc '\*?[]')"

from fd.

tavianator avatar tavianator commented on June 19, 2024

I would not be terribly opposed to a new flag like --anchored, e.g. fd --anchored --fixed-strings '\*?[]'. I'm not sure what @sharkdp thinks. There sure are a lot of flags already.

from fd.

sharkdp avatar sharkdp commented on June 19, 2024

So --anchored would have an effect on regex patterns and on fixed-string patterns, but not on glob patterns (because they are anchored already)?

Or would we introduce this as an option instead of a flag (--anchored=true/false), and make it work for all three pattern options?

from fd.

g2p avatar g2p commented on June 19, 2024

I've started prototyping, here's the help output:

-F, --fixed-strings
    Treat the pattern as a literal string instead of a regular expression. Note that the
    pattern would still match on a substring of the input. If you want to match on an exact
    filename, consider adding '--anchor=input' as well.

… after the --full-path flag

--no-anchor
    By default, with --glob and no --anchor arguments, the pattern must match the whole
    input (--anchor=input). This flag allows a glob pattern to match any part of the input.

--anchor <ANCHOR>
    By default, the search pattern must match the whole input for --glob (--anchor=input),
    while it can match any part of the input for --regex and --fixed-strings (--no-anchor).
    (See the --full-path option for what constitutes input)
    
    This flag allows other ways to anchor the pattern.
    
    [possible values: input-start, input-end, input, word]

(I refrained from making it as general as possible: no word-start / word-end / additive ArgAction::Append flags that are unlikely to be used. But because I had it in mind, I prefer --no-anchor to an --anchor=none that would not be additive)

from fd.

g2p avatar g2p commented on June 19, 2024

globset would need to be patched, however, as anchoring is always done inside Token::to_regex_with with no builder customisation. But since globs are written by hand and still easy to script if needed, having --anchor conflict with --glob works for me.

from fd.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.