Skip to content
This repository
tag: v1887
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 97 lines (82 sloc) 2.349 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
(*
Copyright © 2011 MLstate

This file is part of OPA.

OPA is free software: you can redistribute it and/or modify it under the
terms of the GNU Affero General Public License, version 3, as published by
the Free Software Foundation.

OPA is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
more details.

You should have received a copy of the GNU Affero General Public License
along with OPA. If not, see <http://www.gnu.org/licenses/>.
*)

type color =
    [ `black
    | `red
    | `green
    | `yellow
    | `blue
    | `magenta
    | `cyan
    | `white ]

let color = function
  | `black -> 0
  | `red -> 1
  | `green -> 2
  | `yellow -> 3
  | `blue -> 4
  | `magenta -> 5
  | `cyan -> 6
  | `white -> 7

let foreground c = 30 + color c
let background c = 40 + color c

let open_color_code c = Printf.sprintf "\027[%dm" (foreground c)
let close_color_code = "\027[0m"

(** all call to produce color come from here,
so this global propertie is here to enforce that
no any production are in color *)
let set_ignore_color, get_ignore_color =
  let __ignore_color = ref false in
  (fun b -> __ignore_color := b), (fun () -> !__ignore_color)

let print =
  if Sys.os_type = "Win32" then
    fun (_:color) s -> s
  else
    fun (c:color) s ->
      if get_ignore_color () then s else
        match c with
        | `black -> s
        | c -> Printf.sprintf "%s%s%s" (open_color_code c) s close_color_code

let string_of_color = function
  | `black -> "black"
  | `red -> "red"
  | `green -> "green"
  | `yellow -> "yellow"
  | `blue -> "blue"
  | `magenta -> "magenta"
  | `cyan -> "cyan"
  | `white -> "white"

let color_of_string = function
  | "black" -> Some `black
  | "red" -> Some `red
  | "green" -> Some `green
  | "yellow" -> Some `yellow
  | "blue" -> Some `blue
  | "magenta" -> Some `magenta
  | "cyan" -> Some `cyan
  | "white" -> Some `white
  | _ -> None

let symbols = [
  "black"; "red"; "green"; "yellow"; "blue"; "magenta"; "cyan"; "white"
]

let uncolor = function
  | 0 -> `black
  | 1 -> `red
  | 2 -> `green
  | 3 -> `yellow
  | 4 -> `blue
  | 5 -> `magenta
  | 6 -> `cyan
  | 7 -> `white
  | _ -> assert false
Something went wrong with that request. Please try again.