/
table.clj
62 lines (51 loc) · 2.14 KB
/
table.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
(ns polylith.clj.core.text-table.table
(:require [clojure.string :as str]
[polylith.clj.core.util.interface.color :as c]
[polylith.clj.core.util.interface.str :as str-util]))
(defn none [_ & strings]
(str/join strings))
(def color->function
{:none none
:cyan c/cyan
:grey c/grey
:yellow c/yellow
:green c/green
:blue c/blue
:purple c/purple})
(defn align [[[x y] {:keys [value align color]}] max-width color-mode]
(let [cnt (- max-width (-> value str c/clean-colors count))
cnt-left (quot cnt 2)
cnt-right (- cnt cnt-left)
color-fn (color->function color)
spc (str-util/spaces cnt)
spc-left (str-util/spaces cnt-left)
spc-right (str-util/spaces cnt-right)
new-value (condp = align
:left (color-fn color-mode value spc)
:right (color-fn color-mode spc value)
:center (color-fn color-mode spc-left value spc-right)
(str "Error. Can't find alignment: " align))]
[[x y] {:value new-value}]))
(defn column [x cells]
(filter #(= x (ffirst %)) cells))
(defn max-column-width [x cells]
(apply max (mapv #(-> % second :value c/clean-colors count)
(column x cells))))
(defn align-column [x cells color-mode]
(let [max-width (max-column-width x cells)]
(map #(align % max-width color-mode)
(column x cells))))
(defn align-table [cells color-mode]
(into {} (mapcat #(align-column % cells color-mode) (set (map ffirst cells)))))
(defn value [x y x->spaces cells]
(if-let [{:keys [value]} (cells [x y])]
value
(or (x->spaces x) "#ERROR#")))
(defn row [initial-spaces y xs x->spaces cells]
(str initial-spaces (str/join (mapv #(value % y x->spaces cells) xs))))
(defn table [initial-spaces cells color-mode]
(let [aligned-cells (align-table cells color-mode)
xs (sort (set (map ffirst aligned-cells)))
ys (sort (set (map #(-> % first second) aligned-cells)))
x->spaces (into {} (map (juxt identity #(str-util/spaces (max-column-width % aligned-cells))) xs))]
(mapv #(row initial-spaces % xs x->spaces aligned-cells) ys)))