I wrote a basic functions to spawn and connect to a babashka repl using CIDER
(defun my-babashka-connect--process-filter (proc string)
"Run cider-connect once babashka nrepl server is ready."
(when (string-match "Started nREPL server at .+:\\([0-9]+\\)" string)
(cider-connect-clj (list :host "localhost" :port (match-string 1 string))))
;; Default behavior: write to process buffer
(internal-default-process-filter proc string))
(defun my-babashka-connect ()
"Start babashka on a random port."
(interactive)
(let ((port (+ 1230 (cl-random 300))))
(set-process-filter
(start-process "babashka"
"*babashka*"
"bb" "--nrepl-server" (number-to-string port))
'my-babashka-connect--process-filter)))
(defun my-babashka-quit ()
"Quit cider and kill babashka process."
(interactive)
(cider-quit)
(kill-process (get-process "babashka"))
(message "babashka is kill"))
I don't mind spawning a clojure repl outside of emacs, then connect to it from CIDER, but since babashka is for more short-lived scripts I find it useful not having to go to a terminal, run babashka, connect to ciderWow nice! I think that could be documented here: https://github.com/borkdude/babashka/blob/master/doc/repl.md
I think the "Started nREPL server at ..." should go to stderr. Would --process-filter still work in that case?
Yes, when you start a process on emacs, by default stdout and stderr are mixed together. I'll do a PR for repl.md, although I'll also look into contributing to CIDER to be able to jack-in directly into babashka, which would rend this obsolete.
:thumbsup:
Anyone know a logging lib that works with babashka?
@schpaencoder Probably not the answer you're looking for, but I usually use prn
, println
and *err*
if you want to print to stdout/stderr
Welcome btw!
@schpaencoder I use a function I add to every bb script. YMMV but it serves my needs. If I need to log contents of vars, maps, etc I use a format string.
#!/usr/bin/env bb
(ns app)
(defn logit [msg]
(spit "app.log" (str msg "\n") :append true))
(logit (format "%s %d" "log message" 5))
I also tend to write such functions, usually & msgs
for printing multiple strings
@michael819 Btw (spit "app.log" ... :append true)
does the same thing
@borkdude That does make for a short function, I updated it. I knew about slurp but didn't know about spit. Thanks.
Yes, this is what I do now, I guess it is good enough
Thank you!
Hah, that function is in the examples for spit, guess I wasn't so original.