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:29Here's a nice collection of Lua code snippets and functions:
< http://lua-users.org/wiki/SampleCode >
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 io.open worked:
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
-- 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)
Posted by roberts on Feb. 09 2007,04:55You 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:39I 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 lua-users.org. 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 languages...it has only added a second layer of translation that I have to deal with)
Posted by mikshaw on Feb. 10 2007,06:18Any idea how something like this might be translated to work in murgaLua?
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:31Apparently 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:39Can somebody tell me why this doesn't work?
Posted by mikshaw on Feb. 12 2007,03:55You just need to add "Fl:run()" to the end of the script
Posted by u2musicmike on Feb. 13 2007,15:31Well 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:16Shadow 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.