Source file ascii_table.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
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
open Core
module Color = struct
type t = Console.Ansi.color
end
module Attr = struct
type t = Console.Ansi.attr
end
let list_sum ~f lst = List.fold lst ~init:0 ~f:(fun a b -> a + (f b))
let list_max ~f lst = List.fold lst ~init:0 ~f:(fun a b -> max a (f b))
module El = struct
type t = Attr.t list * string list
type row = t list
type grid = row list
let create attr str = (attr, String.split_lines str)
let width (_, lines) = list_max ~f:String.length lines
let height width (_, lines) ~display_empty_rows =
let height =
list_sum lines ~f:(fun s -> max (((String.length s)+(width-1))/(max width 1)) 1)
in
if display_empty_rows
then max height 1
else height
;;
let rec slices width lines =
match lines with
| [] -> []
| line :: lines ->
slices_split width lines line (String.length line) 0
and slices_split width lines line line_len pos =
let chunk_len = min width (line_len - pos) in
let completely_fits = Int.(=) chunk_len (line_len - pos) in
let chunk = String.sub line ~pos ~len:chunk_len in
if completely_fits
then chunk :: slices width lines
else chunk :: slices_split width lines line line_len (pos + width)
;;
end
module Align = struct
type t =
| Left
| Right
| Center
end
type show = [ `Yes | `No | `If_not_empty ]
module Column = struct
type 'a t = {
max_text_width : int;
header : string;
col_func : 'a -> El.t;
align : Align.t;
min_width : int option;
show : show;
}
type constraints = {
total_width : int;
min_widths : (string * int) list;
} [@@deriving sexp]
exception Impossible_table_constraints of constraints [@@deriving sexp]
let create_attr ?(align=Align.Left) ?min_width ?(max_width=90) ?(show=`Yes)
str parse_func = {
max_text_width = max_width+1;
header = str;
col_func = (fun x -> match parse_func x with (a, b) -> El.create a b);
align = align;
min_width = Option.map min_width ~f:((+)1);
show;
};;
let create ?(align=Align.Left) ?min_width ?(max_width=90) ?show
str parse_func =
create_attr ?min_width ~align ~max_width ?show str
(fun x -> [], (parse_func x))
;;
let header_to_el alst t = El.create alst t.header
let make col_val t = t.col_func col_val
let desired_width ~spacing data t =
let column_data = List.map data ~f:t.col_func in
let header_width =
String.split t.header ~on:'\n'
|> list_max ~f:String.length
in
1 + (2*spacing)
+ (min (t.max_text_width - (1+(2*spacing)))
(max header_width (list_max column_data ~f:El.width)))
;;
let layout ~spacing table_width ts data =
let desired_widths = List.map ts ~f:(desired_width ~spacing data) in
let all_min_width = List.filter_map ts ~f:(fun t -> t.min_width) in
let table_constraints_are_impossible, generic_min_chars =
let columns_with_no_min_width = (List.length ts) - (List.length all_min_width) in
if Int.(<>) 0 columns_with_no_min_width
then begin
let width = table_width - (list_sum all_min_width ~f:Fn.id) in
let generic_min_chars = width / columns_with_no_min_width in
let impossible = generic_min_chars < 1 + (1 + (spacing * 2)) in
(impossible, generic_min_chars)
end else begin
let min_total = List.fold ~init:0 all_min_width ~f:Int.(+) in
let extra_per_col = 1 + 1 + (spacing * 2) in
let impossible = table_width < min_total + (List.length ts * extra_per_col) in
(impossible, 0)
end
in
if table_constraints_are_impossible then
raise (Impossible_table_constraints {
total_width = (table_width+1);
min_widths = (List.filter_map ts ~f:(fun t ->
Option.map t.min_width ~f:(fun num_chars -> (t.header, num_chars))));
});
let left = ref ((list_sum ~f:Fn.id desired_widths) - table_width) in
let stop = ref false in
let rec decide_widths desired_widths =
if !stop then desired_widths else begin
stop := true;
assert(List.length ts = List.length desired_widths);
decide_widths (List.map2_exn ts desired_widths ~f:(fun t column_width ->
let min_chars = match t.min_width with
| Some x -> x
| None -> generic_min_chars
in
if (column_width <= min_chars || !left <= 0) then column_width else
(left := !left - 1; stop := false; column_width - 1)))
end
in
List.map ~f:(fun x -> x-(1+(spacing*2))) (decide_widths desired_widths)
;;
end
module Table_char = struct
type t = {
ascii : char;
utf8 : string;
}
let connect ?top ?bottom ?left ?right () =
let top, bottom, left, right =
is_some top, is_some bottom, is_some left, is_some right
in
let ascii, utf8 =
match top, bottom, left, right with
| false, false, true, true -> ('-', "\226\148\128")
| true, true, false, false -> ('|', "\226\148\130")
| false, true, false, true -> ('|', "\226\148\140")
| false, true, true, false -> ('|', "\226\148\144")
| true, false, false, true -> ('|', "\226\148\148")
| true, false, true, false -> ('|', "\226\148\152")
| true, true, false, true -> ('|', "\226\148\156")
| true, true, true, false -> ('|', "\226\148\164")
| false, true, true, true -> ('-', "\226\148\172")
| true, false, true, true -> ('-', "\226\148\180")
| true, true, true, true -> ('+', "\226\148\188")
| false, false, true, false -> ('-', "\226\149\180")
| true, false, false, false -> ('|', "\226\149\181")
| false, false, false, true -> ('-', "\226\149\182")
| false, true, false, false -> ('|', "\226\149\183")
| false, false, false, false -> (' ', " ")
in
{ ascii; utf8 }
end
module Draw = struct
type point =
| Line
| Blank
| Char of Attr.t list * char
type screen = {
data : point array array;
rows : int;
cols : int;
}
let create_screen ~rows ~cols =
let data = Array.make_matrix ~dimx:rows ~dimy:cols (Char ([], ' ')) in
{ data; rows; cols }
;;
let set_screen_point ~screen ~row ~col ~point =
let c = screen.data.(row).(col) in
let new_point =
match c, point with
| Blank, _ -> Blank
| _, Blank -> Blank
| _, point -> point
in
screen.data.(row).(col) <- new_point
let hline ~screen ~row ~col1 ~col2 ?(point=Line) () =
for col = col1 to col2 do
set_screen_point ~screen ~row ~col ~point
done
;;
let vline ~screen ~col ~row1 ~row2 ?(point=Line) () =
for row = row1 to row2 do
set_screen_point ~screen ~row ~col ~point
done
;;
let char ~screen ~row ~col ~char ~attr =
screen.data.(row).(col) <- Char (attr, char)
;;
let string ~screen ~row ~col ~string ~attr =
for i = 0 to String.length string - 1 do
char ~screen ~row ~col:(col + i) ~char:string.[i] ~attr
done
;;
let aligned ~screen ~row ~col ~string:str ~attr ~width ~align =
let col =
match align with
| Align.Left -> col
| Align.Right -> col + width - String.length str
| Align.Center -> col + (max 0 (width - String.length str)) / 2
in
string ~screen ~row ~col ~string:str ~attr
;;
let get_symbol ~screen ~row ~col =
let top = row > 0 && screen.data.(row - 1).(col ) = Line in
let bottom = row < screen.rows - 1 && screen.data.(row + 1).(col ) = Line in
let left = col > 0 && screen.data.(row ).(col - 1) = Line in
let right = col < screen.cols - 1 && screen.data.(row ).(col + 1) = Line in
Table_char.connect
?top: (Option.some_if top ())
?bottom:(Option.some_if bottom ())
?left: (Option.some_if left ())
?right: (Option.some_if right ())
()
;;
let render ~screen ~bars ~output ~close =
let buf = Buffer.create 1024 in
let current_attr = ref [] in
let update_attr attr =
let attr = List.sort ~compare:Poly.compare attr in
if attr <> !current_attr then begin
if Buffer.length buf > 0 then output !current_attr buf;
current_attr := attr;
end;
in
for row = 0 to screen.rows - 1 do
for col = 0 to screen.cols - 1 do
match screen.data.(row).(col) with
| Char (attr, ch) ->
update_attr attr;
Buffer.add_char buf ch
| Blank ->
Buffer.add_char buf ' '
| Line ->
update_attr [];
let { Table_char.ascii; utf8 } = get_symbol ~screen ~row ~col in
match bars with
| `Ascii -> Buffer.add_char buf ascii
| `Unicode -> Buffer.add_string buf utf8
done;
update_attr [];
Buffer.add_char buf '\n'
done;
output !current_attr buf;
close buf
;;
let output ~oc ~screen ~bars =
render ~screen ~bars ~close:ignore ~output:(fun attr buf ->
Console.Ansi.output_string attr oc (Buffer.contents buf);
Buffer.clear buf)
;;
let to_string ~screen ~bars =
let buf = Buffer.create 1024 in
render ~screen ~bars
~output:(fun attr buf' ->
Buffer.add_string buf (Console.Ansi.string_with_attr attr (Buffer.contents buf'));
Buffer.clear buf')
~close:(fun _ -> Buffer.contents buf)
;;
let to_string_noattr ~screen ~bars =
render ~screen ~bars ~output:(fun _ _ -> ()) ~close:Buffer.contents
;;
end
module Display = struct
type t =
| Short_box
| Tall_box
| Line
| Blank
| Column_titles
let short_box = Short_box
let tall_box = Tall_box
let line = Line
let blank = Blank
let column_titles = Column_titles
end
module Grid = struct
type t = {
data: El.grid;
heights: int list;
widths: int list;
aligns: Align.t list;
}
let create ~spacing ~display max_width h_attr cols raw_data ~display_empty_rows =
let body = List.map raw_data ~f:(fun x -> List.map cols ~f:(Column.make x)) in
let empty =
List.fold body ~init:(List.map cols ~f:(fun _ -> true))
~f:(List.map2_exn ~f:(fun is_empty (_attr, lines) ->
is_empty && List.for_all lines ~f:(String.equal "")))
in
let keep = List.map2_exn cols empty ~f:(fun { Column.show; _ } is_empty ->
match show with
| `Yes -> true
| `No -> false
| `If_not_empty -> not is_empty)
in
let filter l = List.filter_opt (List.map2_exn keep l ~f:Option.some_if) in
let cols = filter cols in
let body = List.map body ~f:filter in
let widths = Column.layout ~spacing (max_width-1) cols raw_data in
let grid_data = (List.map cols ~f:(Column.header_to_el h_attr))::body in
let heights =
if display = Display.Line then
List.map grid_data ~f:(fun _ -> 1)
else
List.map grid_data ~f:(fun row ->
assert(List.length widths = List.length row);
list_max ~f:Fn.id (List.map2_exn widths row ~f:(El.height ~display_empty_rows)))
in
let aligns = List.map cols ~f:(fun c -> c.Column.align) in
{data = grid_data; heights = heights; widths = widths; aligns=aligns}
;;
let draw ~spacing ~display t =
assert(List.length t.data = List.length t.heights);
let mid_row = if display = Display.Tall_box then 1 else 0 in
let cols = list_sum t.widths ~f:((+) (1 + (spacing * 2))) + 1 in
let rows = list_sum t.heights ~f:((+) mid_row) + 3 - 2 * mid_row in
let screen = Draw.create_screen ~rows ~cols in
let point =
if display = Display.Column_titles
then Draw.Blank
else Draw.Line
in
Draw.hline ~screen ~row:0 ~col1:0 ~col2:(cols - 1) ~point ();
Draw.hline ~screen ~row:(rows - 1) ~col1:0 ~col2:(cols - 1) ~point ();
if display <> Display.Blank then begin
Draw.vline ~screen ~col:0 ~row1:0 ~row2:(rows - 1) ~point ();
ignore (
List.fold t.widths ~init:0 ~f:(fun col width ->
let col = col + 1 + width + spacing * 2 in
Draw.vline ~screen ~col ~row1:0 ~row2:(rows - 1) ~point ();
col)
: int
);
end;
ignore (
List.fold2_exn t.data t.heights ~init:1 ~f:(fun row row_elements height ->
let header_row = row = 1 in
ignore (
List.fold2_exn row_elements (List.zip_exn t.widths t.aligns) ~init:(1 + spacing)
~f:(fun col (attr, lines) (width, align) ->
let strings = El.slices width lines in
if display = Display.Line then
match strings with
| [] ->
()
| [string] ->
Draw.aligned ~screen ~row ~col ~attr ~string ~align ~width
| string :: _ ->
Draw.aligned ~screen ~row ~col ~attr ~string ~align ~width;
for col = col + (max 0 (width - 3)) to col + width - 1 do
Draw.char ~screen ~row ~col ~char:'.' ~attr:[]
done
else
ignore (
List.fold strings ~init:row ~f:(fun row string ->
Draw.aligned ~screen ~row ~col ~attr ~string ~align ~width;
row + 1)
: int
);
col + 1 + spacing * 2 + width)
: int
);
let row = row + height in
if display = Display.Tall_box || header_row then begin
if display <> Display.Blank then
Draw.hline ~screen ~row ~col1:0 ~col2:(cols - 1) ();
row + 1
end else
row)
: int
);
screen
;;
end
type ('a,'rest) renderer =
?display : Display.t
-> ?spacing : int
-> ?limit_width_to : int
-> ?header_attr : Attr.t list
-> ?bars : [ `Ascii | `Unicode ]
-> ?display_empty_rows : bool
-> 'a Column.t list
-> 'a list
-> 'rest
let output ?(display=Display.short_box) ?(spacing=1) ?(limit_width_to=90)
?(header_attr=[]) ?(bars=`Unicode) ?(display_empty_rows=false) cols data ~oc =
if cols = [] then
()
else
let screen =
Grid.create ~spacing ~display limit_width_to header_attr cols data
~display_empty_rows
|> Grid.draw ~spacing ~display
in
Draw.output ~oc ~screen ~bars
;;
let to_string_gen ?(display=Display.short_box) ?(spacing=1) ?(limit_width_to=90)
?(header_attr=[]) ?(bars=`Unicode) ?(display_empty_rows=false) cols data ~use_attr =
if cols = [] then
""
else
let screen =
Grid.create ~spacing ~display limit_width_to header_attr cols data
~display_empty_rows
|> Grid.draw ~spacing ~display
in
if use_attr then
Draw.to_string ~screen ~bars
else
Draw.to_string_noattr ~screen ~bars
;;
let to_string_noattr = to_string_gen ~use_attr:false
let to_string = to_string_gen ~use_attr:true
let simple_list_table ?(index=false)
?(limit_width_to=160)
?(oc=stdout)
?(display=Display.line)
cols data =
let cols, data =
if index
then "#" :: cols, List.mapi data ~f:(fun i row -> Int.to_string (i+1) :: row)
else cols, data in
let cols = List.mapi cols ~f:(fun i col ->
let col, align =
match String.chop_prefix col ~prefix:"-" with
| None -> col, Align.Right
| Some col -> col, Align.Left
in Column.create col (fun ls -> List.nth_exn ls i) ~align) in
output ~oc ~display ~limit_width_to cols data
let%test_module _ =
(module struct
let col1 = Column.create "a" (fun (x, _, _) -> x)
let col2 = Column.create "b" (fun (_, x, _) -> x)
let col3 = Column.create "c" (fun (_, _, x) -> x)
let%expect_test _ =
let stringify display =
to_string ~bars:`Ascii ~display [col1; col2; col3]
[ ("a1", "b1", "c1")
; ("a2", "b2", "c2")
]
in
printf "%s" (stringify Display.short_box);
[%expect {|
|--------------|
| a | b | c |
|----+----+----|
| a1 | b1 | c1 |
| a2 | b2 | c2 |
|--------------| |}];
printf "%s" (stringify Display.blank);
[%expect {|
----------------
a b c
a1 b1 c1
a2 b2 c2
---------------- |}];
printf "%s" (stringify Display.column_titles);
[%expect {|
a b c
---- ---- ----
a1 b1 c1
a2 b2 c2 |}]
;;
let%expect_test "we keep empty lines if any" =
let stringify display =
to_string ~bars:`Ascii ~display [col1; col2; col3]
[ ("a1", "b_line1\nb_line2\nb_line3", "c_line1\n\nc_line3")
; ("a2", "b2", "c2")
]
in
printf "%s" (stringify Display.short_box);
[%expect {|
|------------------------|
| a | b | c |
|----+---------+---------|
| a1 | b_line1 | c_line1 |
| | b_line2 | |
| | b_line3 | c_line3 |
| a2 | b2 | c2 |
|------------------------| |}]
;;
let%expect_test "trailing newline does not result in an empty line" =
let stringify display =
to_string ~bars:`Ascii ~display [col1; col2; col3]
[ ("a\n", "b\n", "c\n") ]
in
printf "%s" (stringify Display.short_box);
[%expect {|
|-----------|
| a | b | c |
|---+---+---|
| a | b | c |
|-----------| |}]
;;
let%expect_test _ =
let stringify display =
to_string ~bars:`Ascii ~display [col1; col2; col3]
[ ("a", "b", "c\n\n\n\n\n\n\nc") ]
in
printf "%s" (stringify Display.short_box);
[%expect {|
|-----------|
| a | b | c |
|---+---+---|
| a | b | c |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | c |
|-----------| |}]
;;
let%expect_test _ =
List.iter [ true; false ] ~f:(fun display_empty_rows ->
printf "display empty rows = %b\n" display_empty_rows;
to_string ~bars:`Ascii [col1; col2; col3] ~display_empty_rows
[ "a", "b", "c"
; "", "", ""
; "d", "e", "f"
]
|> printf "%s\n";
);
[%expect {|
display empty rows = true
|-----------|
| a | b | c |
|---+---+---|
| a | b | c |
| | | |
| d | e | f |
|-----------|
display empty rows = false
|-----------|
| a | b | c |
|---+---+---|
| a | b | c |
| d | e | f |
|-----------| |}];
;;
let%test _ = const true (to_string [Column.create ~min_width:9 "foo" Fn.id] ["bar"])
end)