View Source ExAequoColors.Colorizer (ExAequoColors v0.1.1)

Backend of the cli

Summary

Functions

colorizes a binary according to options

Using a map for options and an already provided parser

This differs substentially from calling colorize with a list as we will report errors with line numbers if applicable

Types

@type atoms() :: [atom()]
@type binaries() :: [binary()]
@type color_fn_t() :: (-> color_t())
@type color_t() :: maybe(result_t(binary()))
@type either(lt, rt) :: ok_t(lt) | error_t(rt)
@type error_t() :: {:error, binary()}
@type error_t(t) :: {:error, t}
@type input_source_t() :: Enumerable.t() | binary() | binaries()
@type maybe(t) :: nil | t
@type natural() :: non_neg_integer()
@type numbered(t) :: {t, number()}
@type numbered_line_t() :: numbered(binary())
@type numbered_lines_t() :: [numbered_line_t()]
@type ok_t() :: {:ok, any()}
@type ok_t(t) :: {:ok, t}
@type pair_t() :: {any(), any()}
@type pair_t(t) :: {t, t}
@type pair_t(lt, rt) :: {lt, rt}
@type pairs_t() :: [pair_t()]
@type pairs_t(t) :: [pair_t(t)]
@type pairs_t(lt, rt) :: [pair_t(lt, rt)]
@type parser_fn() :: (binary() -> any())
@type reducer_result_t() :: {:halt, error_t()} | {:cont, ok_t()}
@type result_fun_t() :: (any() -> result_t())
@type result_fun_t(t) :: (any() -> result_t(t))
@type result_t() :: either(any(), binary())
@type result_t(t) :: either(t, binary())
@type rgx_pair() :: {Regex.t(), ExAequoFn.NamedFn.t()}
@type rgx_pairs() :: [rgx_pair()]
@type stream_t() ::
  %IO.Stream{device: term(), line_or_bytes: term(), raw: term()}
  | %File.Stream{
      line_or_bytes: term(),
      modes: term(),
      node: term(),
      path: term(),
      raw: term()
    }
@type zero_fn_t() :: (-> any())
@type zero_fn_t(t) :: (-> t)

Functions

@spec colorize(binary()) :: color_t()

colorizes a binary according to options

iex(1)> colorize("hello")
"hello"

Default Options, resetting with $

Here are the default options, a color is triggered by <...

iex(2)> colorize("<green>")
"\e[32m"

We can also add bold for example

iex(3)> colorize("<green, bold>success")
"\e[32m\e[1msuccess"

And resetting is simply achieved by a $

iex(4)> colorize("$")
"\e[0m"

Which works in both orders, and note that the space after the , is not needed, we use $ to reset here!

iex(5)> colorize("<bold,yellow>highlighted$normal")
"\e[1m\e[33mhighlighted\e[0mnormal"

We can use other color systems, like ANSI 256, also demonstrating how to escape < and $:

iex(6)> colorize("256<color21><<$$")
"256\e[38;5;21m<$"

Dim, italic and named RGB colors are also at your disposal

iex(7)> colorize("<dim, light_coral>and<italic>")
"\e[2m\e[38;2;255;135;135mand\e[3m"

Using different options

Maybe we want to open and close color definitions with the ! character, note also that, in that case we need to double the ! character, but not the < character anymore, to get them as verbatim text

iex(8)> colorize("!red!!!<$", trigger: "!", closer: "!")
"\e[31m!<\e[0m"

# rgb

In three decimal numbers....

iex(9)> colorize("<12,255,0>rgb")
"\e[38;2;12;255;0mrgb"

or in hex

iex(10)> colorize("<#0cff00>rgb")
"\e[38;2;12;255;0mrgb"
Link to this function

colorize(line_or_lines, options)

View Source
@spec colorize(binary() | binaries(), map()) :: color_t()
Link to this function

colorize!(line, parser, options)

View Source

Using a map for options and an already provided parser

iex(11)> parser = make_parser()
...(11)> colorize!("<magenta,bold>", parser, %{auto: true})
{:ok, "\e[35m\e[1m\e[0m"}

This is very useful for meaningful error messages as we can see below

Link to this function

colorize_lines(lines, options \\ [])

View Source

This differs substentially from calling colorize with a list as we will report errors with line numbers if applicable

iex(12)> colorize_lines(["<bold>BOLD", "<red>RED"])
{:ok, ["\e[1mBOLD", "\e[31mRED"]}

It can also join the lines together

iex(13)> colorize_lines(["<bold>BOLD", "<red>RED"], join: true)
{:ok, "\e[1mBOLD\n\e[31mRED"}

with a custom joiner and autoreset too

iex(14)> colorize_lines(["<bold>BOLD", "<red>RED"], join: "", auto: true)
{:ok, "\e[1mBOLD\e[0m\e[31mRED\e[0m"}

Lines can also be a stream

iex(15)> lines = ["<bold>BOLD", "<red>RED"] |> Stream.map(&(&1))
...(15)> colorize_lines(lines)
{:ok, ["\e[1mBOLD", "\e[31mRED"]}

colorize_lines()
{:ok, ["\e[1mBOLD", "\e[31mRED"]}

but the nice thing is to tell the user where the error is

iex(14)> colorize_lines(["<bold>BOLD", "<red"], join: "", auto: true)
{:error, "Illegal color syntax in line 2"}
Link to this function

make_parser(options \\ [])

View Source
Link to this function

make_parser_and_options(options \\ [])

View Source