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
open Common;;
type terminal_code_status = {
weight: weight;
color: color option;
underline: bool;
italic: bool;
}
let initial_state = { weight = Normal; color = None; underline = false; italic = false }
let nesting_level = ref 0;;
let open_nesting () = incr nesting_level
let close_nesting () = decr nesting_level
module AnsiSgrCodes = struct
let normal = "\027[0m";;
let bold = "\027[1m";;
let output_code str =
if Unix.isatty Unix.stdout then
output_string stdout str
else ()
;;
let color_to_code = function
| Black -> "30"
| Red -> "31"
| Green -> "32"
| Yellow -> "33"
| Blue -> "34"
| Magenta -> "35"
| Cyan -> "36"
| White -> "37"
let reset_code = "\027[0m";;
let middle_gray = "\027[0;38;5;244m";;
let to_code ({weight;color;italic;underline} as _x) =
let code = "\027[0" in
let code = code ^ (match weight with
| Normal -> ""
| Bold -> ";1"
| Faint -> ";2") in
let code = code ^ (match italic with
| false -> ""
| true -> ";3") in
let code = code ^ (match underline with
| false -> ""
| true -> ";4") in
let code = code ^ (match color with
| None -> ""
| Some color -> ";" ^ (color_to_code color)) in
code ^ "m"
end;;
let terminal_stag_functions prev =
let stack = Stack.create () in
Stack.push initial_state stack;
let mark_open_stag m =
let exception Not_handeld in try
let cur = Stack.top stack in
let newer = match m with
| Weight weight ->{cur with weight}
| Color color -> {cur with color = Some color}
| Underline underline -> {cur with underline}
| Italic italic -> {cur with italic}
| _ -> raise Not_handeld
in
Stack.push newer stack;
AnsiSgrCodes.to_code newer
with Not_handeld ->
(Stack.push (Stack.top stack) stack;
prev.Format.mark_open_stag m)
in
let mark_close_stag _m =
let _ = Stack.pop stack in
let prev = Stack.top stack in
(AnsiSgrCodes.to_code prev)
in
let print_open_stag _ = assert false in
let print_close_stag _ = assert false in
Format.{ mark_close_stag; mark_open_stag; print_open_stag; print_close_stag }
;;
type line_characters = {
prefix: string;
vertical_bar: string;
right_tee: string;
horizontal_bar: string;
up_right_corner: string;
postfix: string;
}
let unicode_line_characters = {
prefix="";
vertical_bar = "│";
right_tee = "├";
horizontal_bar = "─";
up_right_corner = "└";
postfix = "";
};;
let vt100_line_characters = {
prefix = "\027(0";
vertical_bar = "\120";
right_tee = "\116";
horizontal_bar = "\113";
up_right_corner = "\109";
postfix = "\027(B";
}
;;
let ascii_line_characters = {
prefix = "";
vertical_bar = "|";
right_tee = "+";
horizontal_bar = "-";
up_right_corner = "`";
postfix = ""
};;
type choice = [`Yes | `No | `Terminal | `Autodetect];;
let use_unicode: choice ref = ref `Autodetect;;
let set_use_unicode x = use_unicode := x;;
let rec get_line_characters () =
match !use_unicode with
| `Yes -> unicode_line_characters
| `No -> ascii_line_characters
| `Terminal -> vt100_line_characters
| `Autodetect -> begin
let res = match Sys.getenv "TERM" with
| exception Not_found ->
if Unix.isatty Unix.stdout then `No
else `Yes
| "dumb" | "emacs" -> `Yes
| _ when not @@ Unix.isatty Unix.stdout -> `Yes
| "linux" | "vt100" | "konsole" | "gnome-terminal" | "xterm"
| "xterm-256color" | "screen" | "tmux" | "cygwin" | "putty"
| "alacritty" | "kitty" | "fbterm" ->
`Terminal
| s when String.starts_with ~prefix:"xterm" s -> `Terminal
| s when String.starts_with ~prefix:"rxvt" s -> `Terminal
| s when String.starts_with ~prefix:"linux" s -> `Terminal
| s when String.starts_with ~prefix:"vt100" s -> `Terminal
| s when String.starts_with ~prefix:"vte" s -> `Terminal
| s when String.starts_with ~prefix:"st" s -> `Terminal
| s when String.starts_with ~prefix:"eterm" s -> `Terminal
| _ -> `No
in use_unicode := res;
get_line_characters ()
end
;;
let print_initial_indentation ~last =
match !nesting_level with
| 0 -> ()
| _ ->
AnsiSgrCodes.(output_code middle_gray);
let lc = get_line_characters() in
output_string stdout lc.prefix;
for _i = 0 to (!nesting_level - 2) do
output_string stdout lc.vertical_bar;
output_char stdout ' '
done;
output_string stdout (if last then lc.up_right_corner else lc.right_tee);
output_string stdout lc.horizontal_bar;
output_string stdout lc.postfix;
AnsiSgrCodes.(output_code reset_code);
;;
let print_normal_indentation ~last tc =
AnsiSgrCodes.(output_code middle_gray);
let lc = get_line_characters() in
output_string stdout lc.prefix;
if last then begin
assert (!nesting_level >= 1);
for _i = 0 to !nesting_level - 2 do
output_string stdout lc.vertical_bar;
output_char stdout ' '
done;
output_string stdout " ";
end
else begin
for _i = 0 to !nesting_level do
output_string stdout lc.vertical_bar;
output_char stdout ' '
done
end;
output_string stdout lc.postfix;
AnsiSgrCodes.(output_code tc)
;;
let printf category ~last formatstring =
let buf = Format.make_symbolic_output_buffer () in
let tag = ("[" ^ category ^ "] ") in
let indentation = String.length tag in
let ppf = Format.formatter_of_symbolic_output_buffer buf in
if (Unix.isatty Unix.stdout) then begin
let formatter_tag_functions = terminal_stag_functions (Format.pp_get_formatter_stag_functions ppf ()) in
Format.pp_set_formatter_stag_functions ppf formatter_tag_functions;
Format.pp_set_mark_tags ppf true
end;
let k ppf =
Format.pp_print_flush ppf ();
let symbolic_item_list = Format.get_symbolic_output_buffer buf in
print_initial_indentation ~last;
AnsiSgrCodes.(output_code bold);
output_string stdout tag;
AnsiSgrCodes.(output_code normal);
let output_terminal_command = function
| (true,_) as tc -> tc
| (false,tc) -> output_string stdout tc; (true,tc)
in
let output_newline_and_indent ((_,terminal_command),newline_and_indent) =
match newline_and_indent with
| None -> ()
| Some n ->
output_string stdout "\n";
print_normal_indentation ~last terminal_command;
output_string stdout (String.make (n + indentation - 2) ' ')
in
let rec do_symbolic_output_item ((terminal_command,newline_and_indent) as acc) = function
| Format.Output_flush -> flush stdout; acc
| Format.Output_newline -> begin
match newline_and_indent with
| None -> ()
| Some _ -> output_string stdout "\n";
end;
(terminal_command,Some 0)
| Format.(Output_spaces n | Output_indent n) -> begin
let newline_and_indent = match newline_and_indent with
| Some m -> Some (m + n)
| None -> output_string stdout (String.make n ' '); None
in terminal_command,newline_and_indent
end
| Format.Output_string s ->
let ((terminal_command,newline_and_indent) as acc),rest = parse_for_empty_beginning acc s 0 in
match rest with
| "" -> acc
| rest ->
let terminal_command = output_terminal_command terminal_command in
output_newline_and_indent acc;
begin match String.index rest '\n' with
| exception Not_found -> output_string stdout rest; (terminal_command,None)
| i ->
let fst = String.sub rest 0 i in
let snd = String.sub rest (i + 1) ((String.length rest) - i - 1) in
assert(fst <> "");
output_string stdout fst;
let acc = (terminal_command,None) in
let acc = do_symbolic_output_item acc Format.Output_newline in
let acc = if snd = "" then acc else do_symbolic_output_item acc (Format.Output_string snd) in
acc
end
and parse_for_empty_beginning acc string idx =
match String.get string idx with
| exception Invalid_argument _ -> acc, ""
| ' ' -> parse_for_empty_beginning (do_symbolic_output_item acc (Format.Output_spaces 1)) string (idx + 1)
| '\n' -> parse_for_empty_beginning (do_symbolic_output_item acc (Format.Output_newline)) string (idx + 1)
| '\027' ->
parse_terminal acc string idx
| _ ->
acc, String.sub string idx (String.length string - idx)
and parse_terminal (oldtc,newline_and_indent) string start_idx =
assert (String.get string (start_idx + 1) == '[');
let rec loop : int -> _ * _ = fun idx -> match String.get string idx with
| ';' | '0' | '1' | '2'| '3'| '4'| '5'| '6'| '7'| '8'| '9' -> loop @@ idx + 1
| 'm' -> (String.sub string start_idx (1 + idx - start_idx)), idx + 1
| c ->
Printf.printf "Char is %c" c;
assert false
in
let new_terminal_command,idx = loop (start_idx + 2) in
let newtc = match oldtc with
| (true, x) when x = new_terminal_command -> oldtc
| _ -> (false,new_terminal_command)
in
parse_for_empty_beginning (newtc,newline_and_indent) string idx
in
let (terminal_command,_newline_and_indent) = List.fold_left do_symbolic_output_item ((true,AnsiSgrCodes.reset_code),None) symbolic_item_list in
output_string stdout "\n";
let _ = output_terminal_command terminal_command in
()
in
Format.kfprintf k ppf formatstring
;;