Skip to content
Browse files

Adding the awesome_print ANSI aliases

  • Loading branch information...
1 parent 8dca083 commit dca65a35b852225f018171246e780983760481ce @BjRo committed Jan 20, 2014
Showing with 36 additions and 7 deletions.
  1. +28 −6 lib/apex/format/color.ex
  2. +8 −1 test/format/color_test.ex
View
34 lib/apex/format/color.ex
@@ -1,5 +1,4 @@
defmodule Apex.Format.Color do
-
def colorize(text, data) do
if color = color(data) do
escape(text, color)
@@ -8,12 +7,35 @@ defmodule Apex.Format.Color do
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"
+ def escape(text, color), do: do_escape(text, color)
+
+ 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}")
+ # \e => escape
+ # 30 => color base
+ # 1 => bright
+ # 0 => normal
+ [
+ gray: :black,
+ red: :darkred,
+ green: :darkgreen,
+ yellow: :brown,
+ blue: :navy,
+ purple: :darkmagenta,
+ cyan: :darkcyan,
+ white: :slategray
+ ] |> Stream.with_index |> Enum.each fn {{color, shade}, index} ->
+ @index index
+
+ def do_escape(text, unquote(color)) do
+ "\e[1;#{30+@index}m#{text}\e[0m"
+ end
+
+ def do_escape(text, unquote(binary_to_atom("#{color}ish"))) do
+ "\e[0;#{30+@index}m#{text}\e[0m"
+ end
end
end
View
9 test/format/color_test.ex
@@ -3,6 +3,13 @@ defmodule Apex.Format.Color.Test do
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"
+ assert colorize("Tuple should be yellow", {}) == "\e[1;34mTuple should be yellow\e[0m"
+ end
+
+ test "#escape generates the correct pattern matches" do
+ assert escape("A", :gray) == "\e[1;30mA\e[0m"
+ assert escape("A", :grayish) == "\e[0;30mA\e[0m"
+ assert escape("A", :red) == "\e[1;31mA\e[0m"
+ assert escape("A", :redish) == "\e[0;31mA\e[0m"
end
end

0 comments on commit dca65a3

Please sign in to comment.
Something went wrong with that request. Please try again.