Fork me on GitHub
#babashka
<
2021-09-29
>
Carl10:09:27

I'm fond of the debux library for clj/cljs. Is there a pod/lib that provides similar functionality for debugging babashka?

borkdude10:09:36

@carl.kamholtz There are a few issues with this lib: it's loading the clojurescript analyzer which can't run in bb. Also it's loading clojure.spec.alpha but this may be alleviated with spartan.spec which is a drop-in replacement for the most common parts of spec.

borkdude10:09:51

This may serve as a basic alternative though: https://github.com/technomancy/limit-break

borkdude10:09:30

You can open a REPL at any point in your program and then inspect the locals, and exit the REPL when done to continue program execution

Carl10:09:37

Interesting! Thanks for the quick reply 😊

borkdude10:09:45

@carl.kamholtz Wow, it seems I've got it working:

$ bb  "(require 'spartan.spec) (use 'debux.core) (* 2 (dbg (+ 10 20)))"

{:ns user, :line 1}
dbg: (+ 10 20) =>
|   30
60

❤️ 1
borkdude11:09:29

The -> examples don't show the output that the README shows, just one output

borkdude11:09:26

let me just push what I've got

borkdude11:09:01

@carl.kamholtz Add a bb.edn like this: https://github.com/borkdude/debux/blob/babashka/bb.edn + that repo as a git dep And then first require (require 'spartan.spec)

borkdude11:09:11

And then it sort of worked for me

borkdude11:09:54

if you just want the kind of simple dbg macro you can also write your own:

user=> (defmacro dbg [& forms] `(do (apply prn (rest '~&form)) (let [res# (do [email protected])] (prn (symbol "=>") res#) res#)))
#'user/dbg
user=> (dbg (+ 1 2 3))
(+ 1 2 3)
=> 6
6

❤️ 1
borkdude11:09:16

user=> (* 2 (dbg (+ 1 2 3)))
(+ 1 2 3)
=> 6
12

borkdude11:09:05

I'm slowly adopting a new style of arg parsing, currently in neil but might expand it to other CLIs:

(defn parse-opts [opts]
  (let [[cmds opts] (split-with #(not (str/starts-with? % ":")) opts)]
    (into {:cmds cmds}
          (for [[arg-name arg-val] (partition 2 opts)]
            [(keyword (subs arg-name 1)) arg-val]))))

🙌 1
john03:09:23

I created a similar arg passing style recently for higher order hiccup components

john03:09:59

Key value pairs can be placed anywhere in the args list and they get assoc'ed onto an options map... [1 3 :x 'y 4] ;=> [{:x y} 1 3 4]

john03:09:26

It's a more relaxed argument passing syntax for defining components with optional attributes, that would usually go in a context map as the first argument to a reagent component

john03:09:56

like

[:div :id "section1"
      :style {:padding 0}
 "hi"]

john03:09:33

Though I also made a mixin that would convert :style/padding 0 to :style {:padding 0}, like

[:div :id "section1"
      :style/padding 0
 "hi"]

john03:09:51

But if you were to do

[:div 
 :id "section1"
 "hi"
 :style/padding 0
 "there"]
You'd see hi there, so you can spread out the attributes, if you want to group attributes by certain children, for documentation/conveying context or whatever

john03:09:50

Or you could pour singular values and key/vals in any order and only the single value order matters

john03:09:08

But once you grok it, it kinda makes sense as a sorta intuitive idiom, so I figured I might find use for it in other places too

john03:09:11

The code is on another computer but I can post it up if you're interested. It's a pretty simple function

john03:09:10

Like if you ever wanted to allow in the future something like neil add dep clj-kondo/clj-kondo :latest-sha true acme/widgets :version 0.2.0, where you can add multiple libs... but in that case, you'd want to keep track of which options follow which libs

john04:09:15

Wonder if neil could precache the .cpcache somehow. Maybe with an :install option

borkdude11:09:33

This allows multiple "simple" values (subcommands) followed by key-value pairs

borkdude11:09:47

e.g. neil add dep clj-kondo/clj-kondo :latest-sha true

Carl12:09:00

@borkdude you are amazing! Thank you, I'll have a play with it tomorrow