From 81c8d8d3bf81db72630c7c0a61f26bd4d4a3a0c7 Mon Sep 17 00:00:00 2001 From: Grump Date: Wed, 13 May 2020 19:24:43 +0200 Subject: [PATCH] added getDirectoryItemsInfo --- README.md | 22 ++++++++++++++++------ nativefs.lua | 17 +++++++++++++++++ test/main.lua | 18 ++++++++++++++++++ 3 files changed, 51 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index b71ebdb..ba2ee2b 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,8 @@ nativefs replicates a subset of the [love.filesystem](https://love2d.org/wiki/lo ### nativefs +Link in this list point to their `love.filesystem` counterparts. All functions are designed to behave the same way as `love.filesystem`, but without the path restrictions that LÖVE imposes; i.e., they allow full access to the filesystem. + * [nativefs.newFile](https://love2d.org/wiki/love.filesystem.newFile) * [nativefs.newFileData](https://love2d.org/wiki/love.filesystem.newFileData) * [nativefs.mount](https://love2d.org/wiki/love.filesystem.mount) @@ -20,15 +22,21 @@ nativefs replicates a subset of the [love.filesystem](https://love2d.org/wiki/lo * [nativefs.getInfo](https://love2d.org/wiki/love.filesystem.getInfo) * [nativefs.createDirectory](https://love2d.org/wiki/love.filesystem.createDirectory) * [nativefs.remove](https://love2d.org/wiki/love.filesystem.remove) +* nativefs.getDirectoryItemsInfo +* nativefs.getDriveList +* nativefs.setWorkingDirectory -Function names in this list are links to their `love.filesystem` counterparts. All functions are designed to behave the same way as `love.filesystem`, but without the path restrictions that LÖVE imposes; i.e., they allow full access to the filesystem. +#### Additional `nativefs` functions -Additional functions that are not available in `love.filesystem`: +Functions that are not available in `love.filesystem`: -* `nativefs.getDriveList` +* `nativefs.getDirectoryItemsInfo(path)` +Returns a list of items in a directory that contains not only the names, but also the information returned by `getInfo` for each item. The return value is a list of files, in which each entry is a table as returned by [getInfo](https://love2d.org/wiki/love.filesystem.getInfo), with an additional `name` field for each file. + +* `nativefs.getDriveList()` Returns a table of all populated drive letters on Windows (`{ 'C:/', 'D:/', ...}`). On systems that don't use drive letters, a table with the single entry `/` is returned. -* `nativefs.setWorkingDirectory` +* `nativefs.setWorkingDirectory(directory)` Changes the working directory. ### File @@ -60,9 +68,11 @@ Function names in this list are links to their LÖVE `File` counterparts. `File` local nativefs = require("nativefs") -- deletes all files in C:\Windows -local files = nativefs.getDirectoryItems("C:/Windows") +local files = nativefs.getDirectoryItemsInfo("C:/Windows") for i = 1, #files do - nativefs.remove("C:/Windows/" .. files[i]) + if files[i].type == "file" then + nativefs.remove("C:/Windows/" .. files[i].name) + end end ``` ## License diff --git a/nativefs.lua b/nativefs.lua index 7f9349c..3a2d466 100644 --- a/nativefs.lua +++ b/nativefs.lua @@ -317,6 +317,23 @@ function nativefs.getDirectoryItems(dir) return result or {} end +function nativefs.getDirectoryItemsInfo(path, filtertype) + local result, err = withTempMount(path, function(mount) + local items = {} + local files = love.filesystem.getDirectoryItems(mount) + for i = 1, #files do + local filepath = string.format('%s/%s', mount, files[i]) + local info = love.filesystem.getInfo(filepath, filtertype) + if info then + info.name = files[i] + table.insert(items, info) + end + end + return items + end) + return result or {} +end + function nativefs.getInfo(path, filtertype) local dir = path:match("(.*[\\/]).*$") or './' local file = love.path.leaf(path) diff --git a/test/main.lua b/test/main.lua index 85be65e..c5bdd50 100644 --- a/test/main.lua +++ b/test/main.lua @@ -138,6 +138,24 @@ function test_fs_getDirectoryItems() equals(#fs.getDirectoryItems('does_not_exist'), 0) end +function test_fs_getDirectoryItemsInfo() + local items, map = fs.getDirectoryItems('data'), {} + for i = 1, #items do map[items[i]] = true end + local itemsEx, mapEx = fs.getDirectoryItemsInfo('data'), {} + for i = 1, #itemsEx do mapEx[itemsEx[i].name] = itemsEx[i] end + + equals(#items, #itemsEx) + for i = 1, #itemsEx do + local item = itemsEx[i] + equals(map[item.name], true) + local info = love.filesystem.getInfo('data/' .. item.name) + equals(info.type, item.type) + equals(info.size, item.size) + equals(info.modtime, item.modtime) + end + equals(#fs.getDirectoryItemsInfo('does_not_exist'), 0) +end + function test_fs_setWorkingDirectory() local wd = fs.getWorkingDirectory()