File tree Expand file tree Collapse file tree 2 files changed +53
-11
lines changed Expand file tree Collapse file tree 2 files changed +53
-11
lines changed Original file line number Diff line number Diff line change 53
53
*print-right-margin*
54
54
72)
55
55
56
+ (def ^{:doc "If bound to ``true``, associative collections will be printed in sorted
57
+ order by their keys. Default is ``false``."
58
+ :dynamic true}
59
+ *print-sort-keys*
60
+ false)
61
+
56
62
(def ^{:doc "If ``true``, suppress printing symbol namespaces. This may be useful when
57
63
printing macroexpansions.
58
64
573
579
(defn ^:private print-map
574
580
"Print an associative collection."
575
581
[prefix suffix obj]
576
- (pprint-logical-block :prefix prefix :suffix suffix
577
- (print-length-loop [m obj]
578
- (when (seq m)
579
- (let [[k v] (first m)]
580
- (write-out k)
581
- (.write *out* " ")
582
- (write-out v)
583
- (pprint-newline :linear)
584
- (when-let [more (seq (rest m))]
585
- (.write *out* " ")
586
- (recur more)))))))
582
+ (let [coll (if *print-sort-keys*
583
+ (sort-by key obj)
584
+ obj)]
585
+ (pprint-logical-block :prefix prefix :suffix suffix
586
+ (print-length-loop [m coll]
587
+ (when (seq m)
588
+ (let [[k v] (first m)]
589
+ (pprint-logical-block
590
+ (write-out k)
591
+ (.write *out* " ")
592
+ (pprint-newline :linear)
593
+ ;; set the current length such that we won't print
594
+ ;; only a key without it's corresponding value
595
+ (binding [*current-length* (dec *current-length*)]
596
+ (write-out v)))
597
+ (when-let [more (seq (rest m))]
598
+ (.write *out* " ")
599
+ (pprint-newline :linear)
600
+ (recur more))))))))
587
601
588
602
(defn ^:private print-meta
589
603
"Print the metadata associated with an object if it has any metadata and if
Original file line number Diff line number Diff line change 154
154
"^{:a 1} #{:a}" ^{:a 1} #{:a}
155
155
"^{:a true} #{:a}" ^:a #{:a})))
156
156
157
+ (deftest pprint-print-associative-test
158
+ (let [long-map (into {} (map #(vector (keyword (python/chr %1)) %2)
159
+ (range (python/ord "a") (python/ord "z"))
160
+ (range)))]
161
+ (are [res len expr] (= res (str/rtrim
162
+ (binding [pprint/*print-sort-keys* true
163
+ *print-length* len]
164
+ (with-out-str
165
+ (pprint/pprint expr)))))
166
+ "{...}" 0 long-map
167
+ "{:a 0 ...}" 1 long-map
168
+ "{:a 0
169
+ :b 1
170
+ :c 2
171
+ :d 3
172
+ :e 4
173
+ :f 5
174
+ :g 6
175
+ :h 7
176
+ :i 8
177
+ :j 9
178
+ :k 10
179
+ :l 11
180
+ :m 12
181
+ :n 13
182
+ :o 14
183
+ ...}" 15 long-map)))
184
+
157
185
(deftest pprint-base-and-radix-test
158
186
(are [res base expr] (= res (str/rtrim
159
187
(binding [pprint/*print-radix* true
You can’t perform that action at this time.
0 commit comments