Source file PrintBox_text.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
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
(** {1 Render to Text} *)
module B = PrintBox
type position = PrintBox.position = {
x:int;
y:int;
}
module Style_ansi : sig
val brackets : B.Style.t -> string * string
end = struct
open B.Style
let int_of_color_ = function
| Black -> 0
| Red -> 1
| Green -> 2
| Yellow -> 3
| Blue -> 4
| Magenta -> 5
| Cyan -> 6
| White -> 7
let codes_of_style (self:t) : int list =
let {bold;fg_color;bg_color;preformatted=_} = self in
(if bold then [1] else []) @
(match bg_color with None -> [] | Some c -> [40 + int_of_color_ c]) @
(match fg_color with None -> [] | Some c -> [30 + int_of_color_ c])
let ansi_l_to_str_ = function
| [] -> "", ""
| [a] -> Printf.sprintf "\x1b[%dm" a, "\x1b[0m"
| [a;b] -> Printf.sprintf "\x1b[%d;%dm" a b, "\x1b[0m"
| l ->
let buf = Buffer.create 16 in
let pp_num c = Buffer.add_string buf (string_of_int c) in
Buffer.add_string buf "\x1b[";
List.iteri
(fun i c ->
if i>0 then Buffer.add_char buf ';';
pp_num c)
l;
Buffer.add_string buf "m";
Buffer.contents buf, "\x1b[0m"
let brackets s = ansi_l_to_str_ (codes_of_style s)
end
module Pos = struct
type t = position
let[@inline] compare pos1 pos2 =
match compare pos1.y pos2.y with
| 0 -> compare pos1.x pos2.x
| x -> x
let origin = {x=0; y=0;}
let[@inline] move pos x y = {x=pos.x + x; y=pos.y + y}
let[@inline] (+) pos1 pos2 = move pos1 pos2.x pos2.y
let[@inline] (-) pos1 pos2 = move pos1 (-pos2.x) (-pos2.y)
let[@inline] ( * ) n pos = {x = n*pos.x; y=n*pos.y}
let[@inline] move_x pos x = move pos x 0
let[@inline] move_y pos y = move pos 0 y
end
module M = Map.Make(Pos)
let str_display_len_ = ref (fun s i len ->
Uutf.String.fold_utf_8 ~pos:i ~len:len
(fun n _ c -> match c with
| `Malformed _ -> 0
| `Uchar c -> n + max 0 (Uucp.Break.tty_width_hint c))
0 s)
let[@inline] set_string_len f = str_display_len_ := f
let[@inline] str_display_width_ s i len : int = !str_display_len_ s i len
(** {2 Output: where to print to} *)
module Output : sig
type t
val create : unit -> t
val put_string : t -> position -> string -> unit
val put_sub_string : t -> position -> string -> int -> int -> unit
val put_sub_string_brack :
t -> position -> pre:string -> string -> int -> int -> post:string -> unit
val to_string : ?indent:int -> t -> string
val to_chan : ?indent:int -> out_channel -> t -> unit
val pp : Format.formatter -> t -> unit
end = struct
(** Internal multi-line buffer suitable for unicode strings.
It is a map from start position to a printable entity (string or character)
All printable sequences are supposed to *NOT* introduce new lines *)
type printable =
| String of string
| Str_slice of {s: string;i: int;len: int}
| Str_slice_bracket of {
pre: string;
s: string;
i: int;
len: int;
post: string;
}
type t = {
mutable m: printable M.t;
}
let[@inline] put_string (self:t) pos s =
self.m <- M.add pos (String s) self.m
let[@inline] put_sub_string (self:t) pos s i len =
self.m <- M.add pos (Str_slice {s;i;len}) self.m
let[@inline] put_sub_string_brack (self:t) pos ~pre s i len ~post =
self.m <- M.add pos (Str_slice_bracket {pre;s;i;len;post}) self.m
let create () : t = {m=M.empty}
module type OUT = sig
type t
val output_char : t -> char -> unit
val output_string : t -> string -> unit
val output_substring : t -> string -> int -> int -> unit
val newline : t -> unit
end
module Make_out(O : OUT) : sig
val render : ?indent:int -> O.t -> t -> unit
end = struct
let goto ?(indent=0) (out:O.t) start dest =
for _i = start.y to dest.y - 2 do
O.newline out;
done;
if start.y < dest.y then (
O.newline out;
for _i = 1 to indent do
O.output_char out ' ';
done
);
let x_start = if start.y < dest.y then 0 else start.x in
for _i = x_start to dest.x - 1 do
O.output_char out ' '
done
let to_buf_aux_ ?(indent=0) (out:O.t) start_pos p curr_pos =
assert (Pos.compare curr_pos start_pos <= 0);
goto ~indent out curr_pos start_pos;
match p with
| String s ->
O.output_string out s;
let l = !str_display_len_ s 0 (String.length s) in
Pos.move_x start_pos l
| Str_slice {s; i; len} ->
O.output_substring out s i len;
let l = str_display_width_ s i len in
Pos.move_x start_pos l
| Str_slice_bracket {pre; s; i; len; post} ->
O.output_string out pre;
O.output_substring out s i len;
O.output_string out post;
let l = str_display_width_ s i len in
Pos.move_x start_pos l
let render ?(indent=0) (out:O.t) (self:t) : unit =
for _i = 1 to indent do O.output_char out ' ' done;
ignore (M.fold (to_buf_aux_ ~indent out) self.m Pos.origin : position);
()
end
module Out_buf = Make_out(struct
type t = Buffer.t
let output_char = Buffer.add_char
let output_string = Buffer.add_string
let output_substring = Buffer.add_substring
let newline b = Buffer.add_char b '\n'
end)
let to_string ?indent self : string =
let buf = Buffer.create 42 in
Out_buf.render ?indent buf self;
Buffer.contents buf
module Out_chan = Make_out(struct
type t = out_channel
let output_char = output_char
let output_string = output_string
let output_substring = output_substring
let newline oc = output_char oc '\n'
end)
let to_chan ?indent oc self : unit =
Out_chan.render ?indent oc self
module Out_format = Make_out(struct
type t = Format.formatter
let output_char = Format.pp_print_char
let output_string = Format.pp_print_string
let output_substring out s i len =
let s = if i=0 && len=String.length s then s else String.sub s i len in
Format.pp_print_string out s
let newline out = Format.pp_print_cut out ()
end)
let pp out (self:t) : unit =
Format.fprintf out "@[<v>%a@]" (Out_format.render ~indent:0) self
end
module Box_inner : sig
type t
val of_box : B.box -> t
val render : ansi:bool -> Output.t -> t -> unit
end = struct
type 'a shape =
| Empty
| Text of {
l: (string * int * int) list;
style: B.Style.t;
}
| Frame of 'a
| Pad of position * 'a
| Align of {
h: [`Left | `Center | `Right];
v: [`Top | `Center | `Bottom];
inner: 'a
}
| Grid of [`Bars | `None] * 'a array array
| Tree of int * 'a * 'a array
type t = {
shape : t shape;
size : position lazy_t;
}
type display_conn_basic = {
mutable left: bool;
mutable right: bool;
mutable top: bool;
mutable bottom: bool;
}
type display_connections = {
mutable nontree: display_conn_basic;
mutable tree: display_conn_basic;
}
let init_connection () = {
nontree = {
left = false;
right = false;
top = false;
bottom = false
};
tree = {
left = false;
right = false;
top = false;
bottom = false
}
}
let update_conn ?left ?right ?top ?bottom con_type =
(match left with
| None -> ()
| Some _ -> con_type.left <- true);
(match right with
| None -> ();
| Some _ -> con_type.right <- true);
(match top with
| None -> ();
| Some _ -> con_type.top <- true);
(match bottom with
| None -> ();
| Some _ -> con_type.bottom <- true)
let disp_conn ct conn =
let conn_basic =
match ct with
| `Nontree -> conn.nontree
| `Tree -> conn.tree
in
match conn_basic.left, conn_basic.right, conn_basic.top, conn_basic.bottom with
| false, false, false, false -> false
| true, false, false, false -> false
| false, true, false, false -> false
| false, false, true, false -> false
| false, false, false, true -> false
| _, _, _, _ -> true
let disp_conn_char conn =
match conn.left, conn.right, conn.top, conn.bottom with
| false, false, false, false -> ""
| true, false, false, false -> ""
| false, true, false, false -> ""
| false, false, true, false -> ""
| false, false, false, true -> ""
| true, true, false, false -> "─"
| true, false, true, false -> "┘"
| true, false, false, true -> "┐"
| false, true, true, false -> "└"
| false, true, false, true -> "┌"
| false, false, true, true -> "│"
| true, true, true, false -> "┴"
| true, true, false, true -> "┬"
| true, false, true, true -> "┤"
| false, true, true, true -> "├"
| true, true, true, true -> "┼"
type display_connection_map = {
mutable m: display_connections M.t
}
let has_border ?(ct=`Nontree) pos disp_map =
let c b = if b then 1 else 0 in
try
let conn = match ct with
| `Nontree -> (M.find pos disp_map).nontree
| `Tree -> (M.find pos disp_map).tree in
c conn.left + c conn.right + c conn.top + c conn.bottom > 1
with Not_found -> false
let create_or_update ?(ct=`Nontree) ?left ?right ?top ?bottom pos disp_map =
let (new_el, tmp_disp_map) =
try
let el = M.find pos disp_map in
(el, M.remove pos disp_map)
with Not_found -> (init_connection (), disp_map)
in
(match ct with
| `Nontree -> update_conn ?left ?right ?top ?bottom new_el.nontree
| `Tree -> update_conn ?left ?right ?top ?bottom new_el.tree);
M.add pos new_el tmp_disp_map
let size box = Lazy.force box.size
let shape b = b.shape
let _array_foldi f acc a =
let acc = ref acc in
Array.iteri (fun i x -> acc := f !acc i x) a;
!acc
let _height_line a =
_array_foldi
(fun h _ box ->
let s = size box in
max h s.y
) 0 a
let _width_column m i =
let acc = ref 0 in
for j = 0 to Array.length m - 1 do
acc := max !acc (size m.(j).(i)).x
done;
!acc
let _dim_vertical_array a =
let w = ref 0 and h = ref 0 in
Array.iter
(fun b ->
let s = size b in
w := max !w s.x;
h := !h + s.y
) a;
{x= !w; y= !h;}
let _size_matrix ~bars m =
let dim = PrintBox.dim_matrix m in
let additional_space = if bars then 1 else 0 in
let columns = Array.make (dim.x + 1) 0 in
for i = 0 to dim.x - 1 do
columns.(i+1) <- columns.(i) + (_width_column m i) + additional_space
done;
let lines = Array.make (dim.y + 1) 0 in
for j = 0 to dim.y-1 do
lines.(j+1) <- lines.(j) + (_height_line m.(j)) + additional_space
done;
columns.(dim.x) <- columns.(dim.x) - additional_space;
lines.(dim.y) <- lines.(dim.y) - additional_space;
lines, columns, additional_space
let size_of_shape = function
| Empty -> Pos.origin
| Text {l;style=_} ->
let width =
List.fold_left
(fun acc (s,i,len) -> max acc (str_display_width_ s i len))
0 l
in
{ x=width; y=List.length l; }
| Frame t -> Pos.move (size t) 2 2
| Pad (dim, b') ->
Pos.(size b' + (2 * dim))
| Align {inner=b';_} -> size b'
| Grid (style,m) ->
let bars = match style with
| `Bars -> true
| `None -> false
in
let dim = B.dim_matrix m in
let lines, columns, _space_for_bars = _size_matrix ~bars m in
{ y=lines.(dim.y); x=columns.(dim.x)}
| Tree (indent, node, children) ->
let dim_children = _dim_vertical_array children in
let s = size node in
{ x=max s.x (dim_children.x+3+indent)
; y=s.y + dim_children.y
}
let[@unroll 2] rec lines_ s i (k: string -> int -> int -> unit) : unit =
match String.index_from s i '\n' with
| j ->
k s i (j - i);
lines_ s (j+1) k
| exception Not_found ->
if i < String.length s then (
k s i (String.length s - i)
)
let lines_l_ l k =
match l with
| [] -> ()
| [s] -> lines_ s 0 k
| s1::s2::tl ->
lines_ s1 0 k;
lines_ s2 0 k;
List.iter (fun s -> lines_ s 0 k) tl
let rec of_box (b:B.t) : t =
let shape =
match B.view b with
| B.Empty -> Empty
| B.Text {l;style} ->
let acc = ref [] in
lines_l_ l (fun s i len -> acc := (s,i,len) :: !acc);
Text {l=List.rev !acc; style}
| B.Frame t -> Frame (of_box t)
| B.Pad (dim, t) -> Pad (dim, of_box t)
| B.Align {h;v;inner} -> Align {h;v;inner=of_box inner}
| B.Grid (bars, m) -> Grid (bars, B.map_matrix of_box m)
| B.Tree (i, b, l) -> Tree (i, of_box b, Array.map of_box l)
| B.Link {inner;uri} ->
let self =
of_box (B.v_record ["uri", B.text uri; "inner", inner])
in
self.shape
in
{ shape; size = lazy (size_of_shape shape) }
(** {3 Rendering} *)
let write_vline_ ?ct conn_map pos n =
conn_map.m <- create_or_update ?ct ~bottom:true (Pos.move_y pos ~-1) conn_map.m;
for j=0 to n-1 do
conn_map.m <- create_or_update ?ct ~top:true ~bottom:true (Pos.move_y pos j) conn_map.m;
done;
conn_map.m <- create_or_update ?ct ~top:true (Pos.move_y pos n) conn_map.m
let write_hline_ ?ct conn_map pos n =
conn_map.m <- create_or_update ?ct ~right:true (Pos.move_x pos ~-1) conn_map.m;
for i=0 to n-1 do
conn_map.m <- create_or_update ?ct ~left:true ~right:true (Pos.move_x pos i) conn_map.m;
done;
conn_map.m <- create_or_update ?ct ~left:true (Pos.move_x pos n) conn_map.m
let pre_render ~ansi ?(offset=Pos.origin) ?expected_size ~out b pos =
let conn_m : display_connection_map = {m=M.empty} in
let rec render_rec ~ansi ?(offset=offset) ?expected_size b pos =
match shape b with
| Empty -> conn_m.m
| Text {l;style} ->
let ansi_prelude, ansi_suffix =
if ansi then Style_ansi.brackets style else "", "" in
let has_style = ansi_prelude <> "" || ansi_suffix <> "" in
List.iteri
(fun line_idx (s,s_i,len)->
if has_style then (
Output.put_sub_string_brack out (Pos.move_y pos line_idx)
~pre:ansi_prelude s s_i len ~post:ansi_suffix
) else (
Output.put_sub_string out (Pos.move_y pos line_idx) s s_i len
))
l;
conn_m.m
| Frame b' ->
let {x;y} = size b' in
conn_m.m <- create_or_update ~right:true ~bottom:true pos conn_m.m;
conn_m.m <- create_or_update ~left:true ~top:true (Pos.move pos (x+1) (y+1)) conn_m.m;
conn_m.m <- create_or_update ~top:true ~right:true (Pos.move pos 0 (y+1)) conn_m.m;
conn_m.m <- create_or_update ~left:true ~bottom:true (Pos.move pos (x+1) 0) conn_m.m;
write_hline_ conn_m (Pos.move_x pos 1) x;
write_hline_ conn_m (Pos.move pos 1 (y+1)) x;
write_vline_ conn_m (Pos.move_y pos 1) y;
write_vline_ conn_m (Pos.move pos (x+1) 1) y;
let expected_size = match expected_size with
| Some p -> Some (Pos.move p (-2) (-2))
| None -> None
in
render_rec ~ansi ?expected_size b' (Pos.move pos 1 1)
| Pad (dim, b') ->
let expected_size = match expected_size with
| None -> None
| Some p -> Some Pos.(p - (2*dim))
in
render_rec ~offset:Pos.(offset+dim) ~ansi ?expected_size b' Pos.(pos + dim)
| Align {h;v;inner=b'} ->
begin match expected_size with
| Some expected_size ->
let hpad = match h with
| `Left -> 0
| `Center -> max 0 ((expected_size.x - (size b').x) / 2)
| `Right -> max 0 (expected_size.x - (size b').x)
and vpad = match v with
| `Top -> 0
| `Center -> max 0 ((expected_size.y - (size b').y) / 2)
| `Bottom -> max 0 (expected_size.y - (size b').y)
in
let pos' = Pos.move pos hpad vpad in
render_rec ~offset ~ansi b' pos';
| None ->
render_rec ~ansi ~offset b' pos
end
| Grid (style,m) ->
let dim = B.dim_matrix m in
let bars = match style with
| `None -> false
| `Bars -> true
in
let lines, columns, space_for_bars = _size_matrix ~bars m in
for j = 0 to dim.y - 1 do
for i = 0 to dim.x - 1 do
let expected_x = match expected_size with
| Some es when i=dim.x-1 -> es.x - columns.(i)
| _ -> columns.(i+1) - columns.(i) - (if i=dim.x-1 then 0 else space_for_bars)
and expected_y = match expected_size with
| Some es when j=dim.y-1 -> es.y - lines.(j)
| _ -> lines.(j+1) - lines.(j)- (if j=dim.y-1 then 0 else space_for_bars)
in
let expected_size = {x=expected_x; y=expected_y} in
let pos' = Pos.move pos (columns.(i)) (lines.(j)) in
conn_m.m <- render_rec ~ansi ~expected_size m.(j).(i) pos'
done;
done;
let len_hlines, len_vlines = match expected_size with
| None -> columns.(dim.x), lines.(dim.y)
| Some {x;y} -> x,y
in
begin match style with
| `None -> ()
| `Bars ->
for j=1 to dim.y - 1 do
write_hline_ conn_m (Pos.move pos (-offset.x) (lines.(j)-1)) len_hlines
done;
for i=1 to dim.x - 1 do
write_vline_ conn_m (Pos.move pos (columns.(i)-1) (-offset.y)) len_vlines
done;
for j=1 to dim.y - 1 do
for i=1 to dim.x - 1 do
conn_m.m <- create_or_update ~left:true ~right:true ~top:true ~bottom:true (Pos.move pos (columns.(i)-1) (lines.(j)-1)) conn_m.m
done
done
end;
conn_m.m
| Tree (indent, n, a) ->
conn_m.m <- render_rec ~ansi n pos;
let pos' = Pos.move pos indent (size n).y in
assert (Array.length a > 0);
if (size n).y > 0 && has_border (Pos.move_y pos' ~-1) conn_m.m then
conn_m.m <- create_or_update ~ct:`Nontree ~bottom:true (Pos.move_y pos' ~-1) conn_m.m;
let _ = _array_foldi
(fun pos' i b ->
let s = "└─" in
conn_m.m <- create_or_update ~ct:`Tree ~top:true ~right:true pos' conn_m.m;
conn_m.m <- create_or_update ~ct:`Tree ~left:true ~right:true (Pos.move_x pos' 1) conn_m.m;
conn_m.m <- create_or_update ~ct:`Tree ~top:true (Pos.move_y pos' 1) conn_m.m;
if i<Array.length a-1 then (
write_vline_ ~ct:`Tree conn_m (Pos.move_y pos' 1) ((size b).y-1)
);
let child_pos = Pos.move_x pos' (str_display_width_ s 0 (String.length s)) in
conn_m.m <- render_rec ~ansi b child_pos;
if (size b).x > 0 && has_border child_pos conn_m.m then
conn_m.m <- create_or_update ~ct:`Nontree ~left:true child_pos conn_m.m;
Pos.move_y pos' (size b).y
) pos' a
in
conn_m.m
in
render_rec ~ansi:ansi ~offset:offset ?expected_size:expected_size b pos
let post_render ~out conn_map =
let render_conn_pos pos conn =
if (pos.x >= 0) && (pos.y >=0) then
if disp_conn `Nontree conn then
Output.put_string out pos (disp_conn_char conn.nontree)
else if disp_conn `Tree conn then
Output.put_string out pos (disp_conn_char conn.tree)
in
M.iter render_conn_pos conn_map
let render ~ansi out b =
post_render ~out (pre_render ~ansi ~out b Pos.origin)
end
let to_string_with ~style b =
let buf = Output.create() in
Box_inner.render ~ansi:style buf (Box_inner.of_box b);
Output.to_string buf
let to_string = to_string_with ~style:true
let output ?(style=true) ?(indent=0) oc b =
let buf = Output.create () in
Box_inner.render ~ansi:style buf (Box_inner.of_box b);
Output.to_chan ~indent oc buf;
flush oc
let pp_with ~style out b =
let buf = Output.create () in
Box_inner.render ~ansi:style buf (Box_inner.of_box b);
Output.pp out buf
let pp = pp_with ~style:true