Fork me on GitHub
#babashka
<
2020-10-26
>
solf09:10:56

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 cider

😎 12
borkdude09:10:30

I think the "Started nREPL server at ..." should go to stderr. Would --process-filter still work in that case?

solf09:10:42

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.

Schpaa19:10:12

Anyone know a logging lib that works with babashka?

borkdude19:10:04

@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
borkdude19:10:48

Welcome btw!

Schpaa20:10:08

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
Schpaa20:10:35

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

borkdude20:10:22

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

borkdude20:10:19

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