Alpha - subject to change.
Prints a collection of maps in a textual table. Prints table headings
ks, and then a line of output for each row, corresponding to the keys
in ks. If ks are not specified, use the keys of the first item in rows.
user=> (use 'clojure.pprint)
nil
;; By default, columns are in the order returned by (keys (first rows))
user=> (print-table [{:a 1 :b 2 :c 3} {:b 5 :a 7 :c "dog"}])
=============
:a | :c | :b
=============
1 | 3 | 2
7 | dog | 5
=============
nil
;; If there are keys not in the first row, and/or you want to specify only
;; some, or in a particular order, give the desired keys as the first arg.
user=> (print-table [:b :a] [{:a 1 :b 2 :c 3} {:b 5 :a 7 :c "dog"}])
=======
:b | :a
=======
2 | 1
5 | 7
=======
nil
user=> (use 'clojure.pprint 'clojure.reflect)
nil
user=> (def x (:members (reflect clojure.lang.BigInt)))
#'user/x
user=> (print-table [:name :type :flags] (sort-by :name x))
======================================================================
:name | :type | :flags
======================================================================
ONE | clojure.lang.BigInt | #{:static :public :final}
ZERO | clojure.lang.BigInt | #{:static :public :final}
add | | #{:public}
bipart | java.math.BigInteger | #{:public :final}
bitLength | | #{:public}
byteValue | | #{:public}
clojure.lang.BigInt | | #{:private}
doubleValue | | #{:public}
equals | | #{:public}
floatValue | | #{:public}
fromBigInteger | | #{:static :public}
fromLong | | #{:static :public}
hashCode | | #{:public}
intValue | | #{:public}
longValue | | #{:public}
lpart | long | #{:public :final}
lt | | #{:public}
multiply | | #{:public}
quotient | | #{:public}
remainder | | #{:public}
shortValue | | #{:public}
toBigInteger | | #{:public}
toString | | #{:public}
valueOf | | #{:static :public}
======================================================================
nil
creates a graphical (Swing) inspector on the supplied regular data,
(defn print-table
"Alpha - subject to change.
Prints a collection of maps in a textual table. Prints table headings
ks, and then a line of output for each row, corresponding to the keys
in ks. If ks are not specified, use the keys of the first item in rows."
{:added "1.3"}
([ks rows]
(when (seq rows)
(let [widths (map
(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)
(doseq [row rows]
(println (fmt-row row)))
(println bar))))
([rows] (print-table (keys (first rows)) rows)))
Comments top
No comments for print-table. Log in to add a comment.