-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4b64e23
commit d26beb0
Showing
8 changed files
with
236 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,26 @@ and any changes you make in this file will most likely be lost | |
|
||
- And that's about it | ||
|
||
## Documentation | ||
|
||
|
||
### Features | ||
|
||
- ANSI Colors | ||
|
||
- Extended ANSI Colors | ||
|
||
- `colorize` CLI | ||
|
||
|
||
|
||
### ExAequoColors.color/2 | ||
|
||
`color` add ANSI code for a color to a string | ||
|
||
|
||
|
||
|
||
## Author | ||
|
||
Copyright © 2024 Robert Dober [email protected] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,14 @@ | |
|
||
- And that's about it | ||
|
||
## Documentation | ||
|
||
<%= xtra.moduledoc "ExAequoColors", wrap_code_blocks: "elixir" %> | ||
|
||
<%= xtra.functiondoc "ExAequoColors.color/2", wrap_code_blocks: "elixir", headline: 3 %> | ||
|
||
|
||
|
||
## Author | ||
|
||
Copyright © 2024 Robert Dober [email protected] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,35 @@ | ||
defmodule ExAequoColors do | ||
@version "0.0.0" | ||
@moduledoc """ | ||
Documentation for `ExAequoColors`. | ||
@version "0.1.0" | ||
|
||
alias __MODULE__.Color | ||
|
||
@moduledoc ~S""" | ||
### Features | ||
- ANSI Colors | ||
- Extended ANSI Colors | ||
- `colorize` CLI | ||
""" | ||
|
||
@doc """ | ||
Hello world. | ||
@doc ~S""" | ||
add ANSI code for a color to a string | ||
## Examples | ||
We can use the 8 basic ANSI color codes | ||
iex> ExAequoColors.hello() | ||
:world | ||
iex(0)> color("This is red", :red) | ||
{:ok, "\e[31mThis is red"} | ||
""" | ||
def hello do | ||
:world | ||
|
||
def color(subject, flags) do | ||
Color.color(tostring(subject), flags) | ||
end | ||
|
||
def version, do: @version | ||
end | ||
|
||
# SPDX-License-Identifier: AGPL-3.0-or-later |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
defmodule ExAequoColors.Color do | ||
@moduledoc false | ||
|
||
@doc ~S""" | ||
Transform a string and color specifications into an IO Chardata | ||
""" | ||
def color(subject, colorspec) | ||
|
||
def color(subject, colorspec) when is_atom(colorspec) do | ||
color(subject, [colorspec]) | ||
end | ||
|
||
def color(subject, colorspec) do | ||
with {:ok, prefix, suffix} <- _extract_codes_from(colorspec) do | ||
{:ok, [prefix, subject, suffix]} | ||
end | ||
end | ||
|
||
defp _extract_codes_from(colorspec) do | ||
if Enum.member?(colorspec, :reset) do | ||
case (colorspec |> List.delete(:reset) |> _extract_prefix_from()) do | ||
{:ok, prefix} -> {prefix, "\e[0m"} | ||
error -> error | ||
end | ||
else | ||
case colorspec |> _extract_prefix_from() do | ||
{:ok, prefix} -> {prefix, ""} | ||
error -> error | ||
end | ||
end | ||
end | ||
|
||
defp _extract_prefix_from(colorspec) do | ||
|
||
end | ||
|
||
|
||
defp _to_kwd(list) do | ||
list | ||
|> Enum.map(&{&1, true}) | ||
end | ||
end | ||
# SPDX-License-Identifier: AGPL-3.0-or-later |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
%{ | ||
"dialyxir": {:hex, :dialyxir, "1.4.3", "edd0124f358f0b9e95bfe53a9fcf806d615d8f838e2202a9f430d59566b6b53b", [:mix], [{:erlex, ">= 0.2.6", [hex: :erlex, repo: "hexpm", optional: false]}], "hexpm", "bf2cfb75cd5c5006bec30141b131663299c661a864ec7fbbc72dfa557487a986"}, | ||
"erlex": {:hex, :erlex, "0.2.7", "810e8725f96ab74d17aac676e748627a07bc87eb950d2b83acd29dc047a30595", [:mix], [], "hexpm", "3ed95f79d1a844c3f6bf0cea61e0d5612a42ce56da9c03f01df538685365efb0"}, | ||
"ex_aequo_kwds": {:hex, :ex_aequo_kwds, "0.1.0", "a33c6fcba2da4afb1db2375888b854a5034ace4a9691cdcd7c43cabeb4a55fc7", [:mix], [], "hexpm", "bdf4967aa8796b9d72326fbf1a397d1070847f93599bd56f07b552c6d2b94432"}, | ||
"excoveralls": {:hex, :excoveralls, "0.18.1", "a6f547570c6b24ec13f122a5634833a063aec49218f6fff27de9df693a15588c", [:mix], [{:castore, "~> 1.0", [hex: :castore, repo: "hexpm", optional: true]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "d65f79db146bb20399f23046015974de0079668b9abb2f5aac074d078da60b8d"}, | ||
"extractly": {:hex, :extractly, "0.5.4", "22ff3a624d814227ba842a2b59a38b1298df5531dbd4772dcbe9b97e05627145", [:mix], [], "hexpm", "612e16920317b2fb963b2da013019a614ba12b928d535a053a1efb21ddaa6268"}, | ||
"jason": {:hex, :jason, "1.4.3", "d3f984eeb96fe53b85d20e0b049f03e57d075b5acda3ac8d465c969a2536c17b", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "9a90e868927f7c777689baa16d86f4d0e086d968db5c05d917ccff6d443e58a3"}, | ||
"minipeg": {:hex, :minipeg, "0.6.3", "9b1efe4ed37b8fbbadaff15f29a119cf6ea6c0b56206a4e23bbf65aa91a384cb", [:mix], [], "hexpm", "413e13b603d1e9c43722c7c63fc5a4694897c234e6aa5aa7efac647a181b5867"}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
defmodule Test.Color.BasicAnsiTest do | ||
use ExUnit.Case | ||
import ExAequoColors.Color, only: [color: 2] | ||
|
||
describe "basic ANSI colors" do | ||
[ | ||
{30, :black}, | ||
{31, :red}, | ||
{32, :green}, | ||
{33, :yellow}, | ||
{34, :blue}, | ||
{35, :magenta}, | ||
{36, :cyan}, | ||
{37, :white} | ||
] | ||
|> Enum.each(fn {escape, color_name} -> | ||
expected = ["\e[#{escape}m", "#{color_name}", "\e[0m"] | ||
test_name = "basic ANSI color #{color_name} with reset" | ||
|
||
test test_name do | ||
assert color(to_string(unquote(color_name)), unquote(:color_name)) == unquote(expected) | ||
end | ||
end) | ||
end | ||
|
||
# defmodule Test.ExAequo.ColorTest do | ||
# use ExUnit.Case | ||
|
||
# alias Support.Random | ||
# import ExAequo.Color | ||
|
||
# describe "rgb" do | ||
# # 3 param RGB form | ||
# Random.tuple_samples(0..255, 3, 20) | ||
# |> Enum.each( fn {red, green, blue} -> | ||
# expected = "\e[38;2;#{red};#{green};#{blue}m" | ||
# test_name = "rgb(#{red}, #{green}, #{blue})" | ||
# test test_name do | ||
# assert rgb(unquote(red), unquote(green), unquote(blue)) == unquote(expected) | ||
# end | ||
# end) | ||
|
||
# # Triple RGB form | ||
# Random.tuple_samples(0..255, 3, 20) | ||
# |> Enum.each( fn {red, green, blue} -> | ||
# expected = "\e[38;2;#{red};#{green};#{blue}m" | ||
# test_name = "rgb({#{red}, #{green}, #{blue}})" | ||
# test test_name do | ||
# assert rgb({unquote(red), unquote(green), unquote(blue)}) == unquote(expected) | ||
# end | ||
# end) | ||
# end | ||
|
||
# describe "legacy ANSI colors" do | ||
# [ | ||
# {30, :black }, | ||
# {31, :red }, | ||
# {32, :green }, | ||
# {33, :yellow }, | ||
# {34, :blue }, | ||
# {35, :magenta }, | ||
# {36, :cyan }, | ||
# {37, :white} | ||
# ] |> Enum.each( fn {escape, name} -> | ||
# expected = ["\e[#{escape}m", "#{name}"] | ||
# input = [name, to_string(name)] | ||
# test_name = "legacy ANSI color #{name}" | ||
# test test_name do | ||
# assert format(unquote(input)) == unquote(expected) | ||
# end | ||
# end) | ||
# end | ||
|
||
# describe "legacy background colors" do | ||
# [ | ||
# {40, :bg_black }, | ||
# {41, :bg_red }, | ||
# {42, :bg_green }, | ||
# {43, :bg_yellow }, | ||
# {44, :bg_blue }, | ||
# {45, :bg_magenta }, | ||
# {46, :bg_cyan }, | ||
# {47, :bg_white} | ||
# ] |> Enum.each( fn {escape, name} -> | ||
# expected = ["\e[#{escape}m", "#{name}"] | ||
# input = [name, to_string(name)] | ||
# test_name = "legacy ANSI color #{name}" | ||
# test test_name do | ||
# assert format(unquote(input)) == unquote(expected) | ||
# end | ||
# end) | ||
# end | ||
|
||
# describe "256 colors space" do | ||
# (0..255) | ||
# |> Enum.each(fn n -> | ||
# expected = ["\e[38;5;#{n}m"] | ||
# input = ["color#{n}" |> String.to_atom] | ||
# test_name = "color #{n} from 256 colors space" | ||
# test test_name do | ||
# assert format(unquote(input)) == unquote(expected) | ||
# end | ||
# end) | ||
# end | ||
|
||
# describe "256 colors background space" do | ||
# (0..255) | ||
# |> Enum.each(fn n -> | ||
# expected = ["\e[48;5;#{n}m"] | ||
# input = ["bg_color#{n}" |> String.to_atom] | ||
# test_name = "bg color #{n} from 256 colors space" | ||
# test test_name do | ||
# assert format(unquote(input)) == unquote(expected) | ||
# end | ||
# end) | ||
|
||
# end | ||
|
||
# describe "error message" do | ||
# test "not a color" do | ||
# assert_raise ExAequo.Error, fn -> | ||
# format([:definitely_not_a_colorzjieozfef]) | ||
# end | ||
# end | ||
# end | ||
|
||
# end | ||
end | ||
|
||
# SPDX-License-Identifier: AGPL-3.0-or-later |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,12 @@ | ||
defmodule ExAequoColorsTest do | ||
use ExUnit.Case | ||
doctest ExAequoColors | ||
doctest ExAequoColors, import: true | ||
|
||
@version_rgx ~r/\A \d+ \. \d+ \. \d+ /x | ||
|
||
test "version is defined and well formed" do | ||
assert Regex.match?(@version_rgx, ExAequoColors.version()) | ||
end | ||
end | ||
|
||
# SPDX-License-Identifier: AGPL-3.0-or-later |