package ocolor

  1. Overview
  2. Docs

Source file ocolor_pp.ml

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
module type PRETTY_PRINTERS =
  (sig
    type formatter
    type 'a pp = formatter -> 'a -> unit

    val pp_bool_generic:
      ?false_style:Ocolor_types.style list ->
      ?true_style:Ocolor_types.style list ->
      bool pp
    val pp_bool: bool pp

    val pp_option_generic:
      ?none:string ->
      ?none_style:Ocolor_types.style list ->
      ?some_style:Ocolor_types.style list ->
      'a pp -> 'a option pp
    val pp_option: 'a pp -> 'a option pp

    val pp_pair_generic:
      ?left:string ->
      ?sep:string ->
      ?right:string ->
      ?delim_style:Ocolor_types.style list ->
      ?sep_style:Ocolor_types.style list ->
      ?elem_style:Ocolor_types.style list ->
      'a pp -> 'b pp -> ('a * 'b) pp
    val pp_pair: 'a pp -> 'b pp -> ('a * 'b) pp

    val pp_3_tuple_generic:
      ?left:string ->
      ?sep:string ->
      ?right:string ->
      ?delim_style:Ocolor_types.style list ->
      ?sep_style:Ocolor_types.style list ->
      ?elem_style:Ocolor_types.style list ->
      'a pp -> 'b pp -> 'c pp -> ('a * 'b * 'c) pp
    val pp_3_tuple: 'a pp -> 'b pp -> 'c pp -> ('a * 'b * 'c) pp

    val pp_4_tuple_generic:
      ?left:string ->
      ?sep:string ->
      ?right:string ->
      ?delim_style:Ocolor_types.style list ->
      ?sep_style:Ocolor_types.style list ->
      ?elem_style:Ocolor_types.style list ->
      'a pp -> 'b pp -> 'c pp -> 'd pp -> ('a * 'b * 'c * 'd) pp
    val pp_4_tuple: 'a pp -> 'b pp -> 'c pp -> 'd pp -> ('a * 'b * 'c * 'd) pp

    val pp_5_tuple_generic:
      ?left:string ->
      ?sep:string ->
      ?right:string ->
      ?delim_style:Ocolor_types.style list ->
      ?sep_style:Ocolor_types.style list ->
      ?elem_style:Ocolor_types.style list ->
      'a pp -> 'b pp -> 'c pp -> 'd pp -> 'e pp -> ('a * 'b * 'c * 'd * 'e) pp
    val pp_5_tuple: 'a pp -> 'b pp -> 'c pp -> 'd pp -> 'e pp -> ('a * 'b * 'c * 'd * 'e) pp

    val pp_iterable_generic:
      ?left:string ->
      ?sep:string ->
      ?right:string ->
      ?delim_style:Ocolor_types.style list ->
      ?sep_style:Ocolor_types.style list ->
      ?elem_style:Ocolor_types.style list ->
      (('a -> unit) -> 'b -> unit) ->
      'a pp -> 'b pp
    val pp_iterable: (('a -> unit) -> 'b -> unit) -> 'a pp -> 'b pp

    val pp_list_generic:
      ?left:string ->
      ?sep:string ->
      ?right:string ->
      ?delim_style:Ocolor_types.style list ->
      ?sep_style:Ocolor_types.style list ->
      ?elem_style:Ocolor_types.style list ->
      'a pp -> 'a list pp
    val pp_list: 'a pp -> 'a list pp

    val pp_iterable_mapping_more_generic :
      ?left:string ->
      ?sep:string ->
      ?right:string ->
      ?delim_style:Ocolor_types.style list ->
      ?sep_style:Ocolor_types.style list ->
      (('a -> 'b -> unit) -> 'c -> unit) ->
      ('a * 'b) pp -> 'c pp
    val pp_iterable_mapping_generic :
      ?left:string ->
      ?sep:string ->
      ?right:string ->
      ?mapsto:string ->
      ?delim_style:Ocolor_types.style list ->
      ?sep_style:Ocolor_types.style list ->
      ?mapsto_style:Ocolor_types.style list ->
      ?key_style:Ocolor_types.style list ->
      ?value_style:Ocolor_types.style list ->
      (('a -> 'b -> unit) -> 'c -> unit) ->
      'a pp -> 'b pp -> 'c pp
    val pp_iterable_mapping :
      (('a -> 'b -> unit) -> 'c -> unit) ->
      'a pp -> 'b pp -> 'c pp

  end)

module BuildPrettyPrinters
    (PP: sig
       type formatter
       val fprintf: formatter -> ('a, formatter, unit) format -> 'a
       val pp_open_styles: formatter -> Ocolor_types.style list -> unit
       val pp_close_styles: formatter -> unit -> unit
     end
    )
  : PRETTY_PRINTERS
    with type formatter := PP.formatter
  =
  (struct
    include PP
    type 'a pp = PP.formatter -> 'a -> unit

    let pp_bool_generic
        ?(false_style: Ocolor_types.style list=Ocolor_types.[Bold;Fg (C4 red)])
        ?(true_style: Ocolor_types.style list=Ocolor_types.[Bold;Fg (C4 green)])
        (fmt: formatter) (b: bool) : unit =
      let style =
        if b then
          true_style
        else
          false_style
      in
      fprintf fmt "%a%b%a"
        pp_open_styles style b pp_close_styles ()


    let pp_bool (fmt: formatter) (b: bool) : unit =
      pp_bool_generic fmt b

    let pp_option_generic
        ?(none: string="None")
        ?(none_style: Ocolor_types.style list=Ocolor_types.[Faint])
        ?(some_style: Ocolor_types.style list=[])
        (type a)
        (p: a pp)
        (fmt: formatter)
        (o: a option)
      : unit =
      match o with
      | None ->
        fprintf fmt "%a%s%a" pp_open_styles none_style none pp_close_styles ()
      | Some o ->
        fprintf fmt "%a%a%a" pp_open_styles some_style p o pp_close_styles ()

    let pp_option p fmt o = pp_option_generic p fmt o

    let pp_pair_generic
        ?(left: string="(") ?(sep: string=", ") ?(right: string=")")
        ?(delim_style: Ocolor_types.style list=Ocolor_types.[Faint])
        ?(sep_style: Ocolor_types.style list=Ocolor_types.[Faint])
        ?(elem_style: Ocolor_types.style list=[])
        (type a) (type b)
        (f: a pp)
        (g: b pp)
        (fmt: formatter)
        (a, b : a * b)
      : unit =
      let l_delim (fmt: formatter) : unit =
        fprintf fmt "%a%s%a"
          pp_open_styles delim_style left pp_close_styles ()
      in
      let r_delim (fmt: formatter) : unit =
        fprintf fmt "%a%s%a"
          pp_open_styles delim_style right pp_close_styles ()
      in
      let sep (fmt: formatter) : unit =
        fprintf fmt "%a%s%a"
          pp_open_styles sep_style sep pp_close_styles ()
      in
      let elem (type a) (p: formatter -> a -> unit) (fmt: formatter) (a: a) : unit =
        fprintf fmt "%a%a%a"
          pp_open_styles elem_style p a pp_close_styles ()
      in
      fprintf fmt "%t%a%t%a%t"
        l_delim (elem f) a sep (elem g) b r_delim

    let pp_pair
        (type a) (type b)
        (f: a pp)
        (g: b pp)
        (fmt: formatter) (p : a * b)
      : unit =
      pp_pair_generic f g fmt p

    let pp_3_tuple_generic
        ?(left: string="(") ?(sep: string=", ") ?(right: string=")")
        ?(delim_style: Ocolor_types.style list=Ocolor_types.[Faint])
        ?(sep_style: Ocolor_types.style list=Ocolor_types.[Faint])
        ?(elem_style: Ocolor_types.style list=[])
        (type a) (type b) (type c)
        (f: a pp)
        (g: b pp)
        (h: c pp)
        (fmt: formatter)
        (a, b, c : a * b * c)
      : unit =
      let l_delim (fmt: formatter) : unit =
        fprintf fmt "%a%s%a"
          pp_open_styles delim_style left pp_close_styles ()
      in
      let r_delim (fmt: formatter) : unit =
        fprintf fmt "%a%s%a"
          pp_open_styles delim_style right pp_close_styles ()
      in
      let sep (fmt: formatter) : unit =
        fprintf fmt "%a%s%a"
          pp_open_styles sep_style sep pp_close_styles ()
      in
      let elem (type a) (p: formatter -> a -> unit) (fmt: formatter) (a: a) : unit =
        fprintf fmt "%a%a%a"
          pp_open_styles elem_style p a pp_close_styles ()
      in
      fprintf fmt "%t%a%t%a%t%a%t"
        l_delim (elem f) a sep (elem g) b sep (elem h) c r_delim

    let pp_3_tuple (type a) (type b) (type c)
        (f: a pp)
        (g: b pp)
        (h: c pp)
        (fmt: formatter)
        (t : a * b * c)
      : unit =
      pp_3_tuple_generic f g h fmt t

    let pp_4_tuple_generic
        ?(left: string="(") ?(sep: string=", ") ?(right: string=")")
        ?(delim_style: Ocolor_types.style list=Ocolor_types.[Faint])
        ?(sep_style: Ocolor_types.style list=Ocolor_types.[Faint])
        ?(elem_style: Ocolor_types.style list=[])
        (type a) (type b) (type c) (type d)
        (f: a pp)
        (g: b pp)
        (h: c pp)
        (i: d pp)
        (fmt: formatter)
        (a, b, c, d : a * b * c * d)
      : unit =
      let l_delim (fmt: formatter) : unit =
        fprintf fmt "%a%s%a"
          pp_open_styles delim_style left pp_close_styles ()
      in
      let r_delim (fmt: formatter) : unit =
        fprintf fmt "%a%s%a"
          pp_open_styles delim_style right pp_close_styles ()
      in
      let sep (fmt: formatter) : unit =
        fprintf fmt "%a%s%a"
          pp_open_styles sep_style sep pp_close_styles ()
      in
      let elem (type a) (p: formatter -> a -> unit) (fmt: formatter) (a: a) : unit =
        fprintf fmt "%a%a%a"
          pp_open_styles elem_style p a pp_close_styles ()
      in
      fprintf fmt "%t%a%t%a%t%a%t%a%t"
        l_delim (elem f) a sep (elem g) b sep (elem h) c sep (elem i) d r_delim

    let pp_4_tuple (type a) (type b) (type c) (type d)
        (f: a pp)
        (g: b pp)
        (h: c pp)
        (i: d pp)
        (fmt: formatter)
        (q : a * b * c * d)
      : unit =
      pp_4_tuple_generic f g h i fmt q

    let pp_5_tuple_generic
        ?(left: string="(") ?(sep: string=", ") ?(right: string=")")
        ?(delim_style: Ocolor_types.style list=Ocolor_types.[Faint])
        ?(sep_style: Ocolor_types.style list=Ocolor_types.[Faint])
        ?(elem_style: Ocolor_types.style list=[])
        (type a) (type b) (type c) (type d) (type e)
        (f: a pp)
        (g: b pp)
        (h: c pp)
        (i: d pp)
        (j: e pp)
        (fmt: formatter)
        (a, b, c, d, e : a * b * c * d * e)
      : unit =
      let l_delim (fmt: formatter) : unit =
        fprintf fmt "%a%s%a"
          pp_open_styles delim_style left pp_close_styles ()
      in
      let r_delim (fmt: formatter) : unit =
        fprintf fmt "%a%s%a"
          pp_open_styles delim_style right pp_close_styles ()
      in
      let sep (fmt: formatter) : unit =
        fprintf fmt "%a%s%a"
          pp_open_styles sep_style sep pp_close_styles ()
      in
      let elem (type a) (p: formatter -> a -> unit) (fmt: formatter) (a: a) : unit =
        fprintf fmt "%a%a%a"
          pp_open_styles elem_style p a pp_close_styles ()
      in
      fprintf fmt "%t%a%t%a%t%a%t%a%t%a%t"
        l_delim (elem f) a sep (elem g) b sep (elem h) c sep (elem i) d sep (elem j) e r_delim

    let pp_5_tuple (type a) (type b) (type c) (type d) (type e)
        (f: a pp)
        (g: b pp)
        (h: c pp)
        (i: d pp)
        (j: e pp)
        (fmt: formatter)
        (q: a * b * c * d * e)
      : unit =
      pp_5_tuple_generic f g h i j fmt q

    let pp_iterable_generic
        (type value) (type t)
        ?(left: string="[") ?(sep: string="; ") ?(right: string="]")
        ?(delim_style: Ocolor_types.style list=Ocolor_types.[Faint])
        ?(sep_style: Ocolor_types.style list=Ocolor_types.[Faint])
        ?(elem_style: Ocolor_types.style list=[])
        (iter: (value -> unit) -> t -> unit)
        (p: value pp)
        (fmt: formatter)
        (x: t)
      : unit =
      let () = fprintf fmt "%a%s%a" pp_open_styles delim_style left pp_close_styles () in
      let first = ref true in
      let sep (fmt: formatter) : unit =
        fprintf fmt "%a%s%a"
          pp_open_styles sep_style sep pp_close_styles ()
      in
      let value (fmt: formatter) (a: value) : unit =
        fprintf fmt "%a%a%a"
          pp_open_styles elem_style p a pp_close_styles ()
      in
      let () =
        iter
          (fun v ->
             if !first then
               first := false
             else
               sep fmt;
             value fmt v
          )
          x
      in
      let () = fprintf fmt "%a%s%a" pp_open_styles delim_style right pp_close_styles () in
      ()

    let pp_iterable (type value) (type t)
        (iter: (value -> unit) -> t -> unit)
        (p: value pp)
        (fmt: formatter)
        (x: t)
      : unit =
      pp_iterable_generic iter p fmt x

    let pp_list_generic
        ?(left: string="[") ?(sep: string="; ") ?(right: string="]")
        ?(delim_style: Ocolor_types.style list=Ocolor_types.[Faint])
        ?(sep_style: Ocolor_types.style list=Ocolor_types.[Faint])
        ?(elem_style: Ocolor_types.style list=[])
        (type a)
        (p: a pp) (fmt: formatter) (l: a list)
      : unit =
      pp_iterable_generic ~left ~sep ~right
        ~delim_style ~sep_style ~elem_style
        List.iter p fmt l

    let pp_list (type a) (p: a pp)
        (fmt: formatter) (l: a list)
      : unit =
      pp_list_generic p fmt l

    let pp_iterable_mapping_more_generic
        (type key) (type value) (type t)
        ?(left: string="{") ?(sep: string="; ") ?(right: string="}")
        ?(delim_style: Ocolor_types.style list=Ocolor_types.[Faint])
        ?(sep_style: Ocolor_types.style list=Ocolor_types.[Faint])
        (iter: (key -> value -> unit) -> t -> unit)
        (p: (key * value) pp)
        (fmt: formatter)
        (x: t)
      : unit =
      let () = fprintf fmt "%a%s%a" pp_open_styles delim_style left pp_close_styles () in
      let first = ref true in
      let sep (fmt: formatter) : unit =
        fprintf fmt "%a%s%a"
          pp_open_styles sep_style sep pp_close_styles ()
      in
      let () =
        iter
          (fun k v ->
             if !first then
               first := false
             else
               sep fmt;
             p fmt (k, v)
          )
          x
      in
      let () = fprintf fmt "%a%s%a" pp_open_styles delim_style right pp_close_styles () in
      ()

    let pp_iterable_mapping_generic
        (type key) (type value) (type t)
        ?(left: string="{") ?(sep: string="; ") ?(right: string="}")
        ?(mapsto: string=":")
        ?(delim_style: Ocolor_types.style list=Ocolor_types.[Faint])
        ?(sep_style: Ocolor_types.style list=Ocolor_types.[Faint])
        ?(mapsto_style: Ocolor_types.style list=Ocolor_types.[Faint])
        ?(key_style: Ocolor_types.style list=[])
        ?(value_style: Ocolor_types.style list=[])
        (iter: (key -> value -> unit) -> t -> unit)
        (pk: key pp)
        (pv: value pp)
        (fmt: formatter)
        (x: t)
      : unit =
      let () = fprintf fmt "%a%s%a" pp_open_styles delim_style left pp_close_styles () in
      let mapsto (fmt: formatter) : unit =
        fprintf fmt "%a%s%a"
          pp_open_styles mapsto_style mapsto pp_close_styles ()
      in
      let key (fmt: formatter) (a: key) : unit =
        fprintf fmt "%a%a%a"
          pp_open_styles key_style pk a pp_close_styles ()
      in
      let value (fmt: formatter) (a: value) : unit =
        fprintf fmt "%a%a%a"
          pp_open_styles value_style pv a pp_close_styles ()
      in
      let () = fprintf fmt "%a%s%a" pp_open_styles delim_style right pp_close_styles () in
      let p (fmt: formatter) (k, v: key * value) : unit =
        fprintf fmt "%a%t%a" key k mapsto value v
      in
      pp_iterable_mapping_more_generic ~left ~right ~sep
        ~delim_style ~sep_style iter p fmt x

    let pp_iterable_mapping
        (type key) (type value) (type t)
        (iter: (key -> value -> unit) -> t -> unit)
        (pk: key pp)
        (pv: value pp)
        (fmt: formatter)
        (x: t)
      : unit =
      pp_iterable_mapping_generic iter pk pv fmt x
  end)