2018-03-13 20:40:56 +00:00
|
|
|
# Implements a simple pretty printer. Offers similar functionality
|
|
|
|
# as the describe function.
|
2018-02-07 18:19:34 +00:00
|
|
|
|
2018-03-13 20:40:56 +00:00
|
|
|
(defn- pp-seq [pp seen buf a start end]
|
2018-02-07 18:19:34 +00:00
|
|
|
(if (get seen a)
|
|
|
|
(buffer-push-string buf "<cycle>")
|
|
|
|
(do
|
|
|
|
(put seen a true)
|
|
|
|
(def len (length a))
|
|
|
|
(buffer-push-string buf start)
|
|
|
|
(for [i 0 len]
|
|
|
|
(when (not= i 0) (buffer-push-string buf " "))
|
|
|
|
(pp seen buf (get a i)))
|
2018-03-13 20:40:56 +00:00
|
|
|
(buffer-push-string buf end)))
|
2018-02-07 18:19:34 +00:00
|
|
|
buf)
|
2018-03-13 20:40:56 +00:00
|
|
|
|
|
|
|
(defn- pp-dict [pp seen buf a start end]
|
2018-02-07 18:19:34 +00:00
|
|
|
(if (get seen a)
|
|
|
|
(buffer-push-string buf "<cycle>")
|
|
|
|
(do
|
|
|
|
(put seen a true)
|
|
|
|
(var k (next a nil))
|
|
|
|
(buffer-push-string buf start)
|
|
|
|
(while k
|
|
|
|
(def v (get a k))
|
|
|
|
(pp seen buf k)
|
|
|
|
(buffer-push-string buf " ")
|
|
|
|
(pp seen buf v)
|
|
|
|
(varset! k (next a k))
|
2018-03-13 20:40:56 +00:00
|
|
|
(when k (buffer-push-string buf " ")))
|
|
|
|
(buffer-push-string buf end)))
|
2018-02-07 18:19:34 +00:00
|
|
|
buf)
|
|
|
|
|
2018-03-13 20:40:56 +00:00
|
|
|
(def printers :private {
|
2018-02-07 18:19:34 +00:00
|
|
|
:array (fn [pp seen buf x] (pp-seq pp seen buf x "[" "]"))
|
|
|
|
:tuple (fn [pp seen buf x] (pp-seq pp seen buf x "(" ")"))
|
|
|
|
:table (fn [pp seen buf x] (pp-dict pp seen buf x "@{" "}"))
|
|
|
|
:struct (fn [pp seen buf x] (pp-dict pp seen buf x "{" "}"))
|
|
|
|
})
|
|
|
|
|
2018-03-13 20:40:56 +00:00
|
|
|
(defn- default_printer [pp seen buf x]
|
2018-02-07 18:19:34 +00:00
|
|
|
(buffer-push-string buf (string x))
|
|
|
|
buf)
|
|
|
|
|
2018-03-13 20:40:56 +00:00
|
|
|
(defn- pp1 [seen buf x]
|
|
|
|
(def pmaybe (get printers (type x)))
|
|
|
|
(def p (if pmaybe pmaybe default_printer))
|
2018-02-07 18:19:34 +00:00
|
|
|
(p pp1 seen buf x))
|
|
|
|
|
2018-03-13 20:40:56 +00:00
|
|
|
(defn pp
|
|
|
|
"Pretty print a value x."
|
|
|
|
[x] (print (pp1 @{} @"" x)))
|