Playing around with Lua


I’ve really liked used the fui program, which I mentioned in a previous entry. I would like to try improving it somehow, though I can’t think of what it needs. It’s written in Python, which isn’t too difficult to understand I suppose, but I am a bit lazy… or can’t be bothered, to read and try to understand everything.

So what I have been trying to do is to rewrite it in Lua. I like Lua a lot, moreso than Python, and I enjoy learning new programming skills. It really is such a basic app that I don’t think it needs a lot of the features Python has, so I hope that I can get Lua to achieve the same thing with perhaps an improvement in speed. (Yes, that is a very noobish obsession with speed right there.)

For those that don’t know what fui is or haven’t clicked on the link, a brief description: it’s a command line application that saves a list of files or directory to a temporary file, that copies or moves those file/directories when you next invoke it with fui –copy or fui –move. Nothing particularly amazing there, it just allows you to avoid doing cp /nameoffile /destination which I find awkward sometimes. It doesn’t even define its own method of copying or moving, but rather it hands the hard work off to cp and mv through os.system. So, most of this script is concerning with writing to, retrieving information from, and deleting a temporary file.

Lua’s significantly leaner set of libraries is what makes this somewhat more challenging than a simple tweaking of syntax. In particular, Lua’s handling of temporary files is less comprehensive than Python’s. Python allows one to obtain the name of the directory to put temporary files in (typically “/tmp”) which makes it easy to generate a temporary file in that directory with whatever name you wish.

Unfortunately Lua is less flexible. It has two functions that make a temporary file: io.tmpfile() and os.tmpname(). The former creates a temporary file which is immediately destroyed when the script ends, which is undesirable since it means the selection can’t be saved. The latter returns a string with the name of a temporary file that is not removed when the script ends, but the name is random and not fixed. You can’t refer to the same name when you invoke the script repeatedly; it just generates new temporary files and thus the selection can’t be accessed after you define it.

I don’t think there is an elegant solution that exists. What I have been trying to do is to create a temporary file through os.tmpname() and separate out the name of the temporary file from the rest of its path, ie. obtaining the “/tmp” string. Now of course one must be thinking, why don’t you just write “/tmp” directly into the script? As tempting as this is, I want to write the script in such a way that it would work even if you were not storing your temporary files in /tmp. Now isn’t that crazy?! Yes, yes indeed I am crazy, I am completely off the wall, wasting my time, yadda yadda yadda….

The entire purpose of this is to basically replicate the Python function tempfile.gettempdir() in Lua and by doing so enable the script to create a temporary file with a predefined name so that it will always have access to it.

This led to me fiddling around with the temporary file string. I have other work to do, so I’ll just report what I ended up getting:

#!/usr/bin/env lua

test = os.tmpname()

print(string.sub(test, 0, string.find(test, "/", 2)))

for words in string.gmatch(test, "/[_%a]*") do print(words) end

The first line defines the path of the temporary file and creates it. The second line is a feeble attempt to extract the path of the temporary directory. The third line is a possibly more feeble attempt at iterating over the the string with string.gmatch to extract everything that fits the “/[_%a]” criteria (must start with /, and have underscores and/or letters in the string).

Oh well. Progress is progress. I have learnt something!

%d bloggers like this: