Fork me on GitHub
#clerk
<
2023-06-15
>
mkvlr13:06:26

Finally got around to simplifying modifying child viewers and setting the :page-size for the table viewer in https://github.com/nextjournal/clerk/pull/515.

🎉 5
mkvlr13:06:26

;; To customize the number of rows in the table viewer, set
;; `::clerk/page-size`. Use a value of `nil` to show all rows.
(clerk/table {::clerk/page-size 7} (map (comp vector (partial str "Row #")) (range 1 31)))

mkvlr13:06:13

as always, this also works with metadata

^{::clerk/page-size 7 ::clerk/viewer clerk/table}
(map (comp vector (partial str "Row #")) (range 1 31))

mkvlr13:06:47

this feels to me like it was the most difficult part of the viewer api until now. Curious to hear from others if things are easier now.

Craig Brozefsky14:06:29

This print-method override is causing conflicts with my own print-method override to solve the same problem 8^) https://github.com/nextjournal/clerk/blob/fbc5d8d9fb413e53a68e76a952ac744acfc8e00c/src/nextjournal/clerk/viewer.cljc#L362

Craig Brozefsky14:06:11

Here is the solution we had in place to be "good neighbors", perhaps clerk could do something similiar?

(defmacro defpatchmethod
  "An anaphoric macro for patching existing multimethods. Original method is bound to the symbol 'this'.
   Safe to execute multiple times, 'this' will always refer to the original implementation and never
   the previously patched implementation."
  [multifn dispatch-val bindings & body]
  `(let [multi#    ~multifn
         dispatch# ~dispatch-val
         original# (get-method multi# dispatch#)]
     (letfn [(define# [orig#]
               (.addMethod ^clojure.lang.MultiFn multi# dispatch#
                           (let [~'this orig#]
                             (with-meta (fn ~bindings ~@body)
                                        (merge (meta orig#)
                                               {::original orig#})))))]
       (define# (or (some-> original# meta ::original) original#)))))

(def ^:dynamic *my-edn-print-mode* false)

(defpatchmethod print-method clojure.lang.Keyword [v w]
  (if *my-edn-print-mode*
    (let [writer (java.io.StringWriter.)]
      (if (try
            (this v writer)
            (= v (edn/read-string (str writer)))
            (catch Exception e
              false))
        (.write w (str writer))
        (print-method (tagged-literal 'keyword [(.getNamespace v) (name v)]) w)))
    (this v w)))

Craig Brozefsky14:06:45

The key thing here is to use the existing print method unless in our own edn print mode

Craig Brozefsky14:06:26

also, we don't use an eval there, tho I grok why clerk may. We do keyword tagged literal.

Craig Brozefsky14:06:53

In the end, I think there needs to be a totally distinct edn printing stack 8^)

Craig Brozefsky14:06:06

Not filing an issue with clerk, because I think clerk is doing the best it can here

mkvlr17:06:07

I’ll take a look, thanks!

Craig Brozefsky01:06:19

Prolly best just to leap to transit for this use case 8^). Enjoying using Clerk, thanks for all the great work!

mkvlr19:06:58

yeah transit doensn’t have this problem, still like being able to use a zero-dep solution

Sam Ritchie15:06:10

is clerk better than mathematica now??

🧙 10
🎉 16
🏐 2