Skip to content

Commit

Permalink
Release v0.1.1
Browse files Browse the repository at this point in the history
    Ui
  • Loading branch information
RobertDober committed Dec 7, 2024
1 parent c826eeb commit 66ef566
Showing 1 changed file with 39 additions and 9 deletions.
48 changes: 39 additions & 9 deletions lib/ex_aequo_colors/ui.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,39 @@ defmodule ExAequoColors.Ui do
"""

@doc ~S"""
Just issuing an information
iex(1)> capture_io(fn -> info("hello") end)
"\e[34mINFO: \e[0mhello\n"
And you can also color the message
iex(2)> capture_io(fn -> info("hello", color: :blue) end)
"\e[34mINFO: \e[0m\e[34mhello\e[0m\n"
"""

def info(message, options \\ []) do
_message(message, :info, "INFO: ", options)
end

@doc ~S"""
The error function, by default
iex(1)> capture_io(fn -> error("OH NO") end)
iex(3)> capture_io(fn -> error("OH NO") end)
"\e[31m\e[1mERROR: \e[0mOH NO\n"
or to a different device
iex(2)> capture_io(:stderr, fn -> error("OH NO", device: :stderr) end)
iex(4)> capture_io(:stderr, fn -> error("OH NO", device: :stderr) end)
"\e[31m\e[1mERROR: \e[0mOH NO\n"
changing the label?
iex(3)> capture_io(fn -> error("OH NO", label: "BAD") end)
iex(5)> capture_io(fn -> error("OH NO", label: "BAD") end)
"\e[31m\e[1mBAD\e[0mOH NO\n"
"""
def error(message, options \\ []) do
_message(message, :error, "ERROR: ", options)
Expand All @@ -28,25 +45,38 @@ defmodule ExAequoColors.Ui do
@doc ~S"""
The warning function, by default
iex(4)> capture_io(fn -> warning("Watch out!") end)
iex(6)> capture_io(fn -> warning("Watch out!") end)
"\e[33m\e[1mWARNING: \e[0mWatch out!\n"
"""
def warning(message, options \\ []) do
_message(message, :warning, "WARNING: ", options)
end

@colors %{
info: "<blue>",
error: "<red,bold>",
warning: "<yellow,bold>",
warning: "<yellow,bold>"
}

defp _message(message, type, prefix, options) do
device = Keyword.get(options, :device, :stdio)
color = Map.fetch!(@colors, type)
label = Keyword.get(options, :label, prefix)
result = ExAequoColors.Colorizer.colorize(color <> label <> "$" <> message)
{message_color, reset} = _message_color(options)

result =
ExAequoColors.Colorizer.colorize(color <> label <> "$" <> message_color <> message <> reset)

IO.puts(device, result)
end

defp _message_color(options) do
case Keyword.get(options, :color) do
nil -> {"", ""}
color -> {"<#{color}>", "$"}
end
end
end

# SPDX-License-Identifier: AGPL-3.0-or-later

0 comments on commit 66ef566

Please sign in to comment.