-
-
Notifications
You must be signed in to change notification settings - Fork 5.4k
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
Add readdir() #2439
Add readdir() #2439
Conversation
Codecov Report
@@ Coverage Diff @@
## master #2439 +/- ##
==========================================
+ Coverage 74.48% 74.5% +0.02%
==========================================
Files 92 92
Lines 132744 132798 +54
Branches 29123 29136 +13
==========================================
+ Hits 98878 98947 +69
+ Misses 33835 33820 -15
Partials 31 31
Continue to review full report at Codecov.
|
runtime/doc/eval.txt
Outdated
@@ -2282,6 +2282,9 @@ py3eval({expr}) any evaluate |python3| expression | |||
pyxeval({expr}) any evaluate |python_x| expression | |||
range({expr} [, {max} [, {stride}]]) | |||
List items from {expr} to {max} | |||
readdir({directory} [, {max}]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
{max}
-> {expr}
it might be silly, but would it make sense to have an indication on the returned list, whether the resulting entry is a directory? let files = readdir('Xdir')
call assert_equal(['bar.txt', 'dir', 'foo.txt'], files) let files = readdir('Xdir')
call assert_equal(['bar.txt', 'dir/', 'foo.txt'], files) one more thing to think about it, would it make sense to return a recursive list of matches? |
I'm thinking that return recursive list is over work. But it can be done with: function! s:tree(dir)
return {a:dir : map(readdir(a:dir), {_,x -> isdirectory(x) ? {x : s:tree(a:dir . '/' . x)} : x})}
endfunction
echo s:tree(".") Appending suffix '/' may be useful to know the item is directory easily. |
that example should be added to the documentation :) |
runtime/doc/eval.txt
Outdated
return {a:dir : map(readdir(a:dir), {_,x -> | ||
\ isdirectory(x) ? {x : s:tree(a:dir . '/' . x)} : x})} | ||
endfunction | ||
echo s:tree(".") | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
<
is needed in the top of this line.
Yasuhiro Matsumoto wrote:
glob() is bit difficult to use to get file list in Vim script since it
depend on suffixes or wildignore. So this change add new API
`readdir()`.
readdir() take 1 or 2 arguments. first one is a path to the directory
to be listed files. sencond one is expr to add or ignore for the
result. 1 to add the filename. 0 to ignore. If you set -1 to expr,
readdir() stop to read. Below is test code.
Hmm, this results in a large overlap of functionality between glob() and
readdir(). Each covers a part of the functionality, a lot can be done
with either, and there will still be things that neither can do and will
have to be added later.
Using readdir() for recursive stuff is a bit complicated, while with
glob() that's easy. The use of a filter function is nice for readdir()
and lacking from glob().
Also, glob() has gained several optional arguments over the year, which
now make it clumsy to use.
How about this instead:
glob({expr} [, {nosuf} [, {list} [, {alllinks}]]])
glob({expr}, {dict})
The first form is what we already have, we keep supporting it.
The second form passes arguments in a dictionary. Members are:
nosuf as the {nosuf} argument
list as the {list} argument (could be default on)
alllinks as the {alllinks} argument
filter function like the readdir() function has
Then you can use:
global('*', filter)
like the proposed readdir() function.
But you can also do:
global('**', filter)
To make it work recursively.
We can also add an option to add a slash to directory names:
dirslash
let files = readdir('Xdir', {x->stridx(x,'f')!=-1})
If you write a lambda function, please put spaces around the "->",
otherwise it starts looking like C code :-).
…--
hundred-and-one symptoms of being an internet addict:
93. New mail alarm on your palmtop annoys other churchgoers.
/// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net \\\
/// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
\\\ an exciting new programming language -- http://www.Zimbu.org ///
\\\ help me help AIDS victims -- http://ICCF-Holland.org ///
|
Problem: Cannot easily get directory entry matches. Solution: Add the readdir() function. (Yasuhiro Matsumoto, closes vim/vim#2439) vim/vim@543c9b1 closes #12212
…12222 Problem: Cannot easily get directory entry matches. Solution: Add the readdir() function. (Yasuhiro Matsumoto, closes vim/vim#2439) vim/vim@543c9b1 closes neovim#12212
glob() is bit difficult to use to get file list in Vim script since it depend on suffixes or wildignore. So this change add new API
readdir()
.readdir() take 1 or 2 arguments. first one is a path to the directory to be listed files. sencond one is expr to add or ignore for the result. 1 to add the filename. 0 to ignore. If you set -1 to expr, readdir() stop to read. Below is test code.