Source

ocaml-stringpainter / src / ANSI_Colors.ml

Full commit
(*********************************************************************************************************************
 * Copyrights (C) by
 *  Pawel Wieczorek <wieczyk gmail>
 *
 * http://bitbucket.org/wieczyk/ocaml-stringpainter
 ********************************************************************************************************************)


(*********************************************************************************************************************
 * ANSI escapes codes
 ********************************************************************************************************************)

(** ANSI color *)
type color
    = Black
    | Red
    | Green
    | Yellow
    | Blue
    | Magenta
    | Cyan
    | White

let code_of_color = function
    | Black ->   0
    | Red ->     1
    | Green ->   2
    | Yellow ->  3
    | Blue ->    4
    | Magenta -> 5
    | Cyan ->    6
    | White ->   7

type attribute
    = Bright
    | Reverse
    | Underline
    | Blink
    | Background of color
    | Foreground of color

let code_of_attribute = function
    | Bright -> 1
    | Reverse -> 7
    | Underline -> 4
    | Blink     -> 5
    | Background color -> 40 + code_of_color color
    | Foreground color -> 30 + code_of_color color

let escape codes str =
    let f = fun str c -> str ^ ";" ^ string_of_int (code_of_attribute c) in
    (List.fold_left f "\027[" codes) ^ "m" ^ str ^ "\027[0m"