2 -- Copyrigh (c) 2007, Bart Trojanowski <bart@jukie.net>
4 -- Simple wmiir like interface.
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.
10 -- git://www.jukie.net/wmiirc-lua.git/
13 local io
= require("io")
14 local os
= require("os")
15 local posix
= require("posix")
16 local string = require("string")
24 -- ------------------------------------------------------------------------
25 -- TODO I would like to be able to return an interator
26 function ls (dir
, fmt
)
27 local tmpfile
= os
.tmpname()
30 os
.execute (wmiir
.. " ls " .. fmt
.. " " .. dir
.. " > " .. tmpfile
)
32 local fh
= io
.open (tmpfile
, "rb")
35 local data
= fh
:read("*a") -- read everything
42 -- ------------------------------------------------------------------------
43 -- read all contents of a wmii virtual file
45 local tmpfile
= os
.tmpname()
47 os
.execute (wmiir
.. " read " .. file
.. " > " .. tmpfile
)
49 local fh
= io
.open (tmpfile
, "rb")
52 local data
= fh
:read("*a") -- read all
58 -- ------------------------------------------------------------------------
59 -- return an iterator which walks all the lines in the file
62 -- for event in wmii.iread("/event")
66 local tmpfile
= os
.tmpname()
69 io
.write ("-- tmpname " .. tmpfile
.. "\n")
71 local rc
= posix
.mkfifo (tmpfile
)
72 io
.write ("-- mkfifo " .. rc
.. "\n")
76 io
.write ("-- fork failed " .. rc
.. "\n")
81 if rc
== 0 then -- child
82 os
.execute (wmiir
.. " read " .. file
.. " > " .. tmpfile
)
83 posix
.exec ("/usr/bin/env", "cat", "/dev/null")
88 local fh
= io
.open (tmpfile
, "rb")
92 local line
= fh
:read("*l") -- read a line
94 io
.write ("-- closing " .. file
.. "\n")
101 -- ------------------------------------------------------------------------
102 -- returns an events iterator
104 local it
= iread("/event")
108 return string.match(line
, "(%S+)%s(.+)")
112 -- ------------------------------------------------------------------------
113 -- write a value to a wmii virtual file system
114 function write (file
, value
)
115 local tmpfile
= os
.tmpname()
117 local fh
= io
.open (tmpfile
, "wb")
121 os
.execute (wmiir
.. " write " .. file
.. " < " .. tmpfile
)
125 -- ------------------------------------------------------------------------
126 -- write a value to a wmii virtual file system
127 function configure (config
)
129 for x
, y
in pairs(config
) do
130 write ("/ctl", x
.. " " .. y
)
134 -- ------------------------------------------------------------------------
135 -- displays the menu given an table of entires, returns selected text
138 local infile
= os
.tmpname()
139 local fh
= io
.open (infile
, "w+")
141 for n
in pairs(tbl
) do
149 local outfile
= os
.tmpname()
151 os
.execute ("dmenu < " .. infile
.. " > " .. outfile
)
153 fh
= io
.open (outfile
, "r")
156 local sel
= fh
:read("*l")
162 -- ------------------------------------------------------------------------
163 -- displays the a tag selection menu, returns selected tag
165 local tmpfile
= os
.tmpname()
167 os
.execute ("wmiir ls /tag | sed 's|/||; /^sel$/d' | dmenu > " .. tmpfile
)
169 local fh
= io
.open (tmpfile
, "rb")
172 local tag = fh
:read("*l")
178 -- ------------------------------------------------------------------------
179 -- displays the a program menu, returns selected program
181 local tmpfile
= os
.tmpname()
183 os
.execute ("dmenu_path | dmenu > " .. tmpfile
)
185 local fh
= io
.open (tmpfile
, "rb")
188 local prog
= fh
:read("*l")