From c4426306fe0b19bb3656b369414955370f942bed Mon Sep 17 00:00:00 2001 From: RobertDober Date: Sat, 7 Dec 2024 16:55:39 +0100 Subject: [PATCH] Release v0.1.1 Ui --- lib/ex_aequo_colors/ui.ex | 48 +++++++++++++++++++++++++++++++-------- mix.exs | 2 +- 2 files changed, 40 insertions(+), 10 deletions(-) diff --git a/lib/ex_aequo_colors/ui.ex b/lib/ex_aequo_colors/ui.ex index edd6a17..9135231 100644 --- a/lib/ex_aequo_colors/ui.ex +++ b/lib/ex_aequo_colors/ui.ex @@ -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) @@ -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: "", error: "", - warning: "", + warning: "" } 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 diff --git a/mix.exs b/mix.exs index 8fa4bc5..b46d0aa 100644 --- a/mix.exs +++ b/mix.exs @@ -1,6 +1,6 @@ defmodule ExAequoColors.MixProject do use Mix.Project - @version "0.1.0" + @version "0.1.1" @url "https://github.com/RobertDober/ex_aequo_colors" def project do