extract non-blocking unix file input into iostream module

phoen
Daniel Barlow 2022-03-27 18:42:44 +01:00
parent 4c63c0338d
commit 3c8051a84e
3 changed files with 20 additions and 9 deletions

View File

@ -1,9 +1,7 @@
(local {: bar : indicator : stylesheet : run} (require :blinkenlicht))
(local {: view} (require :fennel))
(local posix (require :posix))
(local fcntl (require :posix.fcntl))
(local iostream (require :iostream))
(local metric (require :metric))
(stylesheet "licht.css")
@ -42,10 +40,7 @@
{:text v})
}))
(let [f (io.open "/tmp/statuspipe" :r)]
(fcntl.fcntl (posix.stdio.fileno f)
fcntl.F_SETFL fcntl.O_NONBLOCK)
(let [f (iostream.open "/tmp/statuspipe" :r)]
(indicator {
;; this is a guide to tell blinkenlicht when it might
;; be worth calling your `content` function. Your
@ -59,7 +54,7 @@
;; { :icon "face-sad" } - render icon from theme or pathname
;; { :classes ["foo" "bar"] - add CSS classes to widget
:refresh
#(let [l (posix.unistd.read (posix.stdio.fileno f) 1024)]
#(let [l (f:read 1024)]
(if l
{:text l}))
}))

View File

@ -131,7 +131,7 @@
(window:add box)))
(fn gsource-for-file-input [file cb]
(let [fd (posix.stdio.fileno file)]
(let [fd file.fileno]
(doto (GLib.unix_fd_source_new fd GLib.IOCondition.IN)
(: :set_callback cb))))

16
blinkenlicht/iostream.fnl Normal file
View File

@ -0,0 +1,16 @@
(local posix (require :posix))
(local fcntl (require :posix.fcntl))
(fn open [name direction]
(let [flags (match direction
:r posix.O_RDONLY
:w (+ posix.O_WRONLY posix.O_CREAT))
fd (posix.open name flags)]
(fcntl.fcntl fd fcntl.F_SETFL fcntl.O_NONBLOCK)
{
:read #(posix.unistd.read fd $2) ;XXX needs to check for eof
:close #(posix.unistd.close fd)
:fileno fd
}))
{: open }