Merge pull request #77 from iamvery/exit-on-completion

Congratulate and exit when koans are complete
This commit is contained in:
Uku Taht
2016-04-27 10:27:44 +01:00
5 changed files with 35 additions and 8 deletions

View File

@@ -23,7 +23,7 @@ defmodule Display do
def show_failure(failure, module, name) do
IO.puts("Now meditate upon #{format_module(module)}")
IO.puts(progress_bar(Tracker.get))
IO.puts(progress_bar(Tracker.summarize))
IO.puts(bar())
IO.puts(name)
IO.puts(format_failure(failure))
@@ -53,6 +53,11 @@ defmodule Display do
format_error(error) |> IO.puts
end
def congratulate do
format_green("\nYou have learned much. You must find your own path now.")
|> IO.puts
end
def clear_screen do
if Options.clear_screen? do
IO.puts(ANSI.clear)
@@ -97,6 +102,10 @@ defmodule Display do
Enum.join([ANSI.cyan, str, ANSI.reset], "")
end
defp format_green(str) do
Enum.join([ANSI.green, str, ANSI.reset], "")
end
defp format_module(module) do
Module.split(module) |> List.last
end

View File

@@ -5,7 +5,8 @@ defmodule Mix.Tasks.Meditate do
def run(args) do
Application.ensure_all_started(:elixir_koans)
Code.compiler_options(ignore_module_conflict: true)
Watcher.start
{:ok, watcher} = Watcher.start
Process.monitor(watcher)
Options.start(args)
@@ -15,7 +16,14 @@ defmodule Mix.Tasks.Meditate do
|> Tracker.start
|> Runner.run
:timer.sleep(:infinity)
if Tracker.complete? do
Display.congratulate
exit(:normal)
end
receive do
{:DOWN, _references, :process, ^watcher, _reason} -> nil
end
end
defp ok?(koan) do

View File

@@ -10,7 +10,6 @@ defmodule Tracker do
def get do
Agent.get(__MODULE__, &(&1))
|> summarize
end
def completed(koan) do
@@ -19,7 +18,13 @@ defmodule Tracker do
end)
end
def summarize({total, completed}) do
def complete? do
{total, completed} = get
total == Enum.count(completed)
end
def summarize, do: get |> summarize
defp summarize({total, completed}) do
%{total: total, current: MapSet.size(completed)}
end
end

View File

@@ -12,6 +12,11 @@ defmodule Watcher do
rescue
e -> Display.show_compile_error(e)
end
if Tracker.complete? do
Display.congratulate
exit(:normal)
end
end
end
end

View File

@@ -5,19 +5,19 @@ defmodule TrackerTest do
test "can start" do
Tracker.start(@sample_modules)
assert Tracker.get == %{total: 2, current: 0}
assert Tracker.summarize == %{total: 2, current: 0}
end
test "can be notified of completed koans" do
Tracker.start(@sample_modules)
Tracker.completed(:"Hi there")
assert Tracker.get == %{total: 2, current: 1}
assert Tracker.summarize == %{total: 2, current: 1}
end
test "multiple comletions of the same koan count only once" do
Tracker.start(@sample_modules)
Tracker.completed(:"Hi there")
Tracker.completed(:"Hi there")
assert Tracker.get == %{total: 2, current: 1}
assert Tracker.summarize == %{total: 2, current: 1}
end
end