initial commit
[wmiirc-lua.git] / wmii.lua
blob618f1e92089b2cc87214f8841701f038538e8774
1 --
2 -- Copyrigh (c) 2007, Bart Trojanowski <bart@jukie.net>
3 --
4 -- Simple wmiir like interface.
5 --
6 -- The current intent is to wrap around the wmiir executable.
7 -- This is just a proof of concept, and eventually this will
8 -- be rewritten in C to use libixp.
9 --
10 local base = _G
11 local io = require("io")
12 local os = require("os")
14 module("wmii")
16 -- TODO I would like to be able to return an interator
17 function ls (dir, fmt)
18 local tmpfile = os.tmpname()
19 local fmt = fmt or ""
21 os.execute ("wmiir ls " .. fmt .. " " .. dir .. " > " .. tmpfile)
23 local fh = io.open (tmpfile, "rb")
24 os.remove (tmpfile)
26 local data = fh:read("*a") -- read everything
28 io.close (fh)
30 return data
31 end
33 -- TODO I would like to return a line iterator
34 function read (file)
35 local tmpfile = os.tmpname()
37 os.execute ("wmiir read " .. file .. " > " .. tmpfile)
39 local fh = io.open (tmpfile, "rb")
40 os.remove (tmpfile)
42 return function ()
43 local line = fh:read("*l") -- read a line
44 if not line then
45 io.write ("-- closing " .. file .. "\n")
46 io.close (fh)
47 end
48 return line
49 end
50 end
52 -- write a value to a wmii virtual file system
53 function write (file, value)
54 os.execute ("echo -n '" .. value .. "' | wmiir write " .. file)
55 end