Repository navigation
Expand file tree
/
Copy pathtty.ml
More file actions
145 lines (128 loc) · 3.81 KB
/
Copy pathtty.ml
File metadata and controls
145 lines (128 loc) · 3.81 KB
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
(**
* Copyright (c) 2015, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the "hack" directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*)
open Core
type raw_color =
| Default
| Black
| Red
| Green
| Yellow
| Blue
| Magenta
| Cyan
| White
type style =
| Normal of raw_color
| Bold of raw_color
| Dim of raw_color
| Underline of raw_color
| BoldUnderline of raw_color
| DimUnderline of raw_color
| NormalWithBG of raw_color * raw_color
| BoldWithBG of raw_color * raw_color
type color_mode =
| Color_Always
| Color_Never
| Color_Auto
let text_num = function
| Default -> "39"
| Black -> "30"
| Red -> "31"
| Green -> "32"
| Yellow -> "33"
| Blue -> "34"
| Magenta -> "35"
| Cyan -> "36"
| White -> "37"
let background_num = function
| Default -> "49"
| Black -> "40"
| Red -> "41"
| Green -> "42"
| Yellow -> "43"
| Blue -> "44"
| Magenta -> "45"
| Cyan -> "46"
| White -> "47"
let color_num = function
| Default -> "0"
| x -> text_num x
let style_num = function
| Normal c -> color_num c
| Bold c -> color_num c ^ ";1"
| Dim c -> color_num c ^ ";2"
| Underline c -> color_num c ^ ";4"
| BoldUnderline c -> color_num c ^ ";1;4"
| DimUnderline c -> color_num c ^ ";2;4"
| NormalWithBG (text, bg) -> (text_num text) ^ ";" ^ (background_num bg)
| BoldWithBG (text, bg) -> (text_num text) ^ ";" ^ (background_num bg) ^ ";1"
let print_one ?(color_mode=Color_Auto) ?(out_channel=stdout) c s =
let should_color = match color_mode with
| Color_Always -> true
| Color_Never -> false
| Color_Auto ->
begin
match Sys_utils.getenv_term () with
| None -> false
| Some term ->
Unix.isatty Unix.stdout && term <> "dumb"
end in
if should_color
then Printf.fprintf out_channel "\x1b[%sm%s\x1b[0m" (style_num c) (s)
else Printf.fprintf out_channel "%s" s
let cprint ?(color_mode=Color_Auto) ?(out_channel=stdout) strs =
List.iter strs (fun (c, s) -> print_one ~color_mode ~out_channel c s)
let cprintf ?(color_mode=Color_Auto) ?(out_channel=stdout) c =
Printf.ksprintf (print_one ~color_mode ~out_channel c)
let (spinner, spinner_used) =
let state = ref 0 in
(fun () ->
begin
let str = List.nth_exn ["-"; "\\"; "|"; "/"] (!state mod 4) in
state := !state + 1;
str
end),
(fun () -> !state <> 0)
(* ANSI escape sequence to clear whole line *)
let clear_line_seq = "\r\x1b[0K"
let print_clear_line chan =
if Unix.isatty (Unix.descr_of_out_channel chan)
then Printf.fprintf chan "%s%!" clear_line_seq
else ()
(* Read a single char and return immediately, without waiting for a newline.
* `man termios` to see how termio works. *)
let read_char () =
let tty = Unix.(openfile "/dev/tty" [O_RDWR] 0o777) in
let termio = Unix.tcgetattr tty in
let new_termio = {termio with Unix.
c_icanon = false;
c_vmin = 1;
c_vtime = 0;
} in
Unix.tcsetattr tty Unix.TCSANOW new_termio;
let buf = String.create 1 in
let bytes_read = UnixLabels.read tty ~buf ~pos:0 ~len:1 in
Unix.tcsetattr tty Unix.TCSANOW termio;
assert (bytes_read = 1);
buf.[0]
(* Prompt the user to pick one character out of a given list. If other
* characters are entered, the prompt repeats indefinitely. *)
let read_choice message choices =
let rec loop () =
Printf.printf "%s (%s)%!" message
(String.concat "|" (List.map choices String_utils.string_of_char));
let choice = read_char () in
print_newline ();
if List.mem choices choice then choice else loop ()
in loop ()
let eprintf fmt =
if Unix.(isatty stderr)
then Printf.eprintf fmt
else Printf.ifprintf stderr fmt