Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

First rough cut on ANSI escaping

  • Loading branch information...
commit 8dca08327f4ed0283f1e261329f344e93875285c 1 parent afdb530
@BjRo authored
View
19 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
View
8 lib/apex/format/utils.ex
@@ -4,6 +4,14 @@ defmodule Apex.Format.Utils do
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
Keyword.update(options, :indent_level, 1, &( &1 + 1))
end
View
8 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
View
4 test/format/utils_test.exs
@@ -21,4 +21,8 @@ defmodule Apex.Format.Utils.Test do
assert indent([indent_level: 2, indent: 1]) == " "
assert indent([indent_level: 3, indent: 1]) == " "
end
+
+ test "#colorize should not do anything when it's turned off" do
+ assert colorize("FOO", {}, color: false) == "FOO"
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.