Run Lua code from the command line

User avatar
Vadi
Posts: 5035
Joined: Sat Mar 14, 2009 3:13 pm

Run Lua code from the command line

Post 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!
Attachments
run-lua-code-v4.xml
(548 Bytes) Downloaded 3459 times

eraldo
Posts: 43
Joined: Sun Jul 12, 2009 1:25 am

Re: Execute Lua code from the input line

Post by eraldo »

Nice...
Thank you for this great piece of code!

User avatar
Vadi
Posts: 5035
Joined: Sat Mar 14, 2009 3:13 pm

Re: Run Lua code from the command line

Post by Vadi »

Alias improved, now auto-displays if the function returned any results.

User avatar
Vadi
Posts: 5035
Joined: Sat Mar 14, 2009 3:13 pm

Re: Run Lua code from the command line

Post by Vadi »

Fixed v2.

User avatar
Vadi
Posts: 5035
Joined: Sat Mar 14, 2009 3:13 pm

Re: Run Lua code from the command line

Post by Vadi »

Updated to v4 - if a function returns 'false' as a result, it'll be displayed properly.

Phoenix
Posts: 92
Joined: Tue Feb 15, 2011 3:23 am

Re: Run Lua code from the command line

Post 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.
Code: [show] | [select all] lua
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

Golem
Posts: 30
Joined: Thu Feb 07, 2013 6:46 pm

Re: Run Lua code from the command line

Post by Golem »

Don't know if this will be of any use, but a more universal solution would be something like this:
Code: [show] | [select all] lua
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

Phoenix
Posts: 92
Joined: Tue Feb 15, 2011 3:23 am

Re: Run Lua code from the command line

Post 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...

Golem
Posts: 30
Joined: Thu Feb 07, 2013 6:46 pm

Re: Run Lua code from the command line

Post by Golem »

Truth be told, it probably is. pack() was just a mental exercise in creating an inverse function to the unpack() function. :-)

Phoenix
Posts: 92
Joined: Tue Feb 15, 2011 3:23 am

Re: Run Lua code from the command line

Post 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()}...

Post Reply