Fork me on GitHub
#planck
<
2017-11-28
>
cap10morgan20:11:11

Anyone know how I might implement a file tailer in cljs+planck?

cap10morgan20:11:05

I had something close to working by re-opening a reader each time through a loop, but that just re-prints the whole file again rather than what's new.

cap10morgan20:11:32

(so not that close to working, haha)

mfikes21:11:10

@cap10morgan Hrm. This works in Clojure

(let [r (io/reader "/tmp/foo.txt")]
(loop [line (.readLine r)]
  (if (nil? line)
    (Thread/sleep 1000)
    (println line))
   (recur (.readLine r))))
but the translation to Planck doesn't pan out. I'm logging a ticket. I think the subtle difference might be that once a reader in Planck returns nil things are closed and it will always do so. But arguably it should behave like Clojure and return nil only when there is no line or somesuch (I haven't dug into the details)

cap10morgan21:11:33

@mfikes Interesting. I got something hacky working by keeping track of line counts in an atom and skipping that many the next time through the loop. But I'll happily keep an eye on that ticket to revert to something saner in the future. 🙂

cap10morgan21:11:09

I'm doing this in the first place as a hacky workaround for https://github.com/mfikes/planck/issues/54. I redirect out and err of the command to a couple of tempfiles and run it from sh-async, then I tail those files to *out* and *err* until my sh-async callback gets called.

cap10morgan21:11:23

kinda gross but it works (so far)

mfikes21:11:27

@cap10morgan Yep, I also thought of issue 54 right away as well 🙂

mfikes21:11:35

I've also logged a ticket for the ability to sleep https://github.com/mfikes/planck/issues/558

cap10morgan21:11:26

ah cool. I've been doing (planck.shell/sh "sleep" "1") 😁

mfikes21:11:46

Yep, that was also my reaction 🙂 I see no problem with that being directly in planck.core

mfikes21:11:20

(That way you can have more fine-grained control on how long to sleep

cap10morgan21:11:44

I have to say, overall, having converted a unwieldy bash script to cljs+planck, I am loving it. Kudos!

mfikes21:11:15

Sweet! And if you ever need to convert to Clojure, if you use aliases for the planck namespaces, it is nice that the API is very similar to Clojure.

mfikes22:11:07

I forgot I had also written this off in a side branch. I vaguely recall it being able to block. Hrm. :thinking_face: https://github.com/mfikes/planck/commit/f1b8547bf2a42bc06d04e81d52048555ba223c60