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

Add readdir() #2439

Closed
wants to merge 14 commits into from
Closed

Add readdir() #2439

wants to merge 14 commits into from

Conversation

mattn
Copy link
Member

@mattn mattn commented Dec 13, 2017

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.

func Test_readdir()
  call mkdir('Xdir')
  call writefile([], 'Xdir/foo.txt')
  call writefile([], 'Xdir/bar.txt')
  call mkdir('Xdir/dir')

  let files = readdir('Xdir')
  call assert_equal(['bar.txt', 'dir', 'foo.txt'], files)

  let files = readdir('Xdir', {x->stridx(x,'f')!=-1})
  call assert_equal(['foo.txt'], files)

  let l = []
  let files = readdir('Xdir', {x->len(add(l, x)) == 2 ? -1 : 1})
  call assert_equal(1, len(files))

  call delete('Xdir', 'rf')
endfunc

@mattn mattn mentioned this pull request Dec 13, 2017
3 tasks
@codecov-io
Copy link

codecov-io commented Dec 13, 2017

Codecov Report

Merging #2439 into master will increase coverage by 0.02%.
The diff coverage is 90.9%.

Impacted file tree graph

@@            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
Impacted Files Coverage Δ
src/eval.c 81.01% <ø> (ø) ⬆️
src/evalfunc.c 83.84% <90.9%> (+0.07%) ⬆️
src/channel.c 82.68% <0%> (-0.13%) ⬇️
src/if_py_both.h 76.59% <0%> (-0.01%) ⬇️
src/window.c 81.28% <0%> (+0.06%) ⬆️
src/gui.c 47.5% <0%> (+0.2%) ⬆️
src/term.c 50.75% <0%> (+0.21%) ⬆️
src/message.c 68.72% <0%> (+0.24%) ⬆️
src/gui_gtk_x11.c 47.8% <0%> (+0.24%) ⬆️
src/if_xcmdsrv.c 84.71% <0%> (+0.53%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a6d4849...efbb6d8. Read the comment docs.

@@ -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}])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{max} -> {expr}

@chrisbra
Copy link
Member

it might be silly, but would it make sense to have an indication on the returned list, whether the resulting entry is a directory?
E.g. instead of this:

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?

@mattn
Copy link
Member Author

mattn commented Dec 13, 2017

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.

@chrisbra
Copy link
Member

that example should be added to the documentation :)

return {a:dir : map(readdir(a:dir), {_,x ->
\ isdirectory(x) ? {x : s:tree(a:dir . '/' . x)} : x})}
endfunction
echo s:tree(".")

Copy link
Member

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.

@Shougo
Copy link
Contributor

Shougo commented Dec 14, 2017

#696
#1099

It may fix the delete('rf')problems.
delete('rf') uses glob() internally.
But it should use readdir() instead.

@brammool
Copy link
Contributor

brammool commented Dec 14, 2017 via email

@brammool brammool closed this in 543c9b1 Apr 5, 2019
justinmk pushed a commit to neovim/neovim that referenced this pull request May 5, 2020
Problem:    Cannot easily get directory entry matches.
Solution:   Add the readdir() function. (Yasuhiro Matsumoto, closes vim/vim#2439)
vim/vim@543c9b1

closes #12212
landerlo pushed a commit to landerlo/neovim that referenced this pull request Nov 23, 2020
…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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants