Skip to content

Commit

Permalink
v0.1.2
Browse files Browse the repository at this point in the history
Implementation of puts_col
Fix for `auto: true`
  • Loading branch information
RobertDober committed Dec 18, 2024
1 parent 1986a4a commit 4560f02
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 3 deletions.
9 changes: 9 additions & 0 deletions lib/ex_aequo_colors.ex
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ defmodule ExAequoColors do
defdelegate colorize(line, options), to: Colorizer
defdelegate colorize(line), to: Colorizer

@doc ~S"""
Colorizes and puts to device
"""
def puts_col(line, device \\ :stdio) do
colorized = line |> colorize(auto: true)
IO.puts(device, colorized)
end


def version, do: @version
end

Expand Down
4 changes: 2 additions & 2 deletions lib/ex_aequo_colors/colorizer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,10 @@ defmodule ExAequoColors.Colorizer do
options1 = make_options(options)
reset = if Map.get(options1, :auto), do: Color.color_code(:reset), else: ""

lines
(lines
|> Enum.map(&Minipeg.Parser.parse_string!(Parser.chunks_parser(options1), &1))
# |> IO.inspect
|> Enum.join(reset <> "\n")
|> Enum.join(reset <> "\n")) <> reset
end

@doc ~S"""
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
defmodule ExAequoColors.MixProject do
use Mix.Project
@version "0.1.1"
@version "0.1.2"
@url "https://github.com/RobertDober/ex_aequo_colors"

def project do
Expand Down
3 changes: 3 additions & 0 deletions test/ex_aequo_colors/colorizer_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ defmodule Test.ExAequoColors.ColorizerTest do
test "in hex, 3 bytes" do
assert colorize("<#fd8>rgb hex 3") == "\e[38;2;240;208;128mrgb hex 3"
end
test "in hex, 3 bytes, and auto" do
assert colorize("<#fd8>rgb hex 3", auto: true) == "\e[38;2;240;208;128mrgb hex 3\e[0m"
end
end

end
Expand Down
23 changes: 23 additions & 0 deletions test/ex_aequo_colors/puts_col_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
defmodule Test.ExAequoColors.PutsColTest do
use ExUnit.Case
import ExUnit.CaptureIO

import ExAequoColors, only: [puts_col: 1, puts_col: 2]

describe "puts_col" do
test "it puts colorized to stdout" do
output = capture_io(fn ->
puts_col("<bold>hello")
end)
assert output == "\e[1mhello\e[0m\n"
end
test "it puts colorized to stderr" do
output = capture_io(:stderr, fn ->
puts_col("<red>ERROR", :stderr)
end)
assert output == "\e[31mERROR\e[0m\n"
end
end

end
# SPDX-License-Identifier: Apache-2.0

0 comments on commit 4560f02

Please sign in to comment.