basic ANSI codes for colors in CCFormat

This commit is contained in:
Simon Cruanes 2015-12-02 15:31:52 +01:00
parent 1d6cf2c683
commit fdfc106cad
2 changed files with 55 additions and 0 deletions

View file

@ -159,3 +159,34 @@ let _with_file_out filename f =
let to_file filename format =
_with_file_out filename (fun fmt -> Format.fprintf fmt format)
type color =
[ `Black
| `Red
| `Yellow
| `Green
| `Blue
| `Magenta
| `Cyan
| `White
]
let int_of_color_ = function
| `Black -> 0
| `Red -> 1
| `Green -> 2
| `Yellow -> 3
| `Blue -> 4
| `Magenta -> 5
| `Cyan -> 6
| `White -> 7
(* same as [pp], but in color [c] *)
let color_str c out s =
let n = int_of_color_ c in
Format.fprintf out "\x1b[3%dm%s\x1b[0m" n s
(* same as [pp], but in bold color [c] *)
let bold_str c out s =
let n = int_of_color_ c in
Format.fprintf out "\x1b[3%d;1m%s\x1b[0m" n s

View file

@ -66,6 +66,30 @@ val quad : 'a printer -> 'b printer -> 'c printer -> 'd printer -> ('a * 'b * 'c
val map : ('a -> 'b) -> 'b printer -> 'a printer
(** {2 ASCII codes}
Use ANSI escape codes https://en.wikipedia.org/wiki/ANSI_escape_code
to put some colors on the terminal.
We only allow styling of constant strings, because nesting is almost
impossible with ANSI codes (unless we maintain a stack of codes explicitely).
@since NEXT_RELEASE *)
type color =
[ `Black
| `Red
| `Yellow
| `Green
| `Blue
| `Magenta
| `Cyan
| `White
]
val color_str : color -> string printer
val bold_str : color -> string printer
(** {2 IO} *)
val output : t -> 'a printer -> 'a -> unit