Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/clj_commons/format/table.clj
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@
(.length (ansi/compose formatted))))))

(defn- set-width
[column data]
[column titles? data]
(let [{:keys [key ^String title width formatter]
:or {formatter identity}} column
title-width (.length title)
title-width (if titles? (.length title) 0)
width' (if width
(max width title-width)
(->> data
Expand Down Expand Up @@ -298,7 +298,7 @@
last-column-index (dec (count columns))
columns' (->> columns
(map expand-column)
(map #(set-width % rows))
(map #(set-width % titles? rows))
(map-indexed #(assoc %2 :index %1))
(map (fn [col]
(assoc col
Expand Down
32 changes: 22 additions & 10 deletions test/clj_commons/table_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@
"└──────┴──────┴──────────┘"]
(capture-table [:first :middle :last] sample-rows))))

(deftest default-style-titles-off
(is (= ["┌──────┬─┬──────────┐"
"│Arthur│C│Clark │"
"│ Alan│ │Turing │"
"│ Larry│ │Niven │"
"│ Fred│ │Flintstone│"
"└──────┴─┴──────────┘"]
(capture-table {:columns [:first :middle :last]
:style (assoc table/default-style :titles? false)} sample-rows))))



(deftest skinny-style
(is (= [" First | Middle | Last "
"-------+--------+-----------"
Expand All @@ -44,10 +56,10 @@


(deftest skinny-style-no-titles
(is (= ["Arthur | C | Clark "
" Alan | | Turing "
" Larry | | Niven "
" Fred | | Flintstone"]
(is (= ["Arthur | C | Clark "
" Alan | | Turing "
" Larry | | Niven "
" Fred | | Flintstone"]
(capture-table {:columns [:first :middle :last]
:style (assoc table/skinny-style :titles? false)}
sample-rows))))
Expand All @@ -63,10 +75,10 @@
sample-rows))))

(deftest values-only-style
(is (= ["Arthur C Clark "
" Alan Turing "
" Larry Niven "
" Fred Flintstone"]
(is (= ["Arthur C Clark "
" Alan Turing "
" Larry Niven "
" Fred Flintstone"]
(capture-table {:columns [:first :middle :last]
:style table/values-only-style}
sample-rows))))
Expand Down Expand Up @@ -111,7 +123,7 @@
" I Have No Mouth And | horror | A maleovolent AI physically an *"
" I Must Scream | | d psychologically tortures the"
" | | last remaining humans. "
" The Lathe Of Heaven | sf | A man whose convinced that his *"
" The Lathe Of Heaven | sf | A man who's convinced that his *"
" | | dreams come true seeks the car"
" | | e of a doctor "
;; The space follows the newline and is kept:
Expand All @@ -131,4 +143,4 @@
:summary "A maleovolent AI physically and psychologically tortures the last remaining humans."}
{:title "The Lathe Of Heaven"
:genre "sf"
:summary "A man whose convinced that his dreams come true seeks the care of a doctor\n who wishes to take that power for himself."}]))))
:summary "A man who's convinced that his dreams come true seeks the care of a doctor\n who wishes to take that power for himself."}]))))