Skip to content

Instantly share code, notes, and snippets.

@hellojukay
Last active August 14, 2021 03:57
Show Gist options
  • Save hellojukay/808262b788a86607cc788d747c8f87de to your computer and use it in GitHub Desktop.
Save hellojukay/808262b788a86607cc788d747c8f87de to your computer and use it in GitHub Desktop.
OCaml print with color
module Log = struct
type color = Black | Red | Green | Yellow | Blue | Magenta | Cyan | White
let log_with_color c text =
match c with
| Black -> Printf.printf "\027[38;5;%dm%s\027[0m" 0 text
| Red -> Printf.printf "\027[38;5;%dm%s\027[0m" 1 text
| Green -> Printf.printf "\027[38;5;%dm%s\027[0m" 2 text
| Yellow -> Printf.printf "\027[38;5;%dm%s\027[0m" 3 text
| Blue -> Printf.printf "\027[38;5;%dm%s\027[0m" 4 text
| Magenta -> Printf.printf "\027[38;5;%dm%s\027[0m" 5 text
| Cyan -> Printf.printf "\027[38;5;%dm%s\027[0m" 6 text
| White -> Printf.printf "\027[38;5;%dm%s\027[0m" 7 text
end
let () =
Log.log_with_color Log.Red "hello world\n";
Log.log_with_color Log.Green "Hello World\n";
Log.log_with_color Log.Yellow "Hello World\n";
Log.log_with_color Log.Blue "Hello World\n";
Log.log_with_color Log.Cyan "Hello World\n";
Log.log_with_color Log.Magenta "Hello World\n"
@hellojukay
Copy link
Author

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment