Skip to content

Commit

Permalink
Road to v0.1.1
Browse files Browse the repository at this point in the history
    WIP on Ui
  • Loading branch information
RobertDober committed Dec 7, 2024
1 parent 23f3072 commit c826eeb
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 0 deletions.
30 changes: 30 additions & 0 deletions lib/ex_aequo_colors/keywords.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
defmodule ExAequoColors.Keywords do
@moduledoc ~S"""
Tools to access keywords (to be moved to ExAequoBase)
"""

@doc ~S"""
Get first present value from a keyword list
iex(1)> access_or([a: 1, b: 2], [:b, :a])
2
iex(2)> access_or([a: 1, b: 2], [:x, :y])
nil
or an explicit default
iex(3)> access_or([a: 1, b: 2], [:x, :y], :not_found)
:not_found
"""
def access_or(kwd, accessors, default \\ nil) do
accessors
|> Enum.reverse
|> Enum.reduce(default, fn index, result ->
Keyword.get(kwd, index, result)
end)
end
end
# SPDX-License-Identifier: AGPL-3.0-or-later
52 changes: 52 additions & 0 deletions lib/ex_aequo_colors/ui.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
defmodule ExAequoColors.Ui do
@moduledoc ~S"""
Some convenience methods to output colorized messages
"""

@doc ~S"""
The error function, by default
iex(1)> 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)
"\e[31m\e[1mERROR: \e[0mOH NO\n"
changing the label?
iex(3)> 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)
end

@doc ~S"""
The warning function, by default
iex(4)> 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 %{
error: "<red,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)
IO.puts(device, result)
end
end
# SPDX-License-Identifier: AGPL-3.0-or-later
6 changes: 6 additions & 0 deletions test/keywords_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
defmodule Test.ExAequoColors.KeywordsTest do
use ExUnit.Case

doctest ExAequoColors.Keywords, import: true
end
# SPDX-License-Identifier: AGPL-3.0-or-later
7 changes: 7 additions & 0 deletions test/ui_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
defmodule Test.ExAequoColors.UiTest do
use ExUnit.Case
import ExUnit.CaptureIO

doctest ExAequoColors.Ui, import: true
end
# SPDX-License-Identifier: AGPL-3.0-or-later

0 comments on commit c826eeb

Please sign in to comment.