=== modified file 'ppio.ml' --- ppio.ml 2019-04-13 08:40:40 +0000 +++ ppio.ml 2019-04-13 01:47:59 +0000 @@ -61,67 +61,39 @@ let pp_writer hideendl endl colors = let s = {to_print = Buffer.create 80; dm = NoFormat} in - let add_char = Buffer.add_char s.to_print - and add_string = Buffer.add_string s.to_print in - let add_escaped c = - match c with - | '\x00' -> add_string "\\0" - | '\t' -> add_string "\\t" - | '\n' -> add_string "\\n" - | '\r' -> add_string "\\r" - | _ -> bprintf s.to_print "\\x%02x" (Char.to_int c); - in let write_out () = if Buffer.length s.to_print > 0 then ( match s.dm with NoFormat | _ -> - add_string "\x1b[0m"; + Buffer.add_string s.to_print "\x1b[0m"; Out_channel.output_buffer stderr s.to_print; Out_channel.flush stderr; Buffer.clear s.to_print; s.dm <- NoFormat; ) - and pp_char = match colors with + and add_char = match colors with | None -> fun c -> ( if c >= ' ' && c <= '~' then ( - add_char c; + Buffer.add_char s.to_print c; ) else ( - add_escaped c; + bprintf s.to_print "\\x%02x" (Char.to_int c); ) ) | Some {normal; escaped} -> fun c -> ( if c >= ' ' && c <= '~' then ( - add_string "\x1b["; - add_string normal; - add_char 'm'; - add_char c; + Buffer.add_string s.to_print "\x1b["; + Buffer.add_string s.to_print normal; + Buffer.add_char s.to_print 'm'; + Buffer.add_char s.to_print c; ) else ( - bprintf s.to_print "\x1b[%sm" escaped; - add_escaped c; + bprintf s.to_print "\x1b[%sm\\x%02x" escaped (Char.to_int c); ) ) in - match (String.length endl, hideendl) with - | (0, _) -> fun (len:int) (buffer:Bytes.t) -> ( + fun (len:int) (buffer:Bytes.t) -> ( for n = 0 to len - 1 do - pp_char (Bytes.get buffer n); + add_char (Bytes.get buffer n); done; write_out ()) - | (1, false) -> (let endl = String.get endl 0 in - fun (len:int) (buffer:Bytes.t) -> ( - for n = 0 to len - 1 do - let c = Bytes.get buffer n in - pp_char c; - if c = endl then add_char '\n' - done; - write_out ())) - | (1, true) -> (let endl = String.get endl 0 in - fun (len:int) (buffer:Bytes.t) -> ( - for n = 0 to len - 1 do - let c = Bytes.get buffer n in - if c = endl then add_char '\n' else pp_char c; - done; - write_out ())) - | _ -> failwith "Not implemented" ;; (* Copy data from the reader to the writer, using the provided buffer