Lua one-liner to read entire file? - lua

Is there a trick to slurp a file with just one line of code?
("to slup" = to read entire file into a string.)
Usually I do the following:
local f = io.open("/path/to/file")
local s = f:read("*a")
f:close()
But I wonder if there's a shorter way.
I know that we can do (in Lua 5.2) the following:
local s = io.lines("/path/to/file", "*a")()
But the file would stay open for a while until the garbage collector kicks in (and gets rid of the closure io.lines returns; I believe this closure knows to explicitly close the file, but this could happen only after the second invocation of it, when it knows EOF has been reached).
So, is there a one-line solution I'm missing?

There is no such function in the standard library, but you can just define it yourself:
local function slurp(path)
local f = io.open(path)
local s = f:read("*a")
f:close()
return s
end
Alternatively there is such a function in Penlight.

Related

"function arguments expected near 'levelc'" when using LOVE

I'm currently trying to make a level loading system for a game.
function love.filedropped(file)
ofile=io.open(file:getFilename(),"r")
io.input(ofile)
file:close
levelc=io.read()
for i=1,levelc do
levels[i]=io.read()
print levels[i]
end
levelc should be the first line of the file, and file:getFilename is the file to open (path included) the project gives an error message on startup, and i've used a similar structure before, but for an output. The error is at line 30, which is the levelc=io.read().
I've tried changing the name of the file pointer (it was "f" before, now "ofile") and i've tried using io.read("*l") instead of io.read() but same result.
EDITS:
-this is a love.filedropped(file)
-i need to open other files from a .txt later and i don't really understand how do do that
The parameter given by love.filedropped is a DroppedFile.
In your case helpful could be File:lines().
For example:
function love.filedropped(file)
-- Open for reading
file:open("r")
-- Iterate over the lines
local i = 0
for line in file:lines() do
i = i + 1
levels[i] = line
print(i, levels[i]) -- Notice the parentheses missing in your code
end
-- Close the file
file:close()
end
Notice that love2d usually only allows reading/writing files within the save or working directory. Dropped files are an exception.
Unrelated to this answer but things I noticed in your code:
Use locals, oFile should be local
file:close() required parentheses as its a function call
Same for the print
The filedropped callback has no end
You mentioned reading other files too, to do so, you can either:
Use love.filesystem.newFile and a similar approach as before
The recommended one-liner love.filesystem.lines

How can I reload my Neovim config written in lua with a shortcut?

I want to reload my neovim configuration files with just a couple of keystrokes instead of having to restart the app. I was able to do this when using an init.vim with the following command:
nnoremap <leader>sv <cmd>source $MYVIMRC<CR>
$MYVIMRC points correctly to my config entry point.
The problem is that I switched to using lua, and now I can't do the same. I have read the docs and tried variants of the following without success:
util.nnoremap("<leader>sv", "<cmd>luafile $MYVIMRC<CR>")
Finally, I found a solution doing this:
function load(name)
local path = vim.fn.stdpath('config') .. '/lua/' .. name .. '.lua'
dofile(path)
end
load('plugins')
load('config/mapping')
load('lsp/init')
Which is buggy and feels wrong.
Is there any way to do this? I read the example in vimpeccable, but I want to see the other available options since I would rather not install another plugin.
I know that plenary includes a function to reload modules, but I don't understand how to use it. A complete example of that would be good too since I already use plenary in my config.
I am a new Neovim user, so I guess my solution may not work for some edge cases.
This function flushes the module of current buffer:
local cfg = vim.fn.stdpath('config')
Flush = function()
local s = vim.api.nvim_buf_get_name(0)
if string.match(s, '^' .. cfg .. '*') == nil then
return
end
s = string.sub(s, 6 + string.len(cfg), -5)
local val = string.gsub(s, '%/', '.')
package.loaded[val] = nil
end
You can call it whenever you write to a buffer with this autocommand:
autocmd BufWrite *.lua,*vim call v:lua.Flush()
This way, after you execute :source $MYVIMRC it will also reload changed Lua modules.

How to run a function without using any functions defined in the script in Lua

I am trying to loadstring a string and run it as a function. Here is my problem:
a = "hello"
loadstring("print(a)")()
I dont want the code above to use any vars/funcs outside of it. My goal is to make the script above print nil since a isnt defined inside the loadstring.
Sorry if the question is very brief. It was hard to explain my problem.
Your variable a lives in the global environment. To keep your chunk from seeing it, you need to give it a different environment. In Lua 5.1, you'd do that like this:
a = "hello"
local chunk = loadstring("print(a)")
setfenv(chunk, {print = print})
chunk()
In Lua 5.2 or newer, you'd do that like this:
a = "hello"
load("print(a)", nil, "t", {print = print})()
It's important to note that print isn't magic. It won't be in the new environment unless you put it there explicitly, like I did.

Reading a text file located on the computer with NodeMCU using Lua

My problem is about reading a text file (which is located in my computer) in the NodeMCU development kit. I am able to read the file content in Ubuntu terminal using a Lua script. Here I am sharing the code that I have been using for reading. Both are working pretty well in the Ubuntu terminal.
First one:
local open = io.open
local function read_file(path)
local file = open(path, "rb") -- r read mode and b binary mode
if not file then return nil end
local content = file:read "*a" -- *a or *all reads the whole file
file:close()
return content
Second one:
local fileContent = read_file("output.txt");
print (fileContent);
function file_exists(file)
local f = io.open(file, "rb")
if f then f:close() end
return f ~= nil
end
-- get all lines from a file, returns an empty
-- list/table if the file does not exist
function lines_from(file)
if not file_exists(file) then return {} end
lines = {}
for line in io.lines(file) do
lines[#lines + 1] = line
end
return lines
end
-- tests the functions above
local file = 'output.txt'
local lines = lines_from(file)
-- print all line numbers and their contents
for k,v in pairs(lines) do
print('line[' .. k .. ']', v)
end
My problem occurs when I have sent the code to NodeMCU, using Esplorer to send the code in. But the error occurs like this:
attempt to index global 'io' (a nil value)
stack traceback:
applicationhuff.lua:5: in function 'file_exists'
applicationhuff.lua:13: in function 'lines_from'
applicationhuff.lua:23: in main chunk
[C]: in function 'dofile'
stdin:1: in main chunk
My general purpose is actually to read this datas and publish it to a Mosquitto Broker via MQTT protocol. I am new to these topics. If anyone can handle my problem it will be appreciated. Thanks for your help...
NodeMCU does not have an io library. Therefore you get an error for indexing io, which is a nil value.
No offense, but sometimes I wonder how you guys actually manage to find your way to StackOverflow and even write some code without knowing how to do basic web research.
https://nodemcu.readthedocs.io/en/master/en/lua-developer-faq/
The firmware has replaced some standard Lua modules that don't align
well with the SDK structure with ESP8266-specific versions. For
example, the standard io and os libraries don't work, but have been
largely replaced by the NodeMCU node and file libraries.
https://nodemcu.readthedocs.io/en/master/en/modules/file/
The file module provides access to the file system and its individual
files.
I hope that's enough help...

Getting return status AND program output

I need to use Lua to run a binary program that may write something in its stdout and also returns a status code (also known as "exit status").
I searched the web and couldn't find something that does what I need. However I found out that in Lua:
os.execute() returns the status code
io.popen() returns a file handler that can be used to read process output
However I need both. Writing a wrapper function that runs both functions behind the scene is not an option because of process overhead and possibly changes in result on consecutive runs. I need to write a function like this:
function run(binpath)
...
return output,exitcode
end
Does anyone has an idea how this problem can be solved?
PS. the target system rung Linux.
With Lua 5.2 I can do the following and it works
-- This will open the file
local file = io.popen('dmesg')
-- This will read all of the output, as always
local output = file:read('*all')
-- This will get a table with some return stuff
-- rc[1] will be true, false or nil
-- rc[3] will be the signal
local rc = {file:close()}
I hope this helps!
I can't use Lua 5.2, I use this helper function.
function execute_command(command)
local tmpfile = '/tmp/lua_execute_tmp_file'
local exit = os.execute(command .. ' > ' .. tmpfile .. ' 2> ' .. tmpfile .. '.err')
local stdout_file = io.open(tmpfile)
local stdout = stdout_file:read("*all")
local stderr_file = io.open(tmpfile .. '.err')
local stderr = stderr_file:read("*all")
stdout_file:close()
stderr_file:close()
return exit, stdout, stderr
end
This is how I do it.
local process = io.popen('command; echo $?') -- echo return code of last run command
local lastline
for line in process:lines() do
lastline = line
end
print(lastline) -- the return code is the last line of output
If the last line has fixed length you can read it directly using file:seek("end", -offset), offset should be the length of the last line in bytes.
This functionality is provided in C by pclose.
Upon successful return, pclose() shall return the termination status
of the command language interpreter.
The interpreter returns the termination status of its child.
But Lua doesn't do this right (io.close always returns true). I haven't dug into these threads but some people are complaining about this brain damage.
http://lua-users.org/lists/lua-l/2004-05/msg00005.html
http://lua-users.org/lists/lua-l/2011-02/msg00387.html
If you're running this code on Win32 or in a POSIX environment, you could try this Lua extension: http://code.google.com/p/lua-ex-api/
Alternatively, you could write a small shell script (assuming bash or similar is available) that:
executes the correct executable, capturing the exit code into a shell variable,
prints a newline and terminal character/string onto standard out
prints the shell variables value (the exit code) onto standard out
Then, capture all the output of io.popen and parse backward.
Full disclosure: I'm not a Lua developer.
yes , your are right that os.execute() has returns and it's very simple if you understand how to run your command with and with out lua
you also may want to know how many variables it returns , and it might take a while , but i think you can try
local a, b, c, d, e=os.execute(-what ever your command is-)
for my example a is an first returned argument , b is the second returned argument , and etc.. i think i answered your question right, based off of what you are asking.

Resources