Permalink
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>
  • Loading branch information...
1 parent db9bb15 commit 5af28af8660e19d4a05bcc2e138dd702aed2aa84 @stuarthalloway stuarthalloway committed Aug 15, 2012
Showing with 34 additions and 12 deletions.
  1. +12 −12 src/clj/clojure/pprint/print_table.clj
  2. +22 −0 test/clojure/test_clojure/pprint/test_cl_format.clj
View
24 src/clj/clojure/pprint/print_table.clj
@@ -20,17 +20,17 @@
(fn [k]
(apply max (count (str k)) (map #(count (str (get % k))) rows)))
ks)
- fmts (map #(str "%-" % "s") widths)
- fmt-row (fn [row]
- (apply str (interpose " | "
- (for [[col fmt] (map vector (map #(get row %) ks) fmts)]
- (format fmt (str col))))))
- header (fmt-row (zipmap ks ks))
- bar (apply str (repeat (count header) "="))]
- (println bar)
- (println header)
- (println bar)
+ spacers (map #(apply str (repeat % "-")) widths)
+ fmts (map #(str "%" % "s") widths)
+ fmt-row (fn [leader divider trailer row]
+ (str leader
+ (apply str (interpose divider
+ (for [[col fmt] (map vector (map #(get row %) ks) fmts)]
+ (format fmt (str col)))))
+ trailer))]
+ (println)
+ (println (fmt-row "| " " | " " |" (zipmap ks ks)))
+ (println (fmt-row "|-" "-+-" "-|" (zipmap ks spacers)))
(doseq [row rows]
- (println (fmt-row row)))
- (println bar))))
+ (println (fmt-row "| " " | " " |" row))))))
([rows] (print-table (keys (first rows)) rows)))
View
22 test/clojure/test_clojure/pprint/test_cl_format.clj
@@ -759,3 +759,25 @@ but it was called with an argument of type short-float.\n")
'((hot dog) (hamburger) (ice cream) (french fries)))
"/hot .../hamburger")
+(simple-tests pprint-table-tests
+ (with-out-str
+ (print-table [:b :a]
+ [{:a 1 :b {:a 'is-a} :c ["hi" "there"]}
+ {:b 5 :a 7 :c "dog" :d -700}]))
+ "
+| :b | :a |
+|-----------+----|
+| {:a is-a} | 1 |
+| 5 | 7 |
+"
+ (with-out-str
+ (print-table [:a :e :d :c]
+ [{:a 54.7e17 :b {:a 'is-a} :c ["hi" "there"]}
+ {:b 5 :a -2/3 :c "dog" :d 'panda}]))
+ "
+| :a | :e | :d | :c |
+|---------+----+-------+----------------|
+| 5.47E18 | | | [\"hi\" \"there\"] |
+| -2/3 | | panda | dog |
+"
+ )

0 comments on commit 5af28af

Please sign in to comment.