I’m confused I’m having such a hard time finding this info but how do I get stdin in an nbb script?
i tried to use stdin in nodejs once. it is a mess.
(js/process.stdin.on "data" (fn [data] (println "From stdin: " (str data))))
You can put that in a .cljs file and call
echo ‘something’ | nbb myfile.cljs
Basically, process.stdin gives you a Stream object and you can attach callbacks for certain events on that.
Thank you! That API feels weird but I guess we’ll go with it 😁
Yep, that’s typical node weirdness. 🙂 No blocking is good so it’s callbacks or sugar around it like promises
I think there’s a promises based version of the stream api
Uhhhmm kinda yes. At least there’s pipeline function that can construct streaming pipelines. Here’s a stupid example that just pipes stuff from stdin to stdout. 😄
(ns demo
(:require ["stream/promises" :as sp]))
(sp/pipeline js/process.stdin js/process.stdout)But you could add there transform steps and whatnot
Kinda weird, kinda cool.
Heh, yeah you can do this kind of things
(ns demo
(:require ["stream/promises" :as sp]
["stream" :as s]))
(def upcase
(s/Transform. #js{:transform (fn [chunk enc cb]
(cb nil (clojure.string/upper-case (str chunk))))}))
(sp/pipeline js/process.stdin upcase js/process.stdout)And now I can upcase enormous amounts of text with very low memory footprint.
Would it be possible to use zprint within nbb?
zprint does currently work with babashka, bit I expect there to be some issues with nbb compatibility since nbb doesn't have rewrite-clj built-in
Thanks! Use case is a build script that fetches data, formats it as a clojure file str, and was hoping to use zprint api to format it as a file and write it to the fs. Worst case can use it from an npm wrapper after the build script https://www.npmjs.com/package/zprint-wrapper
Didn't realize you were on vacation, sorry for the interruption!
Oh not at all, it would be ridiculous if people couldn't ask questions here if I were on vacation. It's up to me to answer or not ;)
hey, aren’t you on vacation?
fwiw, zprint does carry out some tests for self-hosted clojurescript under planck.