-
Notifications
You must be signed in to change notification settings - Fork 1
/
object.clj
700 lines (638 loc) · 20.3 KB
/
object.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
(ns pp-grid.object
(:require [clojure.string :as s]
[pp-grid.core :as c]
[pp-grid.layout :as l]))
(defn text
"Constructs a grid containing given string.
If s is not a string, then (str s)
will be used.
Each line in the string is put in a new row."
([s]
(text s 0 0))
([s pad-left pad-right]
(text s pad-left pad-right \space))
([s pad-left pad-right pad-char]
(if (string? s)
(->> s
s/split-lines
(map vector (range))
(reduce
(fn [g [y ^String line]]
(reduce
(fn [g [x v]]
(assoc g [x y] v))
g
(map vector
(range)
(str (apply str (repeat pad-left pad-char))
(if (empty? line) " " line)
(apply str (repeat pad-right pad-char))))))
(c/empty-grid)))
(text (str s) pad-left pad-right pad-char))))
(defn hline
"Constructs a horizontal line of given length.
For example, (hline 3) is '---'."
([n]
(hline n \─ \─ \─))
([n body-char]
(hline n body-char body-char body-char))
([n body-char start-char end-char]
(cond
(<= n 0) nil
(= n 1) (assoc (c/empty-grid) [0 0] body-char)
(= n 2) (text (str start-char end-char))
:else (text
(s/join
nil
(list
start-char
(s/join nil (repeat (- n 2) body-char))
end-char))))))
(defn vline
"Constructs a vertical line of given length.
For example, (vline 3) is
|
|
|
"
([n]
(vline n \│ \│ \│))
([n body-char]
(vline n body-char body-char body-char))
([n body-char start-char end-char]
(cond
(<= n 0) nil
(= n 1) (assoc (c/empty-grid) [0 0] body-char)
(= n 2) (text (str start-char \newline end-char))
:else (text (s/join
\newline
(list
start-char
(s/join \newline (repeat (- n 2) body-char))
end-char))))))
(defn hfill
"Constructs a horizontal-filler of given size.
For example, (hfill 3 \\*) is '***'."
([n]
(hfill n \space))
([n c]
(hline n c)))
(defn vfill
"Constructs a vertical-filler of given size.
For example, (vfill 3 \\*) is
*
*
*
"
([n]
(vfill n \space))
([n c]
(vline n c)))
(defn fill
"Constructs a filler of given width and height.
For example, (fill 4 4 \\*) is
****
****
****
****
"
([w h]
(fill w h \space))
([w h c]
(l/valign (repeat h (hfill w c)))))
(defn left-arrow
"Constructs a left-arrow of given length.
For example, (left-arrow 4) is '◀───'."
([n]
(left-arrow n \─ "◀"))
([n body-char]
(left-arrow n body-char "◀"))
([n body-char head-char]
(hline n body-char head-char body-char)))
(defn right-arrow
"Constructs a right-arrow of given length.
For example, (right-arrow 4) is '───▶︎'."
([n]
(right-arrow n "─" "▶"))
([n body-char]
(right-arrow n body-char "▶"))
([n body-char head-char]
(hline n body-char body-char head-char)))
(defn left-right-arrow
"Constructs a left-right-arrow of given length.
For example, (left-right-arrow 4) is '◀──▶︎'."
([n]
(left-right-arrow n \─ "◀" "▶︎"))
([n body-char]
(left-right-arrow n body-char "◀" "▶︎"))
([n body-char left-head-char right-head-char]
(hline n body-char left-head-char right-head-char)))
(defn up-arrow
"Constructs a up-arrow of given length.
For example, (up-arrow 4) is
▲
│
│
│
"
([n]
(up-arrow n \│ "▲"))
([n body-char]
(up-arrow n body-char "▲"))
([n body-char head-char]
(vline n body-char head-char body-char)))
(defn down-arrow
"Constructs a down-arrow of given length.
For example, (down-arrow 4) is
│
│
│
▼
"
([n]
(down-arrow n \│ "▼"))
([n body-char]
(down-arrow n body-char "▼"))
([n body-char head-char]
(vline n body-char body-char head-char)))
(defn up-down-arrow
"Constructs a up-down-arrow of given length.
For example, (up-down-arrow 4) is
▲
│
│
▼
"
([n]
(up-down-arrow n \│ "▲" "▼"))
([n body-char]
(up-down-arrow n body-char "▲" "▼"))
([n body-char up-head-char down-head-char]
(vline n body-char up-head-char down-head-char)))
(defn box
"Constructs a grid wrapping given grid into a box.
For example, (box (text \"HELLO\")) is
+-----+
|HELLO|
+-----+
"
[g & {:keys [left-padding
right-padding
top-padding
bottom-padding
left-border-char
right-border-char
top-border-char
bottom-border-char
top-left-corner-char
top-right-corner-char
bottom-left-corner-char
bottom-right-corner-char]
:or {left-padding 0
right-padding 0
top-padding 0
bottom-padding 0
left-border-char \|
right-border-char \|
top-border-char \-
bottom-border-char \-
top-left-corner-char \+
top-right-corner-char \+
bottom-left-corner-char \+
bottom-right-corner-char \+}}]
(let [width (+ (:width g) left-padding right-padding)
height (+ (:height g) top-padding bottom-padding 2)]
(l/halign
[(vline height left-border-char top-left-corner-char bottom-left-corner-char)
(l/valign [(hline width top-border-char)
(vfill top-padding \space)
(l/halign [(hfill left-padding) g (hfill right-padding)])
(vfill bottom-padding \space)
(hline width bottom-border-char)])
(vline height right-border-char top-right-corner-char bottom-right-corner-char)])))
(defn box1
"Constructs a grid wrapping given grid into a box.
Similar to box, but uses different border.
For example, (box1 (text \"HELLO\")) is
┌─────┐
│HELLO│
└─────┘
"
[g & {:keys [left-padding
right-padding
top-padding
bottom-padding
left-border-char
right-border-char
top-border-char
bottom-border-char
top-left-corner-char
top-right-corner-char
bottom-left-corner-char
bottom-right-corner-char]
:or {left-padding 0
right-padding 0
top-padding 0
bottom-padding 0
left-border-char "│"
right-border-char "│"
top-border-char "─"
bottom-border-char "─"
top-left-corner-char "┌"
top-right-corner-char "┐"
bottom-left-corner-char "└"
bottom-right-corner-char "┘"}}]
(box g
:left-padding left-padding
:right-padding right-padding
:top-padding top-padding
:bottom-padding bottom-padding
:left-border-char left-border-char
:right-border-char right-border-char
:top-border-char top-border-char
:bottom-border-char bottom-border-char
:top-left-corner-char top-left-corner-char
:top-right-corner-char top-right-corner-char
:bottom-left-corner-char bottom-left-corner-char
:bottom-right-corner-char bottom-right-corner-char))
(defn box2
"Constructs a grid wrapping given grid into a box.
Similar to box, but uses different border.
For example, (box2 (text \"HELLO\")) is
╒═════╕
│HELLO│
╘═════╛
"
[g & {:keys [left-padding
right-padding
top-padding
bottom-padding
left-border-char
right-border-char
top-border-char
bottom-border-char
top-left-corner-char
top-right-corner-char
bottom-left-corner-char
bottom-right-corner-char]
:or {left-padding 0
right-padding 0
top-padding 0
bottom-padding 0
left-border-char "│"
right-border-char "│"
top-border-char "═"
bottom-border-char "═"
top-left-corner-char "╒"
top-right-corner-char "╕"
bottom-left-corner-char "╘"
bottom-right-corner-char "╛"}}]
(box g
:left-padding left-padding
:right-padding right-padding
:top-padding top-padding
:bottom-padding bottom-padding
:left-border-char left-border-char
:right-border-char right-border-char
:top-border-char top-border-char
:bottom-border-char bottom-border-char
:top-left-corner-char top-left-corner-char
:top-right-corner-char top-right-corner-char
:bottom-left-corner-char bottom-left-corner-char
:bottom-right-corner-char bottom-right-corner-char))
(defn table
"Constructs a table.
Produces similar table as clojure.pprint/print-table.
Adapted from source-code of clojure.pprint/print-table.
For example, (table [:a :b] [{:a 1 :b 2} {:a 3 :b 4}]) is
+----+----+
| :a | :b |
+----+----+
| 1 | 2 |
| 3 | 4 |
+----+----+
"
[ks rows & {:keys [nsew-char
nse-char
nsw-char
ewn-char
ews-char
ns-char
ew-char
nw-char
ne-char
se-char
sw-char
header?]
:or {nsew-char \+
nse-char \+
nsw-char \+
ewn-char \+
ews-char \+
ns-char \|
ew-char \-
nw-char \+
ne-char \+
se-char \+
sw-char \+
header? true}}]
(let [widths (map
(fn [k]
(apply max
(count (str k))
(map #(c/width (get % k)) rows)))
ks)
spacers (map #(apply str (repeat % ew-char)) widths)
fmts (map #(str "%" % "s") widths)
fmt-row (fn [leader divider trailer row]
(let [vs (map #(get row %) ks)
heights (map c/height vs)
max-height (apply max heights)
->cols (fn [i]
(for [v vs]
(nth (s/split-lines (str v)) i "")))]
(s/join
\newline
(for [i (range max-height)
:let [cols (->cols i)]]
(str leader (apply
str
(interpose divider
(for [[col fmt] (map vector cols fmts)]
(format fmt (str col)))))
trailer)))))
top-border (text (fmt-row
(str se-char ew-char)
(str ew-char ews-char ew-char)
(str ew-char sw-char)
(zipmap ks spacers)))
header-divider (text (fmt-row
(str nse-char ew-char)
(str ew-char nsew-char ew-char)
(str ew-char nsw-char)
(zipmap ks spacers)))
bottom-border (text (fmt-row
(str ne-char ew-char)
(str ew-char ewn-char ew-char)
(str ew-char nw-char)
(zipmap ks spacers)))]
(l/valign [top-border
(when header?
(text (fmt-row
(str ns-char \space)
(str \space ns-char \space)
(str \space ns-char)
(zipmap ks ks))))
(when header? header-divider)
(l/valign (map (fn [row]
(text (fmt-row (str ns-char \space)
(str \space ns-char \space)
(str \space ns-char)
row)))
rows))
bottom-border])))
(defn table0
"Constructs a border-less table.
For example, (table0 [:a :b] [{:a 1 :b 2} {:a 3 :b 4}]) is
:a :b
1 2
3 4
"
([ks rows]
(table0 ks rows true))
([ks rows header?]
(table ks rows
:nsew-char " "
:nse-char " "
:nsw-char " "
:ewn-char " "
:ews-char " "
:ns-char " "
:ew-char " "
:se-char " "
:sw-char " "
:ne-char " "
:nw-char " "
:header? header?)))
(defn table1
"Constructs a table.
Similar to table but uses different border.
For example, (table1 [:a :b] [{:a 1 :b 2} {:a 3 :b 4}]) is
┌────┬────┐
│ :a │ :b │
├────┼────┤
│ 1 │ 2 │
│ 3 │ 4 │
└────┴────┘
"
([ks rows]
(table1 ks rows true))
([ks rows header?]
(table ks rows
:nsew-char "┼"
:nse-char "├"
:nsw-char "┤"
:ewn-char "┴"
:ews-char "┬"
:ns-char "│"
:ew-char "─"
:se-char "┌"
:sw-char "┐"
:ne-char "└"
:nw-char "┘"
:header? header?)))
(defn table2
"Constructs a table.
Similar to table but uses different border (rounded corners).
For example, (table2 [:a :b] [{:a 1 :b 2} {:a 3 :b 4}]) is
╭────┬────╮
│ :a │ :b │
├────┼────┤
│ 1 │ 2 │
│ 3 │ 4 │
╰────┴────╯
"
([ks rows]
(table2 ks rows true))
([ks rows header?]
(table ks rows
:nsew-char "┼"
:nse-char "├"
:nsw-char "┤"
:ewn-char "┴"
:ews-char "┬"
:ns-char "│"
:ew-char "─"
:se-char "╭"
:sw-char "╮"
:ne-char "╰"
:nw-char "╯"
:header? header?)))
(defn table3
"Constructs a table.
Similar to table but uses dotted border.
For example, (table3 [:a :b] [{:a 1 :b 2} {:a 3 :b 4}]) is
...........
: :a : :b :
..........:
: 1 : 2 :
: 3 : 4 :
:....:....:
"
([ks rows]
(table3 ks rows true))
([ks rows header?]
(table ks rows
:nsew-char "."
:nse-char "."
:nsw-char ":"
:ewn-char ":"
:ews-char "."
:ns-char ":"
:ew-char "."
:se-char "."
:sw-char "."
:ne-char ":"
:nw-char ":"
:header? header?)))
(defn matrix
"Constructs a matrix.
Similar to table, but changes the border to make it look more like a matrix.
For example, (matrix [:a :b] [{:a 1 :b 2} {:a 3 :b 4}]) is
╭ ╮
│ :a :b │
│ │
│ 1 2 │
│ 3 4 │
╰ ╯
"
([ks rows]
(matrix ks rows true))
([ks rows header?]
(l/halign [(vline (+ (if header? 4 2)
(count rows))
"│" "╭" "╰")
(table0 ks rows header?)
(vline (+ (if header? 4 2)
(count rows))
"│" "╮" "╯")])))
(defn tree
"Constructs a tree representation of a sequence.
An element of the sequence can be another sequence or a grid or
anything that can be converted to a string.
For example, (tree [1 2 [3 [4 5] [6 7] 8]]) is
┌───┐
│ 1 │
└───┘
│
┌───┐
│ 2 │
└───┘
│
┌───┐ ┌───┐ ┌───┐ ┌───┐
│ 3 │ │ 4 │ │ 6 │ │ 8 │
└───┘ └───┘ └───┘ └───┘
│ │
┌───┐ ┌───┐
│ 5 │ │ 7 │
└───┘ └───┘
"
([node]
(tree node 2 0))
([node text-wrapper-fn]
(tree node 2 0 text-wrapper-fn))
([node x-padding y-padding]
(tree node x-padding y-padding box1))
([node x-padding y-padding text-wrapper-fn]
(tree node x-padding y-padding text-wrapper-fn "│"))
([node x-padding y-padding text-wrapper-fn branch-marker]
(cond
(sequential? node) (let [args (list x-padding
y-padding
text-wrapper-fn
branch-marker)
children (for [n node]
(if (sequential? n)
(l/halign (map #(apply tree % args) n) x-padding 0)
(apply tree n args)))]
(as-> children $
(interpose (text (str branch-marker)) $)
(l/valign $ 0 y-padding)))
(c/grid? node) node
:else (text-wrapper-fn (text (str node)) :left-padding 1 :right-padding 1))))
(defn chart-xy
"Constructs a xy-chart (scatter plot).
For example, (chart-xy (range) [0 1 2 1 0 1 2 1 0]) is
y
▲
|
| * *
|* * * *
*---*---*-▶︎ x
"
[xs ys & {:keys [point-symbol
draw-axis
x-label
y-label]
:or {point-symbol \*
draw-axis true
x-label "x"
y-label "y"}}]
(let [ks (map vector xs ys)
p (as-> (c/empty-grid) $
(apply assoc $ (interleave ks (repeat point-symbol))))
p (if draw-axis
(let [x-axis (l/===
1
(right-arrow (+ (:width p) 2) \-)
(text x-label))
y-axis (l/||
0
(vline (+ (:height p) 2) \| \| "▲")
(text y-label))
with-x-axis (assoc p [0 0] x-axis)
with-xy-axis (assoc with-x-axis [0 0] y-axis)]
(c/add with-xy-axis p)) p)]
(c/transform p (c/tf-vflip))))
(defn chart-bar
"Constructs a bar chart.
For example, (chart-bar [10 20 30 40]) is
■■■■■■■■■■■■■ 10
■■■■■■■■■■■■■■■■■■■■■■■■■■■ 20
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 30
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 40
"
[ns & {:keys [labels
max-length
bar-symbol
horizontal]
:or {labels ns
horizontal true}}]
{:pre [(or (nil? max-length)
(pos? max-length))]}
(let [min-n (apply min ns)
max-n (apply max ns)
max-length (or max-length (if horizontal 40 10))
max-delta (- max-n min-n)
unit (/ max-length (if (zero? max-delta) max-length max-delta))
scaled-ns (map (fn [v] (c/round (* v unit))) ns)
bar-symbol (cond
bar-symbol bar-symbol
horizontal "■"
:else "█")
text-labels (if horizontal
(take (count ns) (map #(text (str %)) labels))
(take (count ns) (map (fn [l]
(-> (str l)
text
(c/transform (c/tf-transpose))))
labels)))]
(if horizontal
(l/valign (for [[n text-label] (map vector scaled-ns text-labels)]
(l/=== 1 (hline n bar-symbol) text-label)))
(let [chart (l/halign (for [n scaled-ns]
(or (vline n bar-symbol) (text ""))) 2 0)
flipped-chart (c/transform chart (c/tf-vflip))]
(assoc flipped-chart
[0 0]
(l/halign text-labels 2 0))))))