In iex
, terms are coloured nicely. Can I take advantage of this in my own programs? Is there something like IO.color_inspect
?
Both IO.inspect
and inspect
allow you to pass syntax_colors
option where you can specify the color you want for each type of term as documented here. Unfortunately, it looks like the configuration used by IEx is not exposed so you'll have to copy paste or make your own.
iex(1)> syntax_colors = [number: :yellow, atom: :cyan, string: :green, boolean: :magenta, nil: :magenta]
[number: :yellow, atom: :cyan, string: :green, boolean: :magenta, nil: :magenta]
iex(2)> IO.inspect [1, :a, "b", nil], syntax_colors: syntax_colors; :ok
[1, :a, "b", nil]
:ok
See the IO.ANSI
library for rendering ANSI escape sequences to control formatting, colour, etc.
ExUnit's CLI formatter has some good usage examples.
Using it can be as simple as concatenating or interpolating ANSI characters for foreground or background colours and resets. For example
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With