Skip to content

Commit 5af28af

Browse files
make print-table org-mode compatible
The tests all specify the key order explicitly, since I didn't want to risk the key/column order changing across Clojure versions if they were determined by calling keys on the first map. Signed-off-by: Stuart Halloway <stu@thinkrelevance.com>
1 parent db9bb15 commit 5af28af

File tree

2 files changed

+34
-12
lines changed

2 files changed

+34
-12
lines changed

src/clj/clojure/pprint/print_table.clj

+12-12
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,17 @@
2020
(fn [k]
2121
(apply max (count (str k)) (map #(count (str (get % k))) rows)))
2222
ks)
23-
fmts (map #(str "%-" % "s") widths)
24-
fmt-row (fn [row]
25-
(apply str (interpose " | "
26-
(for [[col fmt] (map vector (map #(get row %) ks) fmts)]
27-
(format fmt (str col))))))
28-
header (fmt-row (zipmap ks ks))
29-
bar (apply str (repeat (count header) "="))]
30-
(println bar)
31-
(println header)
32-
(println bar)
23+
spacers (map #(apply str (repeat % "-")) widths)
24+
fmts (map #(str "%" % "s") widths)
25+
fmt-row (fn [leader divider trailer row]
26+
(str leader
27+
(apply str (interpose divider
28+
(for [[col fmt] (map vector (map #(get row %) ks) fmts)]
29+
(format fmt (str col)))))
30+
trailer))]
31+
(println)
32+
(println (fmt-row "| " " | " " |" (zipmap ks ks)))
33+
(println (fmt-row "|-" "-+-" "-|" (zipmap ks spacers)))
3334
(doseq [row rows]
34-
(println (fmt-row row)))
35-
(println bar))))
35+
(println (fmt-row "| " " | " " |" row))))))
3636
([rows] (print-table (keys (first rows)) rows)))

test/clojure/test_clojure/pprint/test_cl_format.clj

+22
Original file line numberDiff line numberDiff line change
@@ -759,3 +759,25 @@ but it was called with an argument of type short-float.\n")
759759
'((hot dog) (hamburger) (ice cream) (french fries)))
760760
"/hot .../hamburger")
761761

762+
(simple-tests pprint-table-tests
763+
(with-out-str
764+
(print-table [:b :a]
765+
[{:a 1 :b {:a 'is-a} :c ["hi" "there"]}
766+
{:b 5 :a 7 :c "dog" :d -700}]))
767+
"
768+
| :b | :a |
769+
|-----------+----|
770+
| {:a is-a} | 1 |
771+
| 5 | 7 |
772+
"
773+
(with-out-str
774+
(print-table [:a :e :d :c]
775+
[{:a 54.7e17 :b {:a 'is-a} :c ["hi" "there"]}
776+
{:b 5 :a -2/3 :c "dog" :d 'panda}]))
777+
"
778+
| :a | :e | :d | :c |
779+
|---------+----+-------+----------------|
780+
| 5.47E18 | | | [\"hi\" \"there\"] |
781+
| -2/3 | | panda | dog |
782+
"
783+
)

0 commit comments

Comments
 (0)