Fork me on GitHub

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."
  (let ((port (+ 1230 (cl-random 300))))
     (start-process "babashka"
		    "bb" "--nrepl-server" (number-to-string port))

(defun my-babashka-quit ()
  "Quit cider and kill babashka process."
  (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 cider

😎 12

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, although I'll also look into contributing to CIDER to be able to jack-in directly into babashka, which would rend this obsolete.


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

👍 3

Welcome btw!


Thank you!

Michael W20:10:38

@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))

👍 6

Yes, this is what I do now, I guess it is good enough


I also tend to write such functions, usually & msgs for printing multiple strings


@michael819 Btw (spit "app.log" ... :append true) does the same thing

Michael W20:10:25

@borkdude That does make for a short function, I updated it. I knew about slurp but didn't know about spit. Thanks.

Michael W20:10:42

Hah, that function is in the examples for spit, guess I wasn't so original.