-
-
Notifications
You must be signed in to change notification settings - Fork 68
/
pretty_print_condensed.cljs
278 lines (240 loc) · 10.3 KB
/
pretty_print_condensed.cljs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
(ns ^{:doc "Utilities for pretty-printing abbreviated Clojure forms"
:author "Matthew Huebert"}
day8.re-frame-10x.utils.pretty-print-condensed
(:refer-clojure :exclude [pr-seq-writer string-print pr-str-with-opts pr-opts pr])
(:require [clojure.string :as str]
[goog.string :as gstring])
(:import [goog.string StringBuffer]))
(defprotocol ILimited
(-limited? [x]))
(defn ^string truncate-string
"Truncate a string to length `n`.
Removal occurs at `cut-from`, which may be :start, :end, or :middle.
Truncation is indicated by `…` at start/end, or `...` at middle, for readability. "
([n string]
(truncate-string n :end string))
([n cut-from string]
(let [c (count string)]
(if (> c n)
(case cut-from
:start (str "…" (subs string (- c (dec n)) c))
:end (str (subs string 0 (dec n)) "…")
:middle (case n
1 "…"
2 (truncate-string n :start string)
3 (str (subs string 0 1) "…" (subs string (dec c) c))
(let [content-budget (- n 2)
per-side-budget (-> content-budget
(/ 2)
(js/Math.floor))]
;; 100 - 9 = 91 / 2 = 45
;; subs string 0
(str (subs string 0 (cond-> per-side-budget
(even? content-budget)
(dec)))
"..."
(subs string (- c per-side-budget) c)))))
string))))
(comment
(assert (= (truncate-string 5 :start "123456789") "…6789"))
(assert (= (truncate-string 5 :end "123456789") "1234…"))
;; special case use of … for short :middle-truncated strings
(assert (= (truncate-string 1 :middle "123456789") "…"))
(assert (= (truncate-string 2 :middle "123456789") "…9"))
(assert (= (truncate-string 3 :middle "123456789") "1…9"))
(assert (= (truncate-string 4 :middle "123456789") "...9"))
(assert (= (truncate-string 5 :middle "123456789") "1...9"))
(assert (= (truncate-string 6 :middle "123456789") "1...89"))
(assert (= (truncate-string 7 :middle "123456789") "12...89"))
(assert (= (truncate-string 8 :middle "123456789") "12...789")))
(defn ^string truncate-segments
([s limit] (truncate-segments s limit #"^[^.]+\." "…"))
([s limit match trunc-prefix]
(if (<= (count s) limit)
s
(let [rep (str/replace s match trunc-prefix)]
(if (= rep s)
trunc-prefix
(recur (str/replace s match trunc-prefix)
limit
match
trunc-prefix))))))
(comment
(assert (= (truncate-segments "a.bcd" 1) "…"))
(assert (= (truncate-segments "a.bcd" 2) "…"))
(assert (= (truncate-segments "a.bcd" 3) "…"))
(assert (= (truncate-segments "a.bcd" 4) "…bcd"))
(assert (= (truncate-segments "a.bcd" 5) "a.bcd"))
(assert (= (truncate-segments "a.bcd" 6) "a.bcd"))
(assert (= (truncate-segments "a.b.c" 1) "…"))
(assert (= (truncate-segments "a.b.c" 2) "…c"))
(assert (= (truncate-segments "a.b.c" 3) "…c"))
(assert (= (truncate-segments "a.b.c" 4) "…b.c"))
(assert (= (truncate-segments "a.b.c" 5) "a.b.c"))
(assert (= (truncate-segments "a.b.c" 6) "a.b.c")))
(defn ^string truncate-named
"Truncates `named`, which must satisfy INamed protocol, to within `n`
characters, cutting from beginning. Adds a `…` prefix to indicate where
cutting has occurred."
[n named]
(let [the-ns (namespace named)
the-name (name named)
kw? (keyword? named)
ns-prefix-size (if kw? 3 2)]
(if (or (> (count the-name) (if the-ns (- n ns-prefix-size) n))
(nil? the-ns))
(let [prefix (cond-> (if kw? ":" "")
the-ns (str "…/"))]
(str prefix
(truncate-string (- n (count prefix)) :start the-name)))
(let [end (str "/" the-name)
prefix (if kw? ":" "")
ns-budget (- n (count end) (count prefix))
ns-string (truncate-segments the-ns ns-budget)]
(str prefix
ns-string
end)))))
(assert (= (truncate-named 12 :city/saskatoon)
":…/saskatoon"))
(comment
(assert (= (truncate-named 1 :saskatoon) ":…"))
(assert (= (truncate-named 2 :saskatoon) ":…"))
(assert (= (truncate-named 3 :saskatoon) ":…n"))
(assert (= (truncate-named 9 :saskatoon) ":…skatoon"))
(assert (= (truncate-named 10 :saskatoon) ":saskatoon"))
(assert (= (truncate-named 1 :city/saskatoon) ":…/…"))
(assert (= (truncate-named 2 :city/saskatoon) ":…/…"))
(assert (= (truncate-named 3 :city/saskatoon) ":…/…"))
(assert (= (truncate-named 4 :city/saskatoon) ":…/…"))
(assert (= (truncate-named 5 :city/saskatoon) ":…/…n"))
(assert (= (truncate-named 11 :city/saskatoon) ":…/…skatoon"))
(assert (= (truncate-named 12 :city/saskatoon) ":…/saskatoon"))
(assert (= (truncate-named 13 :city/saskatoon) ":…/saskatoon"))
(assert (= (truncate-named 14 :city/saskatoon) ":…/saskatoon"))
(assert (= (truncate-named 15 :city/saskatoon) ":city/saskatoon"))
(assert (= (truncate-named 16 :city/saskatoon) ":city/saskatoon"))
(assert (= (truncate-named 8 'saskatoon) "…skatoon"))
(assert (= (truncate-named 9 'saskatoon) "saskatoon"))
(assert (= (truncate-named 10 'saskatoon) "saskatoon"))
(assert (= (truncate-named 1 'city/saskatoon) "…/…"))
(assert (= (truncate-named 2 'city/saskatoon) "…/…"))
(assert (= (truncate-named 3 'city/saskatoon) "…/…"))
(assert (= (truncate-named 4 'city/saskatoon) "…/…n"))
(assert (= (truncate-named 10 'city/saskatoon) "…/…skatoon"))
(assert (= (truncate-named 11 'city/saskatoon) "…/saskatoon"))
(assert (= (truncate-named 12 'city/saskatoon) "…/saskatoon"))
(assert (= (truncate-named 13 'city/saskatoon) "…/saskatoon"))
(assert (= (truncate-named 14 'city/saskatoon) "city/saskatoon"))
(assert (= (truncate-named 15 'city/saskatoon) "city/saskatoon")))
(defn ^string truncate [n location param]
(if (satisfies? INamed param)
(truncate-named n param)
(truncate-string n location (str param))))
(defn str->namespaced-sym [s]
(if (string? s)
(let [name (second (re-find #"\.([^.]+)$" s))]
(if name (symbol (subs s 0 (- (count s) (count name) 1))
name)
(symbol s)))
s))
(defn edges
"Return left and right edges of a collection (eg. brackets plus prefixes), defaults to [< >]."
[coll]
(cond (map? coll) [\{ \}]
(vector? coll) [\[ \]]
(set? coll) ["#{" \}]
(or (list? coll)
(seq? coll)) ["(" ")"]
:else ["<" ">"]))
(defn with-edges
"Wrap `value` with edges of `coll`"
[coll value]
(let [[left right] (edges coll)]
(str left value right)))
(defn pretty-condensed
"Render form in abbreviated form, showing content only for keywords/strings/symbols and entering collections to a depth of 1."
([form] (pretty-condensed 0 coll? 1 form))
([depth enter-pred max-depth form]
(cond
(satisfies? INamed form) (truncate-named 16 form)
(string? form) (truncate-string 16 :middle form)
(fn? form) (or (some-> (.-name form)
(str/replace #"(^.*\$)(.*)" "$2"))
"ƒ")
(number? form) (str form)
(and (enter-pred form)
(< depth max-depth)) (with-edges form
(str/join ", " (mapv (partial pretty-condensed (inc depth) enter-pred max-depth) form)))
:else (with-edges form "…"))))
;; Copied from cljs.core and modified to take a LimitedStringBufferWriter
(defn pr-writer-impl [obj writer opts]
(if (-limited? writer)
writer
(#'cljs.core/pr-writer-impl obj writer opts)))
(defn- pr-writer
"Prefer this to pr-seq, because it makes the printing function
configurable, allowing efficient implementations such as appending
to a StringBuffer."
[obj writer opts]
(if-let [alt-impl (:alt-impl opts)]
(alt-impl obj writer (assoc opts :fallback-impl pr-writer-impl))
(pr-writer-impl obj writer opts)))
(defn pr-seq-writer [objs writer opts]
(pr-writer (first objs) writer opts)
(doseq [obj (next objs)]
(-write writer " ")
(pr-writer obj writer opts)))
(defn string-print [x]
(*print-fn* x)
nil)
(deftype LimitedStringBufferWriter [sb max-string-length ^:mutable over-limit?]
IWriter
(-write [_ s]
(when-not over-limit?
(if (<= max-string-length (.getLength ^js sb))
(set! over-limit? true)
(.append sb s)))
sb)
(-flush [_] nil)
ICounted
(-count [_]
(.getLength ^js sb))
ILimited
(-limited? [_]
over-limit?))
(defn- pr-sb-with-opts [objs opts]
(let [sb (StringBuffer.)
writer (LimitedStringBufferWriter. sb (:debux/max-string-length opts 72) false)]
(pr-seq-writer objs writer opts)
(-flush writer)
sb))
(defn pr-str-with-opts
"Prints a sequence of objects to a string, observing all the
options given in opts"
[objs opts]
(if (empty? objs)
""
(str (pr-sb-with-opts objs opts))))
(defn- pr-opts []
{:flush-on-newline *flush-on-newline*
:readably *print-readably*
:meta *print-meta*
:dup *print-dup*
:print-length *print-length*})
(defn pr-str-truncated
"Like pr, but truncates output at n characters. Use for efficient printing where
you don't want to pay the cost of printing the (possibly large) data structure."
[n & objs]
(let [opts (pr-opts)]
(->> (pr-str-with-opts objs (assoc opts
;; Each sequence element must be at least a single character, plus a space separator
:print-length (/ n 2)
:more-marker "…"
:debux/max-string-length (inc n) ;; One higher so we can detect truncation vs. max limit
:alt-impl pr-writer-impl))
(truncate-string n))))
(comment (defn testit []
(dotimes [i 5]
(time
(pr-str-truncated 200 @day8.re-frame-10x.inlined-deps.re-frame.v0v10v9.re-frame.db/app-db)))
(pr-str-truncated 200 "=>" @day8.re-frame-10x.inlined-deps.re-frame.v0v10v9.re-frame.db/app-db)))