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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ For breaking changes, check [here](#breaking-changes).

[Babashka CLI](https://github.com/babashka/cli): turn Clojure functions into CLIs!

## Unreleased

- [#119](https://github.com/babashka/cli/issues/119): `format-table` now formats multiline cells appropriately
([@lread](https://github.com/lread))

## v0.8.64

- Remove `pom.xml` and `project.clj` for cljdoc
Expand Down
40 changes: 37 additions & 3 deletions src/babashka/cli.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -548,8 +548,22 @@
(map pad widths row))]
(map pad-row rows)))

(defn- expand-multiline-cells [rows]
(if (empty? rows)
[]
(let [col-cnt (count (first rows))]
(mapcat (fn [row]
(let [row-lines (mapv #(str/split-lines (str %)) row)
max-lines (reduce max (map count row-lines))]
(map (fn [line-idx]
(map #(get-in row-lines [% line-idx] "") (range col-cnt)))
(range max-lines))))
rows))))

(defn format-table [{:keys [rows indent] :or {indent 2}}]
(let [rows (pad-cells rows)
(let [rows (-> rows
expand-multiline-cells
pad-cells)
fmt-row (fn [leader divider trailer row]
(str leader
(apply str (interpose divider row))
Expand All @@ -565,9 +579,29 @@
(def rows [["a" "fooo" "bara" "bazzz" "aa"]
["foo" "bar" "bazzz"]
["fooo" "bara" "bazzz"]])

(pad-cells rows)
(format-table {:rows rows
:indent 2}))
(-> (format-table {:rows rows
:indent 2})
str/split-lines)
;; => [" a fooo bara bazzz aa"
;; " foo bar bazzz"
;; " fooo bara bazzz"]

(-> (format-table {:rows [["r1c1\nr1c1 l2" "r1c2" "r1c3"]
["r2c1 wider" "r2c2\nr2c2 l2\nr2c2 l3" "r2c3\nr2c3 l2"]
["r3c1" "r3c2 wider" "r3c3\nr3c3 l2\nr3c3 l3"]]
:indent 5})
str/split-lines)
;; => [" r1c1 r1c2 r1c3"
;; " r1c1 l2"
;; " r2c1 wider r2c2 r2c3"
;; " r2c2 l2 r2c3 l2"
;; " r2c2 l3"
;; " r3c1 r3c2 wider r3c3"
;; " r3c3 l2"
;; " r3c3 l3"]
)

(defn opts->table [{:keys [spec order]}]
(let [columns (set (mapcat (fn [[_ s]] (keys s)) spec))]
Expand Down
14 changes: 14 additions & 0 deletions test/babashka/cli_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,20 @@
(contains-row-matching #"bar <-"
table)))))

(deftest format-multiline-celled-table-test
(is (= [" r1c1 r1c2 r1c3"
" r1c1 l2"
" r2c1 wider r2c2 r2c3"
" r2c2 l2 r2c3 l2"
" r2c2 l3"
" r3c1 r3c2 wider r3c3"
" r3c3 l2"
" r3c3 l3"]
(-> (cli/format-table {:rows [["r1c1\nr1c1 l2" "r1c2" "r1c3"]
["r2c1 wider" "r2c2\nr2c2 l2\nr2c2 l3" "r2c3\nr2c3 l2"]
["r3c1" "r3c2 wider" "r3c3\nr3c3 l2\nr3c3 l3"]]})
str/split-lines))))

(deftest require-test
(is (thrown-with-msg?
Exception #"Required option: :bar"
Expand Down