Sublet fed with external program output ?
Added by david unric over 13 years ago
Hi,
just curious how to update contents of a sublet not periodically but if there is some new output to stdout of a subprocess only ?
I'd need to display current X-keyboard layout group and I'm using external program called xkb-switch.
I did tried to write following sublet, but it does update its contents after a veeeryy looong time:
# X-keyboard sublet file configure :xkb do |s| # {{{ s.interval = 0 Thread.new do s.data = `xkb-switch`.strip.upcase io = IO.popen 'xkb-switch -W' while (s.data = io.readline.strip.upcase) do s.render end io.close end end # }}} on :run do |s| # {{{ end # }}}
If the thread code block is run separately it immediately get its contents of the piped subprocess.
Replies (3)
RE: Sublet fed with external program output ? - Added by Christoph Kappel over 13 years ago
subtle is single threaded and therefore this won't work like you expect, Ruby has some nasty limitations regarding access to one vm from multiple threads and generally threaded is a double-edged sword. In most cases it makes things more complicated and creates unique problems and I tend to avoid it when possible.
Sublets can be updated my three different events:
- Interval
- Data on a socket/file via inotify
- From external with e.g. subtler
So the second way could work for you like:
{{hide}}#Sublet
configure :switcher
s.filename = "/path/to/switcher.log"
s.watch s.filename
end
on :watch do |s|
s.data = IO.readlines(s.filename).first
end
# Process e.g. in .xinitrc
xdg-switch -W > /path/to/switcher.log
RE: Sublet fed with external program output ? - Added by david unric over 13 years ago
Thank you for the quick response, sublet is now properly updated. I'd add you need to call Array#last method instead of first in the watch handler.
However I'm a bit worried about the way to read a whole log file on every inotify event. Imagine a long running session when a log file gets really big.
RE: Sublet fed with external program output ? - Added by Christoph Kappel over 13 years ago
Yw, thought about that for a while and I have no idea to make that easier without using threads and that is difficult.