Skip to content

Commit

Permalink
First rough cut on ANSI escaping
Browse files Browse the repository at this point in the history
  • Loading branch information
BjRo committed Jan 20, 2014
1 parent afdb530 commit 8dca083
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 0 deletions.
19 changes: 19 additions & 0 deletions lib/apex/format/color.ex
@@ -0,0 +1,19 @@
defmodule Apex.Format.Color do

def colorize(text, data) do
if color = color(data) do
escape(text, color)
else
text
end
end

defp color(data) when is_list(data), do: "yellow"
defp color(data) when is_tuple(data), do: "blue"
defp color(data) when is_binary(data), do: "red"
defp color(data), do: nil

defp escape(text, color) do
IO.ANSI.escape_fragment("%{#{color}}") <> text <> IO.ANSI.escape_fragment("%{reset}")
end
end
8 changes: 8 additions & 0 deletions lib/apex/format/utils.ex
Expand Up @@ -4,6 +4,14 @@ defmodule Apex.Format.Utils do


def new_line, do: "\n" def new_line, do: "\n"


def colorize(str, data, options) do
if options[:color] == false do
str
else
Apex.Format.Color.colorize(str, data)
end
end

def next_indent_level(options) do def next_indent_level(options) do
Keyword.update(options, :indent_level, 1, &( &1 + 1)) Keyword.update(options, :indent_level, 1, &( &1 + 1))
end end
Expand Down
8 changes: 8 additions & 0 deletions test/format/color_test.ex
@@ -0,0 +1,8 @@
defmodule Apex.Format.Color.Test do
use ExUnit.Case
import Apex.Format.Color

test "#colorize should color text based on the type" do
assert colorize("Tuple should be yellow", {}) == "\e[34mTuple should be yellow\e[0m"
end
end
4 changes: 4 additions & 0 deletions test/format/utils_test.exs
Expand Up @@ -21,4 +21,8 @@ defmodule Apex.Format.Utils.Test do
assert indent([indent_level: 2, indent: 1]) == " " assert indent([indent_level: 2, indent: 1]) == " "
assert indent([indent_level: 3, indent: 1]) == " " assert indent([indent_level: 3, indent: 1]) == " "
end end

test "#colorize should not do anything when it's turned off" do
assert colorize("FOO", {}, color: false) == "FOO"
end
end end

0 comments on commit 8dca083

Please sign in to comment.