(** * ANSITerminal.ml * ColorML_v2 project * (C) Lilian Besson, 2014 *) let autoreset = ref true let set_autoreset b = autoreset := b type color = Black | Red | Green | Yellow | Blue | Magenta | Cyan | White | Default type style = | Reset | Bold | Underlined | Blink | Inverse | Hidden | Foreground of color | Background of color let black = Foreground Black let red = Foreground Red let green = Foreground Green let yellow = Foreground Yellow let blue = Foreground Blue let magenta = Foreground Magenta let cyan = Foreground Cyan let white = Foreground White let default = Foreground Default let on_black = Background Black let on_red = Background Red let on_green = Background Green let on_yellow = Background Yellow let on_blue = Background Blue let on_magenta = Background Magenta let on_cyan = Background Cyan let on_white = Background White let on_default = Background Default open Printf open Scanf 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 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 Default -> "49" let ( ^= ) s f = s := (!s) ^ (f);; let print_to_string style txt = let ss = ref "" in ss ^= "\027["; let s = String.concat ";" (List.map style_to_string style) in ss ^= s; ss ^= "m"; ss ^= txt; if !autoreset then ss ^= "\027[0m"; !ss let print_string_style style txt = print_string "\027["; let s = String.concat ";" (List.map style_to_string style) in print_string s; print_string "m"; print_string txt; if !autoreset then print_string "\027[0m" let print_string = print_string_style let prerr_string_style style txt = prerr_string "\027["; let s = String.concat ";" (List.map style_to_string style) in prerr_string s; prerr_string "m"; prerr_string txt; if !autoreset then prerr_string "\027[0m" let prerr_string = prerr_string_style let printf style = ksprintf (print_string_style style)