package spin

  1. Overview
  2. Docs

Source file ansi.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
#1 "./ansi_unix.ml"
(* File: Ansi_unix.ml

   Allow colors, cursor movements, erasing,... under Unix shells.
   *********************************************************************

   Copyright 2004 by Troestler Christophe Christophe.Troestler(at)umons.ac.be

   This library is free software; you can redistribute it and/or modify it under
   the terms of the GNU Lesser General Public License version 3 as published by
   the Free Software Foundation, with the special exception on linking described
   in file LICENSE.

   This library is distributed in the hope that it will be useful, but WITHOUT
   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
   FOR A PARTICULAR PURPOSE. See the file LICENSE for more details. *)

(* man tty(4) *)

open Printf
open Scanf
include Ansi_common

let isatty = ref Unix.isatty

let is_out_channel_atty ch = !isatty (Unix.descr_of_out_channel ch)

(* Cursor *)

let set_cursor x y =
  if is_out_channel_atty stdout then
    if x <= 0 then (
      if y > 0 then
        printf "\027[%id%!" y)
    else if (* x > 0 *) y <= 0 then
      printf "\027[%iG%!" x
    else
      printf "\027[%i;%iH%!" y x

let move_cursor x y =
  if is_out_channel_atty stdout then (
    if x > 0 then
      printf "\027[%iC%!" x
    else if x < 0 then
      printf "\027[%iD%!" (-x);
    if y > 0 then
      printf "\027[%iB%!" y
    else if y < 0 then
      printf "\027[%iA%!" (-y))

let save_cursor () = if is_out_channel_atty stdout then printf "\027[s%!"

let restore_cursor () = if is_out_channel_atty stdout then printf "\027[u%!"

let show_cursor () = if is_out_channel_atty stdout then printf "\027[?25h"

let hide_cursor () = if is_out_channel_atty stdout then printf "\027[?25l"

let move_bol () =
  print_string "\r";
  flush stdout

(* Inpired by http://www.ohse.de/uwe/software/resize.c.html and
   http://qemacs.sourcearchive.com/documentation/0.3.1.cvs.20050713-5/tty_8c-source.html *)
let send_and_read_response fdin query fmt f =
  let alarm = ref false in
  let set_alarm (_ : int) = alarm := true in
  let old_alarm = Sys.signal Sys.sigalrm (Sys.Signal_handle set_alarm) in
  let tty = Unix.tcgetattr fdin in
  Unix.tcsetattr
    fdin
    Unix.TCSANOW
    { tty with
      Unix.c_ignbrk = false
    ; c_brkint = false
    ; c_parmrk = false
    ; c_istrip = false
    ; c_inlcr = false
    ; c_igncr = false
    ; c_icrnl = false
    ; c_ixon = false
    ; c_opost = true
    ; c_csize = 8
    ; c_parenb = false
    ; c_icanon = false
    ; c_isig = false
    ; c_echo = false
    ; c_echonl = false
    ; c_vmin = 1
    ; c_vtime = 0
    };
  let restore () =
    ignore (Unix.alarm 0);
    Unix.tcsetattr fdin Unix.TCSANOW tty;
    Sys.set_signal Sys.sigalrm old_alarm
  in
  let buf = Bytes.make 127 '\000' in
  (* FIXME: make it more robust so that it ignores previous key pressed. *)
  let rec get_answer pos =
    let l = Unix.read fdin buf pos 1 in
    let buf = Bytes.unsafe_to_string buf in
    (* local use only *)
    try sscanf buf fmt f (* bail out as soon as enough info is present *) with
    | Scan_failure _ ->
      if !alarm || pos = 126 then
        failwith "Ansi.input_answer"
      else if buf.[pos] = '\000' then
        get_answer pos
      else
        get_answer (pos + l)
  in
  try
    ignore (Unix.write fdin query 0 (Bytes.length query));
    ignore (Unix.alarm 1);
    let r = get_answer 0 in
    restore ();
    r
  with
  | e ->
    restore ();
    raise e

(* Query Cursor Position	<ESC>[6n *)
(* Report Cursor Position	<ESC>[{ROW};{COLUMN}R *)
let pos_cursor_query = Bytes.of_string "\027[6n"

let pos_cursor () =
  if is_out_channel_atty stdout then
    try
      send_and_read_response
        Unix.stdin
        pos_cursor_query
        "\027[%d;%dR"
        (fun y x -> x, y)
    with
    | _ ->
      failwith "Ansi.pos_cursor"
  else
    failwith "Ansi.pos_cursor: not a TTY"

(* See also the output of 'resize -s x y' (e.g. in an Emacs shell). *)
let resize width height =
  if is_out_channel_atty stdout then (
    if width <= 0 then invalid_arg "Ansi.resize: width <= 0";
    if height <= 0 then invalid_arg "Ansi.resize: height <= 0";
    printf "\027[8;%i;%it%!" height width)

(* FIXME: what about the following recipe: If you run echo -e "\e[18t" then
   xterm will respond with a line of the form ESC [ 8 ; height ; width t It
   generates this line as if it were typed input, so it can then be read by your
   program on stdin. *)
external size_ : Unix.file_descr -> int * int = "Ansi_term_size"

let size () =
  if !isatty Unix.stdin then
    size_ Unix.stdin
  else
    failwith "Ansi.size: not a TTY"

(* Erasing *)

let erase loc =
  if is_out_channel_atty stdout then (
    print_string
      (match loc with
      | Eol ->
        "\027[K"
      | Above ->
        "\027[1J"
      | Below ->
        "\027[0J"
      | Screen ->
        "\027[2J");
    flush stdout)

(* Scrolling *)

let scroll lines =
  if is_out_channel_atty stdout then
    if lines > 0 then
      printf "\027[%iS%!" lines
    else if lines < 0 then
      printf "\027[%iT%!" (-lines)

let style_to_string = function
  | Reset ->
    "0"
  | Bold ->
    "1"
  | Underlined ->
    "4"
  | Blink ->
    "5"
  | Inverse ->
    "7"
  | Hidden ->
    "8"
  | Foreground Black ->
    "30"
  | Foreground Red ->
    "31"
  | Foreground Green ->
    "32"
  | Foreground Yellow ->
    "33"
  | Foreground Blue ->
    "34"
  | Foreground Magenta ->
    "35"
  | Foreground Cyan ->
    "36"
  | Foreground White ->
    "37"
  | Foreground Bright_black ->
    "30;1"
  | Foreground Bright_red ->
    "31;1"
  | Foreground Bright_green ->
    "32;1"
  | Foreground Bright_yellow ->
    "33;1"
  | Foreground Bright_blue ->
    "34;1"
  | Foreground Bright_magenta ->
    "35;1"
  | Foreground Bright_cyan ->
    "36;1"
  | Foreground Bright_white ->
    "37;1"
  | Foreground Default ->
    "39"
  | Background Black ->
    "40"
  | Background Red ->
    "41"
  | Background Green ->
    "42"
  | Background Yellow ->
    "43"
  | Background Blue ->
    "44"
  | Background Magenta ->
    "45"
  | Background Cyan ->
    "46"
  | Background White ->
    "47"
  | Background Bright_black ->
    "40;1"
  | Background Bright_red ->
    "41;1"
  | Background Bright_green ->
    "42;1"
  | Background Bright_yellow ->
    "43;1"
  | Background Bright_blue ->
    "44;1"
  | Background Bright_magenta ->
    "45;1"
  | Background Bright_cyan ->
    "46;1"
  | Background Bright_white ->
    "47;1"
  | Background Default ->
    "49"

let print_with pr ~tty style txt =
  if tty then (
    pr "\027[";
    pr (String.concat ";" (List.map style_to_string style));
    pr "m");
  pr txt;
  if tty && !autoreset then pr "\027[0m"

let print_string style txt =
  print_with print_string style txt ~tty:(is_out_channel_atty stdout)

let prerr_string style txt =
  print_with prerr_string style txt ~tty:(is_out_channel_atty stderr)

let printf style = ksprintf (print_string style)

let eprintf style = ksprintf (prerr_string style)

let to_string style txt =
  let s =
    "\027[" ^ String.concat ";" (List.map style_to_string style) ^ "m" ^ txt
  in
  if !autoreset then s ^ "\027[0m" else s

let sprintf style = ksprintf (to_string style)
OCaml

Innovation. Community. Security.