-
Notifications
You must be signed in to change notification settings - Fork 0
/
print.clj
44 lines (40 loc) · 1.34 KB
/
print.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
(ns io.axrs.cli-tools.print
(:require
[io.axrs.cli-tools.ansi :as ansi]))
(defn- padded [width v]
(let [v (str v)
ansi-offset (- width (count (ansi/strip v)))
padding (max width (+ (count v) ansi-offset))]
(format (str "%-" padding "s") v)))
(defn redln [& args]
(some->> args
(apply str)
ansi/red
println))
(defn table
"Prints a collection of maps in a textual table. Code is derived from
clojure.pprint/print-table, but left-aligned."
([ks rows] (table nil ks rows))
([formatter ks rows]
(when (seq rows)
(let [widths (map
(fn [k]
(apply max (count (str k)) (map #(count (str (get % k))) rows)))
ks)
rows (if formatter
(map formatter rows)
rows)
spacers (map #(apply str (repeat % "-")) widths)
fmt-row (fn [divider row]
(apply str (interpose divider
(map padded widths (map #(get row %) ks)))))]
(println)
(println (fmt-row " | " (zipmap ks ks)))
(println (fmt-row "-+-" (zipmap ks spacers)))
(doseq [row rows]
(println (fmt-row " | " row)))))))
(defn clear-screen []
;Clear
(print (str (char 27) "[2J"))
;Move cursor to start
(print (str (char 27) "[;H")))