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
open! Core
open! Import
include Column_intf
type 'a t =
{ max_width : int
; : String.Utf8.t
; col_func : 'a -> Cell.t
; align : Align.t
; min_width : int option
; show : Show.t
; left_boundary : bool
; right_boundary : bool
}
[@@deriving fields ~getters, sexp_of]
let lift t ~f = { t with col_func = (fun x -> t.col_func (f x)) }
let t = String.Utf8.to_string t.header
let optional t =
{ t with
col_func =
(function
| None -> Cell.create []
| Some x -> t.col_func x)
}
;;
let to_data t a =
let tuples = Cell.lines (t.col_func a) in
List.map tuples ~f:(fun (attrs, line) -> attrs, String.Utf8.to_string line)
;;
let create_attrs
?(align = Align.Left)
?min_width
?(max_width = 90)
?(show = `Yes)
?(left_boundary = true)
?(right_boundary = true)
str
parse_func
=
{ max_width
; header = String.Utf8.of_string str
; col_func = (fun x -> Cell.create (parse_func x))
; align
;
min_width = Option.map min_width ~f:(( + ) 1)
; show
; left_boundary
; right_boundary
}
;;
let create_attr
?align
?min_width
?max_width
?show
?left_boundary
?right_boundary
str
parse_func
=
create_attrs
?align
?min_width
?max_width
?show
?left_boundary
?right_boundary
str
(fun x -> [ parse_func x ])
;;
let create
?align
?min_width
?max_width
?show
?left_boundary
?right_boundary
str
parse_func
=
create_attrs
?align
?min_width
?max_width
?show
?left_boundary
?right_boundary
str
(fun x -> [ [], parse_func x ])
;;
let to_cell t ~value = t.col_func value
let ~f t = { t with header = f (header t) |> String.Utf8.of_string }
let update_show ~f t = { t with show = f t.show }
let desired_width ~spacing data t =
let column_data = List.map data ~f:t.col_func in
let =
String.Utf8.split t.header ~on:(Uchar.of_char '\n')
|> list_max ~f:String.Utf8.length_in_uchars
in
min
(t.max_width - (2 * spacing))
(max header_width (list_max column_data ~f:Cell.width))
;;
let layout ts data ~spacing ~max_width:table_width =
let n = List.length ts in
let = 1 + (spacing * 2) in
let max_width = table_width - (n * extra) in
let requests =
let%map.List t = ts in
{ Layout.Request.label = t.header
; desired_width = desired_width ~spacing data t
; min_width =
(match t.min_width with
| None -> Null
| Some x -> This (x - extra))
}
in
Layout.compute ~max_width requests
;;
module Of_field = struct
let field
?align
?min_width
?max_width
?show
?left_boundary
?right_boundary
?
to_string
record_field
=
create
?align
?min_width
?max_width
?show
?left_boundary
?right_boundary
(Option.value header ~default:(Field.name record_field))
(fun record -> to_string (Field.get record_field record))
;;
let field_attr
?align
?min_width
?max_width
?show
?left_boundary
?right_boundary
?
to_string_and_attr
record_field
=
create_attr
?align
?min_width
?max_width
?show
?left_boundary
?right_boundary
(Option.value header ~default:(Field.name record_field))
(fun record -> to_string_and_attr (Field.get record_field record))
;;
let field_opt
?align
?min_width
?max_width
?show
?left_boundary
?right_boundary
?
to_string
record_field
=
field
?align
?min_width
?max_width
?show
?left_boundary
?right_boundary
?header
(function
| None -> ""
| Some x -> to_string x)
record_field
;;
let field_opt_attr
?align
?min_width
?max_width
?show
?left_boundary
?right_boundary
?
to_string_and_attr
record_field
=
field_attr
?align
?min_width
?max_width
?show
?left_boundary
?right_boundary
?header
(function
| None -> [], ""
| Some x -> to_string_and_attr x)
record_field
;;
end
module Private = struct
let layout = layout
let to_cell = to_cell
let left_boundary = left_boundary
let right_boundary = right_boundary
end