Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
juerg committed Dec 16, 2023
1 parent 47da307 commit 06f7f97
Showing 1 changed file with 36 additions and 35 deletions.
71 changes: 36 additions & 35 deletions src/main/resources/com/github/jlangch/venice/ascii-table.venice
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@
(try-with [sw (io/string-writer)]
(when (header? columns)
(println sw (render-border-row columns padding (bc 1) (bc 0) (bc 2) (bc 3)))
(docoll #(println sw %) (render-data-rows columns padding (header-cells columns) (bc 4) (bc 5) (bc 6)))
(docoll #(println sw %) (render-data-rows columns :header padding (header-cells columns) (bc 4) (bc 5) (bc 6)))
(println sw (render-border-row columns padding (bc 8) (bc 7) (bc 9) (bc 10))))

(flush sw)
Expand All @@ -132,36 +132,47 @@
(render-row (map #(str/repeat fill %) col-widths)
(str left pad) (str pad middle pad) (str pad right))))

(defn render-data-rows [columns padding cells left middle right]
(defn render-data-rows [columns section padding cells left middle right]
(let [col-widths (map :width columns)
pad (str/repeat " " padding)
;; align horizontal cell count
cells (sublist cells 0 (count columns))
add-cell-count (max 0 (- (count columns) (count cells)))
cells (concat cells (repeat add-cell-count [ "" ]))
;; format cell
cells (map-indexed (fn [i v]
(let [col (nth columns i)]
(str/align (:width col)
(-> col :header :align)
(-> col :header :overflow)
v)))
cells)
;; align cell lines
cells (map str/split-lines cells)
max-lines (apply max (map count cells))
cells (map-indexed (fn [i v]
(let [width (:width (nth columns i))]
(concat v (repeat (max 0 (- max-lines (count v)))
(str/repeat " " width)))))
cells)]
(map (fn [linenr]
(render-row (map #(nth % linenr) cells) (str left pad) (str pad middle pad) (str pad right)))
(range max-lines))))
cells (format-row columns section cells)
left (str left pad)
middle (str pad middle pad)
right (str pad right)]
(->> (apply map vector cells) ;; reorder cell lines
(map #(render-row % left middle right)))))

(defn render-row [cells left middle right]
(str left (str/join middle cells) right))

(defn format-row [columns section cells]
(->> (adjust-row-cells columns cells)
(format-row-cells columns section)
(adjust-row-cells-height columns)))

(defn adjust-row-cells [columns cells]
(let [cells (sublist cells 0 (count columns))
add-cell-count (max 0 (- (count columns) (count cells)))]
(concat cells (repeat add-cell-count [ "" ]))))

(defn format-row-cells [columns section cells]
(map-indexed (fn [i v]
(let [col (nth columns i)]
(str/align (:width col)
(-> col section :align)
(-> col section :overflow)
v)))
cells))

(defn adjust-row-cells-height [columns cells]
(let [cells (map str/split-lines cells)
max-lines (apply max (map count cells))]
(map-indexed (fn [i v]
(let [width (:width (nth columns i))]
(concat v (repeat (max 0 (- max-lines (count v)))
(str/repeat " " width)))))
cells)))

(defn header? [columns]
(pos? (count (filter some? (map :header columns)))))

Expand All @@ -185,16 +196,6 @@
:footer { :text footer }
:width width }))

(defn format-cell [text rows cols width align overflow padding]
(-> (str/align width align overflow text)
(expand-lines rows cols)))

(defn expand-lines [text total-lines width]
(let [lines (str/split-lines text)
empty-line (str/repeat " " width)
add-count (max 0 (- total-lines (count lines)))]
(concat lines (repeat add-count empty-line))))

(defn max-line-length [text]
(if (sequential? text)
(->> (map max-line-length text)
Expand Down

0 comments on commit 06f7f97

Please sign in to comment.