Extracts modules for different sections of the UI
This commit is contained in:
35
lib/display/failure.ex
Normal file
35
lib/display/failure.ex
Normal file
@@ -0,0 +1,35 @@
|
||||
defmodule Display.Failure do
|
||||
@no_value :ex_unit_no_meaningful_value
|
||||
|
||||
def format_failure(%{error: %ExUnit.AssertionError{expr: @no_value, message: message}, file: file, line: line}) do
|
||||
"""
|
||||
#{Colours.cyan("Assertion failed in #{file}:#{line}")}
|
||||
#{Colours.red(message)}
|
||||
"""
|
||||
end
|
||||
def format_failure(%{error: %ExUnit.AssertionError{expr: expr}, file: file, line: line}) do
|
||||
format_assertion_error(expr, file, line)
|
||||
end
|
||||
def format_failure(%{error: error, file: file, line: line}) do
|
||||
"""
|
||||
#{Colours.cyan("Error in #{file}:#{line}")}
|
||||
#{format_error(error)}
|
||||
"""
|
||||
end
|
||||
|
||||
defp format_assertion_error(error, file, line) do
|
||||
"""
|
||||
#{Colours.cyan("Assertion failed in #{file}:#{line}")}
|
||||
#{Colours.red(Macro.to_string(error))}
|
||||
"""
|
||||
end
|
||||
|
||||
defp format_error(error) do
|
||||
trace = System.stacktrace |> Enum.take(2)
|
||||
Colours.red(Exception.format(:error, error, trace))
|
||||
end
|
||||
|
||||
def show_compile_error(error) do
|
||||
format_error(error)
|
||||
end
|
||||
end
|
||||
15
lib/display/intro.ex
Normal file
15
lib/display/intro.ex
Normal file
@@ -0,0 +1,15 @@
|
||||
defmodule Display.Intro do
|
||||
def intro(module, modules) do
|
||||
if not module in modules do
|
||||
show_intro(module.intro)
|
||||
else
|
||||
""
|
||||
end
|
||||
end
|
||||
|
||||
def show_intro(""), do: nil
|
||||
def show_intro(message) do
|
||||
message <> "\n"
|
||||
|> Colours.green
|
||||
end
|
||||
end
|
||||
21
lib/display/notification.ex
Normal file
21
lib/display/notification.ex
Normal file
@@ -0,0 +1,21 @@
|
||||
defmodule Display.Notifications do
|
||||
def congratulate do
|
||||
Colours.green("\nYou have learned much. You must find your own path now.")
|
||||
end
|
||||
|
||||
def invalid_koan(koan, modules) do
|
||||
koans_names = module_names(modules)
|
||||
"Did not find koan #{name(koan)} in " <> koans_names
|
||||
end
|
||||
|
||||
defp module_names(modules) do
|
||||
modules
|
||||
|> Enum.map(&Atom.to_string/1)
|
||||
|> Enum.map(&name/1)
|
||||
|> Enum.join(", ")
|
||||
|> Colours.red
|
||||
end
|
||||
|
||||
defp name("Elixir." <> module), do: module
|
||||
defp name(module), do: name(Atom.to_string(module))
|
||||
end
|
||||
18
lib/display/progress_bar.ex
Normal file
18
lib/display/progress_bar.ex
Normal file
@@ -0,0 +1,18 @@
|
||||
defmodule Display.ProgressBar do
|
||||
@progress_bar_length 30
|
||||
|
||||
def progress_bar(%{current: current, total: total}) do
|
||||
arrow = caluculate_progress(current, total) |> build_arrow
|
||||
|
||||
"|" <> String.ljust(arrow, @progress_bar_length) <> "| #{current} of #{total}"
|
||||
end
|
||||
|
||||
defp caluculate_progress(current, total) do
|
||||
round( (current/total) * @progress_bar_length)
|
||||
end
|
||||
|
||||
defp build_arrow(0), do: ""
|
||||
defp build_arrow(length) do
|
||||
String.duplicate("=", length-1) <> ">"
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user