1 (* File: ANSITerminal.ml
2 Allow colors, cursor movements, erasing,... under Unix and DOS shells.
3 *********************************************************************
5 Copyright 2004 by Troestler Christophe
6 Christophe.Troestler(at)umh.ac.be
8 This library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Lesser General Public License
10 version 2.1 as published by the Free Software Foundation, with the
11 special exception on linking described in file LICENSE.
13 This library is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the file
16 LICENSE for more details.
18 (** See the file ctlseqs.html (unix)
19 and (for DOS) http://www.ka.net/jmenees/Dos/Ansi.htm
27 type loc
= Above
| Below
| Screen
30 | Above
-> print_string
"\027[1J"
31 | Below
-> print_string
"\027[0J"
32 | Screen
-> print_string
"\027[2J"
38 if x
<= 0 then (if y
> 0 then printf
"\027[%id" y
)
39 else (* x > 0 *) if y
<= 0 then printf
"\027[%iG" x
40 else printf
"\027[%i;%iH" y x
43 if x
> 0 then printf
"\027[%iC" x
44 else if x
< 0 then printf
"\027[%iD" (-x
);
45 if y
> 0 then printf
"\027[%iB" y
46 else if y
< 0 then printf
"\027[%iA" (-y
)
48 let save_cursor () = print_string
"\027[s"
49 let restore_cursor () = print_string
"\027[u"
54 if lines
> 0 then printf
"\027[%iS" lines
55 else if lines
< 0 then printf
"\027[%iT" (- lines
)
59 let autoreset = ref true
61 let set_autoreset b
= autoreset := b
65 Black
| Red
| Green
| Yellow
| Blue
| Magenta
| Cyan
| White
| Default
68 | Reset
| Bold
| Underlined
| Blink
| Inverse
| Hidden
72 let black = Foreground Black
73 let red = Foreground Red
74 let green = Foreground Green
75 let yellow = Foreground Yellow
76 let blue = Foreground Blue
77 let magenta = Foreground Magenta
78 let cyan = Foreground Cyan
79 let white = Foreground White
80 let default = Foreground Default
82 let on_black = Background Black
83 let on_red = Background Red
84 let on_green = Background Green
85 let on_yellow = Background Yellow
86 let on_blue = Background Blue
87 let on_magenta = Background Magenta
88 let on_cyan = Background Cyan
89 let on_white = Background White
90 let on_default = Background Default
92 let style_to_string = function
99 | Foreground Black
-> "30"
100 | Foreground Red
-> "31"
101 | Foreground Green
-> "32"
102 | Foreground Yellow
-> "33"
103 | Foreground Blue
-> "34"
104 | Foreground Magenta
-> "35"
105 | Foreground Cyan
-> "36"
106 | Foreground White
-> "37"
107 | Foreground Default
-> "39"
108 | Background Black
-> "40"
109 | Background Red
-> "41"
110 | Background Green
-> "42"
111 | Background Yellow
-> "43"
112 | Background Blue
-> "44"
113 | Background Magenta
-> "45"
114 | Background Cyan
-> "46"
115 | Background White
-> "47"
116 | Background Default
-> "49"
119 let print_string style txt
=
120 print_string "\027[";
121 let s = String.concat
";" (List.map
style_to_string style
) in
125 if !autoreset then print_string "\027[0m"
128 let printf style
= kprintf
(print_string style
)
132 (* On DOS & windows, to enable the ANSI sequences, ANSI.SYS should be
133 loaded in C:\CONFIG.SYS with a line of the type
135 DEVICE = C:\DOS\ANSI.SYS
136 DEVICEHIGH=C:\WINDOWS\COMMAND\ANSI.SYS
138 This routine checks whether the line is present and, if not, it
139 inserts it and tells the user to reboot.
141 On WINNT, one will create a ANSI.NT in the user dir and a
142 command.com link on the desktop (with Configfilename = our ANSI.NT)
143 and tell the user to use it.
145 REM: that does NOT work under winxp because OCaml programs are not
146 considered to run in DOS mode only...
148 http://support.microsoft.com/default.aspx?scid=kb;en-us;816179
149 http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/console_functions.asp
153 (* let is_readable file = *)
154 (* try close_in(open_in file); true *)
155 (* with Sys_error _ -> false *)
157 (* let config_sys = "C:\\CONFIG.SYS" *)
161 (* (\* Locate ANSI.SYS *\) *)
162 (* let ansi_sys = List.find is_readable [ *)
163 (* "C:\\DOS\\ANSI.SYS"; *)
164 (* "C:\\WINDOWS\\COMMAND\\ANSI.SYS"; ] in *)
165 (* (\* Parse CONFIG.SYS to see wether it has the right line *\) *)
167 (* let re = Str.regexp_case_fold *)
168 (* ("^DEVICE\\(HIGH\\)?[ \t]*=[ \t]*" ^ ansi_sys ^ "[ \t]*$") in *)
169 (* let fh = open_in config_sys in *)
172 (* if Str.string_match re (input_line fh) 0 then raise OK *)
175 (* | End_of_file -> *)
176 (* (\* Correct line not found: add it *\) *)
178 (* raise(Sys_error "win9x") *)
179 (* | OK -> close_in fh (\* Correct line found, keep going *\) *)
181 (* with Sys_error _ -> *)
182 (* (\* config_sys not does not exists or does not contain the right line. *\) *)
183 (* let fh = open_out_gen [Open_wronly; Open_append; Open_creat; Open_text] *)
184 (* 0x777 config_sys in *)
185 (* output_string fh ("DEVICEHIGH=" ^ ansi_sys ^ "\n"); *)
187 (* prerr_endline "Please restart your computer and rerun the program."; *)
192 (* let winnt home = *)
193 (* (\* Locate ANSI.SYS *\) *)
195 (* try Sys.getenv "SystemRoot" *)
196 (* with Not_found -> "C:\\WINDOWS" in *)
198 (* List.find is_readable (List.map (fun s -> Filename.concat system s) *)
199 (* [ "SYSTEM32\\ANSI.SYS"; ]) in *)
200 (* (\* Create an ANSI.SYS file in the user dir *\) *)
201 (* let ansi_nt = Filename.concat home "ANSI.NT" in *)
202 (* let fh = open_out ansi_nt in *)
203 (* output_string fh "dosonly\ndevice="; *)
204 (* output_string fh ansi_sys; *)
205 (* output_string fh "\ndevice=%SystemRoot%\\system32\\himem.sys *)
210 (* (\* Make a command.com link on the desktop *\) *)
211 (* let fh = open_out (Filename.concat home "command.lnk") in *)
216 (* if Sys.os_type = "Win32" then begin *)
217 (* try winnt(Sys.getenv "USERPROFILE") (\* WinNT, Win2000, WinXP *\) *)
218 (* with Not_found -> win9x() (\* Win9x *\) *)