Skip to content

Commit

Permalink
[inspector] Truncate string representation of possibly long values
Browse files Browse the repository at this point in the history
This includes strings and defaults objects (that might have custom long toString
implementations).
  • Loading branch information
alexander-yakushev authored and bbatsov committed Nov 6, 2018
1 parent c78709b commit 6137e00
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/orchard/inspect.clj
Expand Up @@ -150,6 +150,15 @@
(defn- short? [coll]
(<= (count coll) 5))

(def ^:private truncate-max-length 150)

(defn- truncate-string [s]
(when s
(let [len (count s)]
(if (> len truncate-max-length)
(str (subs s 0 (- truncate-max-length 2)) "...")
s))))

(defn value-types [value]
(cond
(nil? value) nil
Expand All @@ -176,7 +185,7 @@
(defmulti inspect-value #'value-types)

(defmethod inspect-value :atom [value]
(pr-str value))
(truncate-string (pr-str value)))

(defmethod inspect-value :seq-empty [value]
(pr-str value))
Expand Down Expand Up @@ -227,7 +236,7 @@
(pr-str value))

(defmethod inspect-value :default [value]
(str value))
(truncate-string (str value)))

(defn render-onto [inspector coll]
(update-in inspector [:rendered] concat coll))
Expand Down
2 changes: 2 additions & 0 deletions test/orchard/inspect_test.clj
Expand Up @@ -23,6 +23,7 @@
(def long-vector (vec (range 70)))
(def long-map (zipmap (range 70) (range 70)))
(def long-nested-coll (vec (map #(range (* % 10) (+ (* % 10) 80)) (range 200))))
(def truncated-string (str "\"" (apply str (repeat 147 "a")) "..."))

(defn inspect
[value]
Expand Down Expand Up @@ -224,6 +225,7 @@
(are [result form] (= result (inspect/inspect-value form))
"1" 1
"\"2\"" "2"
truncated-string (apply str (repeat 300 \a))
":foo" :foo
":abc/def" :abc/def
"( :a :b :c )" '(:a :b :c)
Expand Down

0 comments on commit 6137e00

Please sign in to comment.