babashka

https://github.com/babashka/babashka. Also see #sci, #nbb and #babashka-circleci-builds .
solf 2020-10-26T09:04:56.092100Z

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

4😎
borkdude 2020-10-26T09:25:27.092200Z

Wow nice! I think that could be documented here: https://github.com/borkdude/babashka/blob/master/doc/repl.md

borkdude 2020-10-26T09:29:30.092600Z

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

solf 2020-10-26T09:37:42.092800Z

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.

borkdude 2020-10-26T09:38:11.093Z

:thumbsup:

Schpaa 2020-10-26T19:40:12.094300Z

Anyone know a logging lib that works with babashka?

borkdude 2020-10-26T19:41:04.094800Z

@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

1👍
borkdude 2020-10-26T19:41:48.095100Z

Welcome btw!

Michael W 2020-10-26T20:01:38.098600Z

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

2👍
borkdude 2020-10-26T20:04:22.099200Z

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

borkdude 2020-10-26T20:09:19.100100Z

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

Michael W 2020-10-26T20:29:25.101200Z

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

Schpaa 2020-10-26T20:38:35.101700Z

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

Schpaa 2020-10-26T20:39:08.101900Z

Thank you!

Michael W 2020-10-26T20:42:42.102700Z

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