make rendering more concise by padding by column

This commit is contained in:
mehbark 2023-01-17 06:49:07 -05:00
parent 5068304b7c
commit c3509f74bd

View file

@ -30,10 +30,10 @@
(defn draw-row (defn draw-row
"Takes a seq of strs and a max length and draws a row. "Takes a seq of strs and a max length and draws a row.
Optionally takes a left and right to surround with" Optionally takes a left and right to surround with"
([seq max-len add-bar] ([seq max-col-lens add-bar]
(let [fmt-str (str "%" max-len "s")] (let [mk-fmt-str #(str "%" (nth max-col-lens %) "s")]
(->> seq (->> seq
(map (partial format fmt-str)) (map-indexed #(format (mk-fmt-str %1) %2))
((if add-bar ((if add-bar
(partial add-before-last "│") (partial add-before-last "│")
identity)) identity))
@ -49,20 +49,34 @@
(map count) (map count)
(apply max 0))) (apply max 0)))
(defn str-len
"Returns the length of a value as a string"
[s]
(count (str s)))
(defn max-str-len-matrix (defn max-str-len-matrix
"Returns the maximum length of the stringified elements of the matrix" "Returns the maximum length of the stringified elements of the matrix"
[m] [m]
(apply max (map max-str-len m))) (apply max (map max-str-len m)))
(defn matrix-col-lens
"Returns the maximum length of each column of a matrix"
[m]
(for [col (range 0 (matrix-width m))]
(->> m
(map #(get % col))
(map str-len)
(apply max))))
; could do latex, but what's the point ; could do latex, but what's the point
(defn pretty-matrix-rows (defn pretty-matrix-rows
"Make a list of fancy display strings of the rows given matrix" "Make a list of fancy display strings of the rows given matrix"
([m] (pretty-matrix-rows m false)) ([m] (pretty-matrix-rows m false))
([m add-bar] ([m add-bar]
(let [max-len (max-str-len-matrix m) (let [max-col-lens (matrix-col-lens m)
width (matrix-width m) width (matrix-width m)
row-len (+ (- width 1) (* max-len width)) row-len (+ (- width 1) (reduce + max-col-lens))
middle (map #(draw-row % max-len add-bar "┃ " " ┃") m) middle (map #(draw-row % max-col-lens add-bar "┃ " " ┃") m)
space (apply str (repeat (+ (if add-bar 2 0) row-len) " "))] space (apply str (repeat (+ (if add-bar 2 0) row-len) " "))]
(concat [(str "┏ " space " ┓")] (concat [(str "┏ " space " ┓")]
middle middle
@ -198,4 +212,4 @@
:add (pretty-add inner)))) :add (pretty-add inner))))
([op _] (with-inc-row-indices op))) ([op _] (with-inc-row-indices op)))
;TODO: better row formatting (each column is only as wide as the longest in the column) ;DONE: better row formatting (each column is only as wide as the longest in the column)