Extract display into supervision tree.

This commit is contained in:
Nathan Walker
2017-04-24 19:02:53 -05:00
parent 9025744154
commit b71221977c
6 changed files with 91 additions and 53 deletions

View File

@@ -1,21 +1,80 @@
defmodule Display do defmodule Display do
alias IO.ANSI use GenServer
alias Display.ProgressBar
alias Display.Intro
alias Display.Failure
alias Display.Notifications
def invalid_koan(koan, modules) do alias IO.ANSI
alias Display.{ProgressBar, Intro, Failure, Notifications}
def start_link do
GenServer.start_link(__MODULE__, %{clear_screen: true}, name: __MODULE__)
end
def disable_clear do
GenServer.cast(__MODULE__, :disable_clear)
end
def handle_cast(:disable_clear, state) do
{:noreply, %{state | clear_screen: false}}
end
def handle_cast({:invalid, koan, modules}, state) do
Notifications.invalid_koan(koan, modules) Notifications.invalid_koan(koan, modules)
|> IO.puts |> IO.puts
{:noreply, state}
end
def handle_cast({:show_failure, failure, module, name}, state) do
format(failure, module, name)
|> IO.puts
{:noreply, state}
end
def handle_cast({:show_compile_error, error}, state) do
Failure.show_compile_error(error)
|> IO.puts
{:noreply, state}
end
def handle_cast(:congratulate, state) do
Notifications.congratulate
|> IO.puts
{:noreply, state}
end
def handle_cast(:clear_screen, state = %{clear_screen: true}) do
IO.puts(ANSI.clear)
IO.puts(ANSI.home)
{:noreply, state}
end
def handle_cast(:clear_screen, state) do
{:noreply, state}
end
def invalid_koan(koan, modules) do
GenServer.cast(__MODULE__, {:invalid, koan, modules})
end end
def show_failure(failure, module, name) do def show_failure(failure, module, name) do
format(failure, module, name) GenServer.cast(__MODULE__, {:show_failure, failure, module, name})
|> IO.puts
end end
def format(failure, module, name) do def show_compile_error(error) do
GenServer.cast(__MODULE__, {:show_compile_error, error})
end
def congratulate do
GenServer.cast(__MODULE__, :congratulate)
end
def clear_screen do
GenServer.cast(__MODULE__, :clear_screen)
end
defp format(failure, module, name) do
""" """
#{Intro.intro(module, Tracker.visited)} #{Intro.intro(module, Tracker.visited)}
Now meditate upon #{format_module(module)} Now meditate upon #{format_module(module)}
@@ -26,23 +85,6 @@ defmodule Display do
""" """
end end
def show_compile_error(error) do
Failure.show_compile_error(error)
|> IO.puts
end
def congratulate do
Notifications.congratulate
|> IO.puts
end
def clear_screen do
if Options.clear_screen? do
IO.puts(ANSI.clear)
IO.puts(ANSI.home)
end
end
defp format_module(module) do defp format_module(module) do
Module.split(module) |> List.last Module.split(module) |> List.last
end end

15
lib/elixir_koans.ex Normal file
View File

@@ -0,0 +1,15 @@
defmodule ElixirKoans do
use Application
alias Options
def start(_type, _args) do
import Supervisor.Spec
children = [
worker(Display, [])
]
opts = [strategy: :one_for_one, name: ElixirKoans.Supervisor]
Supervisor.start_link(children, opts)
end
end

View File

@@ -12,6 +12,11 @@ defmodule Mix.Tasks.Meditate do
Options.start(args) Options.start(args)
{parsed, _, _} = OptionParser.parse(args)
if Keyword.has_key?(parsed, :no_clear_screen) do
Display.disable_clear()
end
Options.initial_koan Options.initial_koan
|> ok? |> ok?
|> Runner.modules_to_run |> Runner.modules_to_run

View File

@@ -1,6 +1,5 @@
defmodule Options do defmodule Options do
@defaults %{ @defaults %{
clear_screen: true,
initial_koan: Equalities, initial_koan: Equalities,
} }
@@ -8,16 +7,8 @@ defmodule Options do
Agent.start_link(fn -> parse(args) end, name: __MODULE__) Agent.start_link(fn -> parse(args) end, name: __MODULE__)
end end
def clear_screen? do
Agent.get(__MODULE__, fn(options) ->
Map.fetch!(options, :clear_screen)
end)
end
def initial_koan() do def initial_koan() do
Agent.get(__MODULE__, fn(options) -> Agent.get(__MODULE__, &Map.fetch!(&1, :initial_koan))
Map.fetch!(options, :initial_koan)
end)
end end
defp parse(args) do defp parse(args) do
@@ -27,6 +18,5 @@ defmodule Options do
end end
def parse_argument("--koan="<>module), do: %{ initial_koan: String.to_atom("Elixir."<> module)} def parse_argument("--koan="<>module), do: %{ initial_koan: String.to_atom("Elixir."<> module)}
def parse_argument("--no-clear-screen"), do: %{ clear_screen: false}
def parse_argument(_), do: %{} def parse_argument(_), do: %{}
end end

View File

@@ -10,7 +10,8 @@ defmodule Koans.Mixfile do
end end
def application do def application do
[applications: [:exfswatch, :logger]] [mod: {ElixirKoans, []},
applications: [:exfswatch, :logger]]
end end
defp deps do defp deps do

View File

@@ -1,23 +1,8 @@
defmodule OptionTest do defmodule OptionTest do
use ExUnit.Case, async: true use ExUnit.Case, async: true
test "has default options" do
Options.start([])
assert Options.clear_screen?
end
test "override clearing of screen" do
Options.start(["--no-clear-screen"])
refute Options.clear_screen?
end
test "can target specifc koans" do test "can target specifc koans" do
Options.start(["--koan=Strings"]) Options.start(["--koan=Strings"])
assert Options.initial_koan() == Strings assert Options.initial_koan() == Strings
end end
test "ignores unknown options" do
Options.start(["--foo"])
assert Options.clear_screen?
end
end end