Lua functions and other bits

Forum: Programming and Scripting
Topic: Lua functions and other bits
started by: mikshaw

Posted by mikshaw on Feb. 09 2007,04:29
Here's a nice collection of Lua code snippets and functions:
< >

Some work as-is in murgaLua, and some need a little modification. For example, the length_of_file function on the "File Input Output" page uses assert, which crashes murgaLua if the file doesn't exist instead of just giving an error message. I used "if" instead to check if worked:
Code Sample
function length_of_file(filename)
 local fh =, "rb")
 if fh then
 local len = fh:seek("end")
 return len end

The original function was written by Jay Carlson, the creator of Lua-FLTK

If anyone else knows of a good resource for Lua samples, please paste a link here. thanks.

Here are a couple I did yesterday while working on another project (murgaLua only):

-- Returns a hex value from the given widget's color.
-- In the case of redthing:color(fltk.FL_RED)
-- hexcolor(redthing) returns #FF0000
Code Sample
function hexcolor(widget)
       local r,g,b=Fl:get_color(widget:color(),r,g,b);
       local hex=string.format("#%.2X%.2X%.2X",r,g,b);
       return hex

-- An alternative to the above, which uses a color index (0-255) as
-- an argument. You can plug in any FLTK color rather than rely on
-- a specific widget's base color. Numbers above 255 will work, but
-- I couldn't tell you what rgb values they represent =o)
Code Sample
function hexcolor2(num)
       local r,g,b=Fl:get_color(num,r,g,b);
       local hex=string.format("#%.2X%.2X%.2X",r,g,b);
       return hex

Posted by roberts on Feb. 09 2007,04:55
You can see my collection of generic Lua functions that I have been building as I write Lua scripts for DSL in /etc/init.d/functions5.lua

I also have created many generic non-GUI Lua scripts in DSL. I lost count. I usually factor out code and I place it in my functions5 collection.

I think DSL has more use of LuaFltk and generic stand-a-lone Lua scripts than most any other place.

I still write Lua4 as well Lua5 and even have Lua on my FreeDOS diskette.

Lua is a small, very fast, and a very nice language.

I know, you probably can't find many jobs as a Lua script writer and most sites use Lua together with C.

I prefer John Murga's bindings as they are much closer to the standards of Lua and Fltk.  The prior version, Jay Carlson's Fltk binding seemed to have a tcl-like parameter passing scheme.

Anyway, it is a nice language. You can find good documentation for Lua5.1 and most of the Fltk Manual is also helpfful for the GUI side.

I think DSL has nearly 60 Lua programs.

Honestly, I would rather code Lua than the other tasks that I do for DSL.

Posted by mikshaw on Feb. 09 2007,14:39
I knew of /etc/init.d/functions.lua, but I didn't even think to look at functions5.lua...sometimes I get it into my head that a particular file is probably just a backup or test version that the author forgot to delete. There are so many of them floating around in various archives that i usually don't bother looking in them unless i'm having trouble with the archive.  Anyway, yeah, that's something i want to study.

I also am reading through the tutorial pages at They're not really tutorials by my definition, and most of them cover the same material as the Lua manual, but they use real  examples rather than that friggin annoying BNF syntax. So the tutorials are a definite plus.
(no, I don't believe BNF has ever helped me to translate between has only added a second layer of translation that I have to deal with)

Posted by mikshaw on Feb. 10 2007,06:18
Any idea how something like this might be translated to work in murgaLua?
Code Sample
-- Perform a shell command and return its output
function shell(c)
 local o, h
 h = assert(io.popen(c,"r"))
 o = h:read("*all")
 return o

It works in Lua 5.1, but apparently popen is not implemented in murgaLua.
Until now I just assumed that shell command output needed to be redirected to a file first and then read into Lua.  I hope it's not the case with Murga.

Posted by roberts on Feb. 10 2007,13:31
Apparently popen was not compiled into murgaLua from Lua source.
Interestingly, it is also the case for Carlson's Lua4 bindings as well.
That is why I wrote my own capturing function. But like you stated it uses temp files.

Posted by u2musicmike on Feb. 12 2007,03:39
Can somebody tell me why this doesn't work?

Code Sample

-- hello.cxx from the FLTK tutorial in Lua.

-- Create a window

w = fltk:Fl_Window(300,180,"Hello")

-- and a box with the "Hello, World!" string in it:

box = fltk:Fl_Box(20,40,260,100,"Hello, World!")

-- Next, we set the type of box and the size, font, and style of the label:


-- Of course, that's the C++ way.  The Lua way:

-- box = Box{20,40,260,100,"Hello, World!";
--    box=Boxtype.up, labelsize=36, labelfont=Font.bold+Font.italic,
--   labeltype=Labeltype.shadow}

-- Finally, we show the window and enter the FLTK event loop:

-- w:end_layout() -- ends the layout of the window

-- The resulting program will display the window below. You can quit
-- the program by closing the window or pressing the ESCape key.

Posted by mikshaw on Feb. 12 2007,03:55
You just need to add "Fl:run()" to the end of the script
Posted by u2musicmike on Feb. 13 2007,15:31
Well I was beating my head on the wall over that one.  I just didn't notice the Fl:run was missing and the strange thing was no error message.  I played around with the Hello World program some more and was not able to get the shadow text to work.  I commented out labelfont and labeltype and it didn't make a difference.
Posted by mikshaw on Feb. 13 2007,18:16
Shadow text is not currently available in murgaLua (unless it's just not available in the non-Xft version).

There was no error because there was nothing wrong with your script. The Lua portions of the script still run, but since no Fl:run was included the FLTK portions were not displayed.

Powered by Ikonboard 3.1.2a
Ikonboard © 2001 Jarvis Entertainment Group, Inc.