Trying to do a plugin with the new go standard library lua bindings but I can not get it to work. Below is the code. (I am new to lua)
Anyone got any ideas ?
local ioutil = import("io/ioutil")
local files , err = ioutil.ReadDir(".")
if err ~= nil then
messenger:Error("Error reading directory in filemanager plugin.")
else
for file in files do
list[i] = file.Name()
i = i + 1
end
end
It should list current directory filenames in a list.
version: 1.3.4-72
Commit hash: d6ccaf0
OS: Linux Arch
Terminal: Gnome
There are a couple problems with your program. The main one is that the for loop should be for i = 1, #files do, and then use files[i] to access the file. Here is a working version doing what you want:
local ioutil = import("io/ioutil")
local files, err = ioutil.ReadDir(".")
local list = {}
if err ~= nil then
messenger:Error("Error reading directory in filemanager plugin.")
else
for i = 1, #files do
list[i] = files[i]:Name()
end
end
Extra notes: you should also use file:Name() instead of file.Name() to call the function and you need to define the list variable.
Thank you for the quick response.
I have tested and it works perfect.
I have been trying to do this for hours. (Testing different things and googling lua pages)
Thanks again.
What does # mean in front of files in the for loop.
Is there any good websites or books on lua ?
The #files means length of the files array.
I'm not really sure what the best website/book for learning lua is but I sometimes end up reading things from this tutorial on lua.org. Things might be a bit different because you may want to call the go stdlib from Lua instead of the Lua stdlib (although the lua stdlib should be mostly supported).
Ok, thank you for all your help.
You have saved me hours and a lot of hair pulling.
I was going in circles.
I think the help plugins example is wrong for the ioutl.ReadFile
This is in the example.
local ioutil = import("ioutil")
This is what I had to use to get it to work with ReadDir
local ioutil = import("io/ioutil")
Yep, that has been fixed now.
Most helpful comment
Thank you for the quick response.
I have tested and it works perfect.
I have been trying to do this for hours. (Testing different things and googling lua pages)
Thanks again.