From 9af5bdfe46970eaaee341a8c53bd5ef0b9455a1b Mon Sep 17 00:00:00 2001 From: Stuart Halloway Date: Wed, 15 Aug 2012 08:25:51 -0700 Subject: [PATCH] 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. --- src/clj/clojure/pprint/print_table.clj | 24 ++++++++++---------- .../clojure/test_clojure/pprint/test_cl_format.clj | 22 ++++++++++++++++++ 2 files changed, 34 insertions(+), 12 deletions(-) diff --git a/src/clj/clojure/pprint/print_table.clj b/src/clj/clojure/pprint/print_table.clj index f285b8c..539908b 100644 --- a/src/clj/clojure/pprint/print_table.clj +++ b/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))) diff --git a/test/clojure/test_clojure/pprint/test_cl_format.clj b/test/clojure/test_clojure/pprint/test_cl_format.clj index b754cb0..e06f16f 100644 --- a/test/clojure/test_clojure/pprint/test_cl_format.clj +++ b/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 | +" + ) -- 1.7.10