-
Notifications
You must be signed in to change notification settings - Fork 7
/
DodoBox.purs
149 lines (134 loc) · 3.85 KB
/
DodoBox.purs
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
module DodoBox where
import Prelude
import Ansi.Codes (GraphicsParam)
import Data.Array (intersperse)
import Data.Array as Array
import Data.Maybe (fromMaybe)
import Dodo (Doc, plainText, print, textParagraph, twoSpaces)
import Dodo as Dodo
import Dodo.Ansi (ansiGraphics)
import Dodo.Ansi as Ansi
import Dodo.Box (Align(..), DocBox, docBox, fill, halign, horizontal, hpadding, resize, sizeOf, valign, vertical)
import Dodo.Box as Box
import DodoExampleJson (exampleJson, printJson)
import Effect (Effect)
import Effect.Class.Console as Console
para2 :: forall a. Doc a
para2 = textParagraph
"""
Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere
cubilia curae; Suspendisse eget tortor.
"""
textBox :: forall a. Int -> Doc a -> DocBox a
textBox pageWidth = print docBox (twoSpaces { pageWidth = pageWidth })
heading :: DocBox GraphicsParam -> DocBox GraphicsParam -> DocBox GraphicsParam
heading head body =
vertical
[ head
, fill (Ansi.dim (Dodo.text "-"))
{ width: max (sizeOf head).width (sizeOf body).width
, height: 1
}
, body
]
test :: Doc GraphicsParam
test = Box.toDoc do
heading
(textBox 40 (Ansi.bold (Dodo.text "Example JSON")))
( vertical
[ fill (Ansi.dim (Dodo.text "*")) { width: 120, height: 1 }
, halign Middle $ horizontal $ intersperse (hpadding 4)
[ textBox 40 (printJson exampleJson)
, valign Middle $ vertical
[ halign Middle $ textBox 40 (Ansi.bold (Dodo.text "NOTE"))
, textBox 40 (Ansi.italic para2)
]
]
]
)
table
:: forall a
. { headers :: Array (DocBox a)
, rows :: Array (Array (DocBox a))
}
-> DocBox a
table { headers, rows } =
vertical
[ rowSep
, vertical $ Array.intersperse rowSep $ map columns $ Array.cons headers rows
, rowSep
]
where
joint =
fill (Dodo.text "+") { width: 1, height: 1 }
rowSep =
horizontal
[ joint
, horizontal $ Array.intersperse joint $ map
( \width ->
fill (Dodo.text "-")
{ width: width + 2
, height: 1
}
)
widths
, joint
]
columns cols = do
let
height =
Array.foldr (max <<< _.height <<< Box.sizeOf) 0 cols
colBoxes = Array.mapWithIndex
( \ix col ->
horizontal
[ hpadding 1
, resize
{ width: fromMaybe 0 (Array.index widths ix)
, height
}
col
, hpadding 1
]
)
cols
sep = fill (Dodo.text "|") { width: 1, height }
horizontal
[ sep
, horizontal $ Array.intersperse sep colBoxes
, sep
]
widths = Array.mapWithIndex
( \ix hd ->
Array.foldr
( flip Array.index ix
>>> map (_.width <<< Box.sizeOf)
>>> fromMaybe 0
>>> max
)
(Box.sizeOf hd).width
rows
)
headers
testTable :: Doc GraphicsParam
testTable = Box.toDoc $ table
{ headers:
[ valign Middle $ halign Middle $ textBox 20 $ Dodo.text "Example"
, valign Middle $ halign Middle $ textBox 20 $ Dodo.text "Comment"
]
, rows:
[ [ textBox 40 (printJson exampleJson)
, valign Middle $ halign Middle $ textBox 40 (Ansi.italic para2)
]
, [ textBox 120 (printJson exampleJson)
, valign Middle $ textBox 60 (Ansi.italic para2)
]
, [ textBox 120 (printJson exampleJson)
, textBox 60 (Ansi.italic para2)
]
]
}
main :: Effect Unit
main = do
Console.log $ print plainText (twoSpaces { pageWidth = top }) test
Console.log $ print ansiGraphics (twoSpaces { pageWidth = top }) test
Console.log $ print plainText (twoSpaces { pageWidth = top }) testTable