-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpretty.ml
150 lines (119 loc) · 3.89 KB
/
pretty.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
open Pretty_types
let pp_fg_colour = function
| BLACK -> "30"
| RED -> "31"
| GREEN -> "32"
| YELLOW -> "33"
| BLUE -> "34"
| MAGENTA -> "35"
| CYAN -> "36"
| WHITE -> "37"
let pp_bg_colour = function
| BLACK -> "40"
| RED -> "41"
| GREEN -> "42"
| YELLOW -> "43"
| BLUE -> "44"
| MAGENTA -> "45"
| CYAN -> "46"
| WHITE -> "47"
let pp_attr = function
| RESET -> "0"
| BRIGHT -> "1"
| DIM -> "2"
| UNDERSCORE -> "3"
| BLINK -> "4"
| REVERSE -> "5"
| HIDDEN -> "6"
let pp_attribute = function
| Attribute attr -> pp_attr attr
| Foreground colour -> pp_fg_colour colour
| Background colour -> pp_bg_colour colour
| String s -> (* should not be used in this context *)
raise (Invalid_argument "pp_attribute: String")
let set_fg colour =
print_string ("\027[" ^ (pp_fg_colour colour) ^ "m")
let set_bg colour =
print_string ("\027[" ^ (pp_bg_colour colour) ^ "m")
let reset_attrs () =
print_string "\027[0m"
let clear_screen () =
print_string "\027[2J\027[H"
let clear_to_eol () =
print_string "\027[K"
let clear_to_sol () =
print_string "\027[1K"
let clear_line () =
print_string "\027[2K"
let clear_down () =
print_string "\027[J"
let clear_up () =
print_string "\027[1J"
let scroll_screen ?(pos = (-1, -1)) () =
print_string
(if pos = (-1, -1) then
"\027[r"
else
let x = string_of_int (fst pos) and y = string_of_int (snd pos)
in "\027[" ^ y ^ ";" ^ x ^ "r")
let scroll_down () =
print_string "\027D"
let scroll_up () =
print_string "\027M"
let move_xy ?(x = 0) ?(y = 0) () =
print_string ("\027[" ^ (string_of_int y) ^ ";" ^ (string_of_int x) ^ "H")
let move_up ?(amount = 1) () =
print_string ("\027[" ^ (string_of_int amount) ^ "A")
let move_down ?(amount = 1) () =
print_string ("\027[" ^ (string_of_int amount) ^ "B")
let move_forward ?(amount = 1) () =
print_string ("\027[" ^ (string_of_int amount) ^ "C")
let move_backward ?(amount = 1) () =
print_string ("\027[" ^ (string_of_int amount) ^ "D")
let move_save () = print_string "\027[s"
let move_unsave () = print_string "\027[u"
let move_save_attr () = print_string "\0277"
let move_restore_attr () = print_string "\0278"
let print_string_attrs ?(attrs = []) str =
let rec build_attrs = function
| [] -> ""
| [atr] -> (pp_attribute atr) ^ "m"
| atr :: xs -> (pp_attribute atr) ^ ";" ^ build_attrs xs
in
print_string (
(if attrs = [] then ""
else ("\027[" ^ (build_attrs attrs))) ^ str ^ "\027[0m")
let print_custom output =
let rec build_attrs = function
| [] -> ""
| [atr] -> (pp_attribute atr) ^ "m"
| atr :: xs -> (pp_attribute atr) ^ ";" ^ build_attrs xs in
let rec inner fmt = function
| [] -> ""
| x :: xs ->
(match x with
| Attribute _ | Foreground _ | Background _ ->
inner (x :: fmt) xs
| String s ->
(if fmt = [] then
s
else ("\027[" ^ (build_attrs fmt) ^ s)) ^ inner [] xs)
in
print_string ((inner [] output) ^ "\027[0m")
let print_string ?(attrs = []) ?(x = -1) ?(y = -1) str =
(if x <> -1 || y <> -1 then
let x = if x <> -1 then string_of_int x else ""
and y = if y <> -1 then string_of_int y else ""
in print_string ("\027[" ^ y ^ ";" ^ x ^ "H"));
print_string_attrs ~attrs:attrs str
let noecho_aux fd f =
let open Unix in
let attrs = tcgetattr stdin in
tcsetattr fd TCSANOW { attrs with c_echo = false };
let ret = try f fd with
| ex -> tcsetattr fd TCSANOW attrs; raise ex
in
tcsetattr fd TCSANOW attrs; ret
let noecho_read_line () = noecho_aux Unix.stdin (fun fd -> read_line ())
let noecho_read_int () = noecho_aux Unix.stdin (fun fd -> read_int ())
let noecho_read_float () = noecho_aux Unix.stdin (fun fd -> read_float ())