diff --git a/src/core/CCFormat.ml b/src/core/CCFormat.ml index bdb425d6..58b1636a 100644 --- a/src/core/CCFormat.ml +++ b/src/core/CCFormat.ml @@ -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 diff --git a/src/core/CCFormat.mli b/src/core/CCFormat.mli index bb7279d6..95a53ad6 100644 --- a/src/core/CCFormat.mli +++ b/src/core/CCFormat.mli @@ -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