2 to_string : unit -> string;
3 to_html : unit -> string
6 let sepx = "\xe2\xbf\x96";;
7 let endx = "\xe2\xbf\x97";;
9 let socket_name = "/tmp/fancy.log";;
11 let html_enabled = Sys.file_exists socket_name;;
13 let socket = let open Unix in
15 then socket PF_UNIX SOCK_STREAM 0
16 else socket PF_INET SOCK_STREAM 0;;
18 let html_enabled = if html_enabled then
20 let _ = Unix.connect socket (Unix.ADDR_UNIX socket_name) in true
21 with Unix.Unix_error _ -> false
26 let process_output_to_list2 = fun command ->
27 let chan = Unix.open_process_in command in
28 let res = ref ([] : string list) in
29 let rec process_otl_aux () =
30 let e = input_line chan in
33 try process_otl_aux ()
35 let stat = Unix.close_process_in chan in (List.rev !res,stat)
36 in let cmd_to_list command =
37 let (l,_) = process_output_to_list2 command in l
38 in try let lines = cmd_to_list "tput cols" in int_of_string (List.hd (lines))
39 with _ -> 100 (* default value *)
43 let _ = Unix.send socket (Bytes.of_string s) 0 (String.length s) [] in ()
46 let concat ls = (String.concat sepx ls) ^ endx;;
50 writeall(concat["helo"; String.concat " " (Array.to_list Sys.argv)])
55 if html_enabled then (
56 let strs = (List.map (fun x -> x#to_html()) objs) in
57 writeall (concat ("log" :: strs))
58 ); prerr_endline (String.concat " " (List.map (fun x -> x#to_string()) objs))
62 let m = [("&", "&"); (">", ">"); ("<", "<"); (""", "\""); ("'", "'")]
63 in let m = List.map (fun (x,y) -> Str.regexp x, y) m
64 in List.fold_right (fun (x,y) z -> Str.global_replace x y z) m s;; (* FIXME TODO *)
66 let fancy_of_string s : fancyobj = object
67 method to_string () = s
68 method to_html () = html_escape s
71 let empty = fancy_of_string "";;
73 let line = ref empty;;
76 method to_string () = a
81 method to_string () = (a#to_string () ^ "" ^ b#to_string ())
82 method to_html () = a#to_html () ^ " " ^ b#to_html ()
85 (* Output functions on standard output *)
88 line := !line ^^ fancy_of_string s
92 print_string (String.make 1 c)
95 (* let print_bytes : bytes -> unit *)
98 print_string (string_of_int n)
101 (* val print_float : float -> unit *)
103 let print_newline () =
104 if !line <> empty then (
105 Pervasives.print_endline (!line#to_string());
106 if html_enabled then (writeall (concat ["log"; !line#to_html()]));
115 let print_string_endline f =
116 print (fancy_of_string f); print_newline ()
119 let print_endline f =
120 print f; print_newline ()
125 print_endline ( String.make cols '-' / "<hr>")
128 let print_heading s =
130 print_endline (("# " ^ s) / ("<h2>" ^ html_escape s ^ "</h2>"))
135 print_endline (("- " / "<ul><li>") ^^ f ^^ ("" / "</ul>"))
139 print_endline ( s / "" );
140 if html_enabled then (writeall (concat ["math"; s]))