Page 1 of 2
Run Lua code from the command line
Posted: Fri May 15, 2009 12:35 pm
by Vadi
Small alias to execute lua code from the input line - import in, and do lua echo ("hi") for example. Can also display things with lua display(mytable) or lua mytable.
Note: this script is included by default with Mudlet now, you can use it as-is without installing!
Re: Execute Lua code from the input line
Posted: Tue Jul 14, 2009 12:14 am
by eraldo
Nice...
Thank you for this great piece of code!
Re: Run Lua code from the command line
Posted: Wed Jun 09, 2010 6:16 pm
by Vadi
Alias improved, now auto-displays if the function returned any results.
Re: Run Lua code from the command line
Posted: Thu Jun 10, 2010 5:09 pm
by Vadi
Fixed v2.
Re: Run Lua code from the command line
Posted: Mon May 30, 2011 6:17 pm
by Vadi
Updated to v4 - if a function returns 'false' as a result, it'll be displayed properly.
Re: Run Lua code from the command line
Posted: Sat Sep 10, 2011 7:58 pm
by Phoenix
I use the following code - it's basically the same, except it allows multiple returns. IE, if you did
lua getMainWindowSize(), this would return both the first and second values.
local f,e = loadstring("return "..matches[2])
if not f then
f,e = assert(loadstring(matches[2])) end
local a,b,c = f()
if a ~= nil then echo ("A: ") display(a,nil,1) end
if b ~= nil then echo ("B: ") display(b,nil,1) end
if c ~= nil then echo ("C: ") display(c,nil,1) end
if e ~= nil then echo ("Er: ") display(e,nil,1) end
The reason for the exotic use of display is to tell it that it's already started... it's not at '0'. This allows it to display the first line on the same line as the A: or B: etc.
lua getMainWindowSize()
A: 1280
B: 633
Re: Run Lua code from the command line
Posted: Thu Feb 07, 2013 6:52 pm
by Golem
Don't know if this will be of any use, but a more universal solution would be something like this:
local f,e = loadstring("return "..matches[2])
if not f then
f,e = assert(loadstring(matches[2]))
end
local pack = function(...)
return arg
end
local r = pack(f())
if #r == 1 then
display(r[1])
elseif #r > 1 then
echo("Multiple returns: ")
display(r)
end
Re: Run Lua code from the command line
Posted: Fri Feb 08, 2013 7:54 pm
by Phoenix
Interesting - what is the pack() function doing with f()? Would not r = {f()} be more simple? I've long since modified my lua code alias to do that, just never updated this post...
Re: Run Lua code from the command line
Posted: Fri Feb 08, 2013 7:58 pm
by Golem
Truth be told, it probably is. pack() was just a mental exercise in creating an inverse function to the unpack() function.
Re: Run Lua code from the command line
Posted: Fri Feb 08, 2013 8:12 pm
by Phoenix
... I see. pack() is interesting, looks like it basically just wraps {} around the arguments, and throws in a key of 'n' equal to the number of arguments. I wonder how it compares speedwise to r = {f()}...