diff --git a/lib/display.ex b/lib/display.ex index 7b58519..b123f4d 100644 --- a/lib/display.ex +++ b/lib/display.ex @@ -2,8 +2,9 @@ defmodule Display do alias IO.ANSI @current_dir File.cwd! - def show_failure(%{expr: expr}, module, name) do - IO.puts("") + def show_failure(%{expr: expr}, module, name, options) do + clear_screen(options) + IO.puts("Now meditate upon #{display_module(module)}") IO.puts("---------------------------------------") IO.puts(format_cyan(display_failed_assertion(module, expr))) @@ -15,9 +16,10 @@ defmodule Display do IO.puts("Considering #{display_module(module)}...") end - def before_run do - IO.puts("") - IO.puts("") + def clear_screen(%{ clear_screen: false }), do: false + def clear_screen(_) do + IO.puts(ANSI.clear) + IO.puts(ANSI.home) end def display_failed_assertion(module, expr) do diff --git a/lib/meditate.ex b/lib/meditate.ex index 14dd9f1..58612f6 100644 --- a/lib/meditate.ex +++ b/lib/meditate.ex @@ -1,11 +1,15 @@ defmodule Mix.Tasks.Meditate do use Mix.Task + alias Options - def run(_) do + def run(args) do Application.ensure_all_started(:elixir_koans) Code.compiler_options(ignore_module_conflict: true) Watcher.start - Runner.run + + options = Options.parse(args) + Runner.run(options) + :timer.sleep(:infinity) end end diff --git a/lib/options.ex b/lib/options.ex new file mode 100644 index 0000000..ad63371 --- /dev/null +++ b/lib/options.ex @@ -0,0 +1,16 @@ +defmodule Options do + + def parse([]), do: defaults + def parse(args) do + Enum.reduce(args, defaults, fn(arg, acc) -> + Map.merge(acc, parse_argument(arg)) + end) + end + + def parse_argument("--clear-screen"), do: %{ clear_screen: true} + def parse_argument(_), do: %{} + + defp defaults do + %{ clear_screen: false} + end +end diff --git a/lib/runner.ex b/lib/runner.ex index 9a049b4..28d329f 100644 --- a/lib/runner.ex +++ b/lib/runner.ex @@ -8,27 +8,25 @@ defmodule Runner do Enums ] - def run do - run(Equalities) + def run(options) do + run(Equalities, options) end - def run(start_module) do - Display.before_run - + def run(start_module, options) do start_idx = Enum.find_index(@modules, &(&1 == start_module)) Enum.drop(@modules, start_idx) |> Enum.take_while(fn(mod) -> - run_module(mod) == :passed + run_module(mod, options) == :passed end) end - def run_module(module) do + def run_module(module, options) do Display.considering(module) koans = extract_koans_from(module) passed = Enum.take_while(koans, fn(name) -> - run_koan(module, name) == :passed + run_koan(module, name, options) == :passed end) if Enum.count(koans) == Enum.count(passed) do @@ -38,11 +36,11 @@ defmodule Runner do end end - def run_koan(module, name) do + def run_koan(module, name, options) do case apply(module, name, []) do :ok -> :passed error -> - Display.show_failure(error, module, name) + Display.show_failure(error, module, name, options) :failed end end diff --git a/test/options_test.exs b/test/options_test.exs new file mode 100644 index 0000000..21bc5da --- /dev/null +++ b/test/options_test.exs @@ -0,0 +1,18 @@ +defmodule OptionTest do + use ExUnit.Case, async: true + + test "has default options" do + options = Options.parse([]) + refute Map.fetch!(options, :clear_screen) + end + + test "override clearing of screen" do + options = Options.parse(["--clear-screen"]) + assert Map.fetch!(options, :clear_screen) + end + + test "ignores unknown options" do + options = Options.parse(["--foo"]) + refute Map.fetch!(options, :clear_screen) + end +end