To improve testing, add a layer of indirection around colours.
This commit is contained in:
36
lib/display/colours.ex
Normal file
36
lib/display/colours.ex
Normal file
@@ -0,0 +1,36 @@
|
||||
defmodule Display.Paint do
|
||||
def red(str), do: painter().red(str)
|
||||
|
||||
def cyan(str), do: painter().cyan(str)
|
||||
|
||||
def green(str), do: painter().green(str)
|
||||
|
||||
defp painter do
|
||||
case Mix.env do
|
||||
:test -> Display.Uncoloured
|
||||
_ -> Display.Colours
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defmodule Display.Colours do
|
||||
alias IO.ANSI
|
||||
|
||||
def red(str), do: colourize(ANSI.red, str)
|
||||
|
||||
def cyan(str), do: colourize(ANSI.cyan, str)
|
||||
|
||||
def green(str), do: colourize(ANSI.green, str)
|
||||
|
||||
defp colourize(color, message) do
|
||||
Enum.join([color, message, ANSI.reset], "")
|
||||
end
|
||||
end
|
||||
|
||||
defmodule Display.Uncoloured do
|
||||
def red(str), do: str
|
||||
|
||||
def cyan(str), do: str
|
||||
|
||||
def green(str), do: str
|
||||
end
|
||||
Reference in New Issue
Block a user