Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 36 lines (34 sloc) 1.736 kb
826ff848 » stuarthalloway
2010-10-15 #458 print-table
1 ; Copyright (c) Rich Hickey. All rights reserved.
2 ; The use and distribution terms for this software are covered by the
3 ; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
4 ; which can be found in the file epl-v10.html at the root of this distribution.
5 ; By using this software in any fashion, you are agreeing to be bound by
6 ; the terms of this license.
7 ; You must not remove this notice, or any other, from this software.
8
9 (in-ns 'clojure.pprint)
10
11 (defn print-table
12 "Alpha - subject to change.
13 Prints a collection of maps in a textual table. Prints table headings
14 ks, and then a line of output for each row, corresponding to the keys
15 in ks. If ks are not specified, use the keys of the first item in rows."
16 {:added "1.3"}
17 ([ks rows]
18 (when (seq rows)
19 (let [widths (map
20 (fn [k]
21 (apply max (count (str k)) (map #(count (str (get % k))) rows)))
22 ks)
5af28af8 » stuarthalloway
2012-08-15 make print-table org-mode compatible
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)))
826ff848 » stuarthalloway
2010-10-15 #458 print-table
34 (doseq [row rows]
5af28af8 » stuarthalloway
2012-08-15 make print-table org-mode compatible
35 (println (fmt-row "| " " | " " |" row))))))
826ff848 » stuarthalloway
2010-10-15 #458 print-table
36 ([rows] (print-table (keys (first rows)) rows)))
Something went wrong with that request. Please try again.