gokcehan / lf

Terminal file manager
MIT License
7.72k stars 330 forks source link

recursively select files #371

Open mohkale opened 4 years ago

mohkale commented 4 years ago

Today I had a simple problem. I wanted to select all files at depth 2.

eg.

foo/
foo/bar
foo/baz
foo/bag
...
bar/
bar/foo
bar/baz
bar/bag
...

but lf doesn't seem to support recursive globs I.E. :glob-select **/*. In ranger I'd normally do :flat 0 and then filter out directories so I can select the files, but lf doesn't have that feature yet so is there another recommended way?

I tried the subprocess approach:

find -type f -maxdepth 2 -mindepth 2 -exec lf -remote "send $id :select \"{}\"" ';'

but that spawns a tonne of lf processes and seems to hang indefinitely if you try to do too many files at once.

gotroyb127 commented 4 years ago

Here's what i use:

cmd SelRecurs %{{
    IFS=$'\n'
    [[ ! -d $f ]] && exit 0
    for d in $(find $f -type d); do
        lf -remote "send $id select '$d'"$'\n'\
\   \   \   "send $id open"$'\n'\
\   \   \   "send $id invert"
    done
    lf -remote "send $id select '$f'"
}}

This selectes every subdirectory of the selected directory, enters it selects (actually toggles selection on) everything inside that (and then repeat to a next one, if there is any). (The IFS=$'\n' is used because I use the default ifs value "\n" )

It have worked for me very well for what I use it and it seems to be effective even for directories with too many subdirectories.

You can modify it to fit your needs.

gokcehan commented 4 years ago

@mohkale Easiest way to fix this would be to add recursive capabilities to our current glob-select command as you mention much like globstar in bash.

mohkale commented 4 years ago

It'd be nice if we could simply use :glob-select */*. That way, at least for simple cases, we could specify how many directories deep it should be able to go.

I'm not sure how useful :glob-select **/* would be in practice.

gokcehan commented 4 years ago

@mohkale I'm hesitant to add a custom syntax for this purpose. Is it used somewhere?

mohkale commented 4 years ago

I've never seen something like this explicitly demonstrated, but I believe its the expected behavior with globs. It seems to work in:

I presume if you allow recursive globs than globs like this will also be enabled, but I'm not at all familiar with golang so I can't say for sure.

gokcehan commented 4 years ago

@mohkale So then it is used somewhere. I also realized this already works in Go as well. But we're using the base name of a file in glob-select implementation and we only check the files in the current directory. We need to walk the directory to implement this though we should somehow do this efficiently in a smart way. It may be a little tricky to implement.

jpggithub commented 1 year ago

The flat command in ranger is very useful, not only for selecting files but also to open them or just to have a glimpse of them. If you have a flat command, you can do many things. If you only modify `glob-select`, you can only... select files ;-)