Run Lua code from the command line

Run Lua code from the command line

Postby Vadi » Fri May 15, 2009 12:35 pm

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.
Attachments
run-lua-code-v4.xml
(548 Bytes) Downloaded 1191 times
User avatar
Vadi
 
Posts: 3814
Joined: Sat Mar 14, 2009 3:13 pm

Re: Execute Lua code from the input line

Postby eraldo » Tue Jul 14, 2009 12:14 am

Nice...
Thank you for this great piece of code!
eraldo
 
Posts: 43
Joined: Sun Jul 12, 2009 1:25 am

Re: Run Lua code from the command line

Postby Vadi » Wed Jun 09, 2010 6:16 pm

Alias improved, now auto-displays if the function returned any results.
User avatar
Vadi
 
Posts: 3814
Joined: Sat Mar 14, 2009 3:13 pm

Re: Run Lua code from the command line

Postby Vadi » Thu Jun 10, 2010 5:09 pm

Fixed v2.
User avatar
Vadi
 
Posts: 3814
Joined: Sat Mar 14, 2009 3:13 pm

Re: Run Lua code from the command line

Postby Vadi » Mon May 30, 2011 6:17 pm

Updated to v4 - if a function returns 'false' as a result, it'll be displayed properly.
User avatar
Vadi
 
Posts: 3814
Joined: Sat Mar 14, 2009 3:13 pm

Re: Run Lua code from the command line

Postby Phoenix » Sat Sep 10, 2011 7:58 pm

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.

Syntax: [ Download ] [ Hide ]
Using lua Syntax Highlighting
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
Phoenix
 
Posts: 92
Joined: Tue Feb 15, 2011 3:23 am

Re: Run Lua code from the command line

Postby Golem » Thu Feb 07, 2013 6:52 pm

Don't know if this will be of any use, but a more universal solution would be something like this:

Syntax: [ Download ] [ Hide ]
Using lua Syntax Highlighting
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
Golem
 
Posts: 30
Joined: Thu Feb 07, 2013 6:46 pm

Re: Run Lua code from the command line

Postby Phoenix » Fri Feb 08, 2013 7:54 pm

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...
Phoenix
 
Posts: 92
Joined: Tue Feb 15, 2011 3:23 am

Re: Run Lua code from the command line

Postby Golem » Fri Feb 08, 2013 7:58 pm

Truth be told, it probably is. pack() was just a mental exercise in creating an inverse function to the unpack() function. :-)
Golem
 
Posts: 30
Joined: Thu Feb 07, 2013 6:46 pm

Re: Run Lua code from the command line

Postby Phoenix » Fri Feb 08, 2013 8:12 pm

... 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()}...
Phoenix
 
Posts: 92
Joined: Tue Feb 15, 2011 3:23 am

Next

Return to Howtos, FAQs and Tips & Tricks

Who is online

Users browsing this forum: No registered users and 1 guest