Skip to content

Commit

Permalink
Switched to minipeg for color parsing
Browse files Browse the repository at this point in the history
	WIP debug colorizer doctests
  • Loading branch information
RobertDober committed Oct 15, 2024
1 parent 8aad4e9 commit 771fd5a
Show file tree
Hide file tree
Showing 6 changed files with 214 additions and 244 deletions.
11 changes: 6 additions & 5 deletions lib/ex_aequo_colors/cli.ex
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
defmodule ExAequoColors.Cli do

alias ExAequoColors.Colorizer.Parser, as: Parser
import ExAequoBase.Map, only: [put_if: 3]
import ExAequoColors.Color, only: [color: 2]
import ExAequoColors.Colorizer, only: [colorize: 1, colorize: 2, colorize!: 2, mk_options_and_rgxen: 1]
import ExAequoColors.Colorizer, only: [colorize: 1, colorize: 2, colorize!: 3, mk_options_and_rgxen: 1]

@moduledoc false

Expand Down Expand Up @@ -43,18 +44,18 @@ defmodule ExAequoColors.Cli do
end

defp colorize_input(options) do
opt_and_rgxen = mk_options_and_rgxen(options)
case Map.get(options, :file) do
nil -> IO.stream(:stdio, :line)
file -> File.stream!(file, :line)
end
|> run(opt_and_rgxen)
|> run(options)
end


defp run(stream, opt_and_rgxen) do
defp run(stream, options) do
parser = Parser.chunks_parser(options)
stream
|> Stream.map(&colorize!(&1, opt_and_rgxen))
|> Stream.map(&colorize!(&1, parser, options))
|> Enum.join("\n")
end

Expand Down
129 changes: 78 additions & 51 deletions lib/ex_aequo_colors/color.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
defmodule ExAequoColors.Color do

@moduledoc false

@names %{
Expand Down Expand Up @@ -471,77 +470,105 @@ defmodule ExAequoColors.Color do
yellow3: {175, 215, 0},
yellow4: {135, 135, 0},
yellow: 33
}
}

@doc ~S"""
Transform a string and color specifications into an IO Chardata
"""
def color(subject, colorspec)
@doc ~S"""
just get the code for colorspec without a string (subject)
"""
def color(colorspec)

def color(subject, colorspec) when is_atom(colorspec) do
color(subject, [colorspec])
end
def color(colorspec) when is_list(colorspec) do
colorspec
|> Enum.map(&_extract_code/1)
|> Enum.join()
end

def color(subject, colorspec) do
{cspec, suffix} = _extract_reset(colorspec)
prefix = cspec
|> Enum.map(&_extract_code/1)
|> Enum.join
[ prefix, subject, suffix ]
|> IO.chardata_to_string
end
def color(colorspec) do
_extract_code(colorspec)
end

@doc ~S"""
Transform a string and color specifications into an IO Chardata
"""
def color(subject, colorspec)

def color_code(colorspec)
def color_code(colorspec) when is_list(colorspec) do
colorspec
def color(subject, colorspec) when is_atom(colorspec) do
color(subject, [colorspec])
end

def color(subject, colorspec) do
{cspec, suffix} = _extract_reset(colorspec)

prefix =
cspec
|> Enum.map(&_extract_code/1)
|> Enum.join
end
def color_code(colorspec), do: _extract_code(:reset)
|> Enum.join()

@doc ~S"""
Transform a string and color specifications into an IO Chardata with a
reset at the end
"""
def color_reset(subject, colorspec)
[prefix, subject, suffix]
|> IO.chardata_to_string()
end

def color_reset(subject, colorspec) when is_atom(colorspec) do
color_reset(subject, [colorspec])
end
def color_code(colorspec)

def color_reset(subject, colorspec) do
color(subject, [:reset | colorspec])
end
def color_code(colorspec) when is_list(colorspec) do
colorspec
|> Enum.map(&_extract_code/1)
|> Enum.join()
end

defp _extract_code(code)
defp _extract_code({_, _, _}=rgb) do
def color_code(colorspec), do: _extract_code(:reset)

@doc ~S"""
Transform a string and color specifications into an IO Chardata with a
reset at the end
"""
def color_reset(subject, colorspec)

def color_reset(subject, colorspec) when is_atom(colorspec) do
color_reset(subject, [colorspec])
end

def color_reset(subject, colorspec) do
color(subject, [:reset | colorspec])
end

defp _extract_code(code)

defp _extract_code({_, _, _} = rgb) do
if System.get_env("NO_COLOR") do
""
else
_extract_rgb(rgb)
end
end

defp _extract_code(code) do
defp _extract_code(code) do
if System.get_env("NO_COLOR") do
""
else
case Map.get(@names, code) do
{_, _, _} = rgb -> _extract_rgb(rgb)
{:color, col} -> _extract_col(col)
ansi -> "\e[#{ansi}m"
ansi -> "\e[#{ansi}m"
end
end
end

defp _extract_col(col) do
"\e[38;5;#{col}m"
end
defp _extract_col(col) do
"\e[38;5;#{col}m"
end

defp _extract_rgb({r, g, b}) do
"\e[38;2;#{r};#{g};#{b}m"
end
defp _extract_rgb({r, g, b}) do
"\e[38;2;#{r};#{g};#{b}m"
end

defp _extract_reset(colorspec) do
if Enum.member?(colorspec, :reset) do
{List.delete(colorspec, :reset), _extract_code(:reset)}
else
{colorspec, ""}
end
defp _extract_reset(colorspec) do
if Enum.member?(colorspec, :reset) do
{List.delete(colorspec, :reset), _extract_code(:reset)}
else
{colorspec, ""}
end

end
end

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

0 comments on commit 771fd5a

Please sign in to comment.