Skip to content
This repository was archived by the owner on Jun 12, 2024. It is now read-only.

Commit

Permalink
fix a bug in the table-pretty-print
Browse files Browse the repository at this point in the history
minhtuannguyen committed Jul 1, 2016
1 parent 237297c commit 2d2c916
Showing 2 changed files with 46 additions and 10 deletions.
26 changes: 16 additions & 10 deletions src/leiningen/leinsync/table_pretty_print.clj
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
(ns leiningen.leinsync.table-pretty-print
(:require [leiningen.core.main :as m]))

(defn- print-table
([rows with-extra-seperator-line] (print-table (keys (first rows)) rows with-extra-seperator-line))
([ks rows with-extra-seperator-line]
(defn print-table
([rows with-extra-seperator-line log-fn]
(let [ks (->> rows
(map keys)
(distinct)
(reduce concat)
(distinct))]
(print-table ks rows with-extra-seperator-line log-fn)))
([ks rows with-extra-seperator-line log-fn]
(when (seq rows)
(let [widths (map
(fn [k]
@@ -17,16 +23,16 @@
(for [[col fmt] (map vector (map #(get row %) ks) fmts)]
(format fmt (str col)))))
trailer))]
(m/info)
(m/info (fmt-row "| " " | " " |" (zipmap ks ks)))
(m/info (fmt-row "|-" "-+-" "-|" (zipmap ks spacers)))
(log-fn)
(log-fn (fmt-row "| " " | " " |" (zipmap ks ks)))
(log-fn (fmt-row "|-" "-+-" "-|" (zipmap ks spacers)))
(doseq [row rows]
(m/info (fmt-row "| " " | " " |" row))
(log-fn (fmt-row "| " " | " " |" row))
(if with-extra-seperator-line
(m/info (fmt-row "|-" "---" "-|" (zipmap ks spacers)))))))))
(log-fn (fmt-row "|-" "---" "-|" (zipmap ks spacers)))))))))

(defn print-compact-table [rows]
(print-table rows false))
(print-table rows false m/info))

(defn print-full-table [rows]
(print-table rows true))
(print-table rows true m/info))
30 changes: 30 additions & 0 deletions test/leiningen/leinsync/table_pretty_print_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
(ns leiningen.leinsync.table-pretty-print-test
(:require [clojure.test :refer :all]
[leiningen.leinsync.table-pretty-print :as pp]))

(deftest ^:unit print-table
(testing "rows with the same structure"
(let [print-state (atom [])
print-fn (fn info [& args] (swap! print-state conj args))
rows [{:k1 :v1} {:k2 :v2} {:k3 :v3}]
_ (pp/print-table rows false print-fn)]
(is (= [nil
["| :k1 | :k2 | :k3 |"]
["|-----+-----+-----|"]
["| :v1 | | |"]
["| | :v2 | |"]
["| | | :v3 |"]]
@print-state))))

(testing "rows with different structure"
(let [print-state (atom [])
print-fn (fn info [& args] (swap! print-state conj args))
rows [{:k1 :v1} {:k1 :v1 :k2 :k2} {:k3 :v3}]
_ (pp/print-table rows false print-fn)]
(is (= [nil
["| :k1 | :k2 | :k3 |"]
["|-----+-----+-----|"]
["| :v1 | | |"]
["| :v1 | :k2 | |"]
["| | | :v3 |"]]
@print-state)))))

0 comments on commit 2d2c916

Please sign in to comment.