Add a progress bar that also displays the number of koans.
This commit is contained in:
@@ -2,6 +2,8 @@ defmodule Display do
|
|||||||
alias IO.ANSI
|
alias IO.ANSI
|
||||||
@current_dir File.cwd!
|
@current_dir File.cwd!
|
||||||
@no_value :ex_unit_no_meaningful_value
|
@no_value :ex_unit_no_meaningful_value
|
||||||
|
@window_width 40
|
||||||
|
@half_window 20
|
||||||
|
|
||||||
def invalid_koan(koan, modules) do
|
def invalid_koan(koan, modules) do
|
||||||
koans_names = module_names(modules)
|
koans_names = module_names(modules)
|
||||||
@@ -22,11 +24,54 @@ defmodule Display do
|
|||||||
|
|
||||||
def show_failure(failure, module, name) do
|
def show_failure(failure, module, name) do
|
||||||
IO.puts("Now meditate upon #{format_module(module)}")
|
IO.puts("Now meditate upon #{format_module(module)}")
|
||||||
IO.puts("---------------------------------------")
|
IO.puts(progress_bar(Tracker.get))
|
||||||
|
IO.puts(bar())
|
||||||
IO.puts(name)
|
IO.puts(name)
|
||||||
IO.puts(format_failure(failure))
|
IO.puts(format_failure(failure))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp bar() do
|
||||||
|
String.duplicate("-", @window_width + 1)
|
||||||
|
end
|
||||||
|
|
||||||
|
def progress_bar(%{current: current, total: total}) do
|
||||||
|
progress = caluculate_progress(current, total)
|
||||||
|
progress_notification = "(#{current}/#{total})"
|
||||||
|
|
||||||
|
if current/total < 0.5 do
|
||||||
|
left_progress(progress_notification, progress)
|
||||||
|
else
|
||||||
|
right_progress(progress_notification, progress)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp caluculate_progress(current, total) do
|
||||||
|
round( Float.floor((current/total) * @window_width))
|
||||||
|
end
|
||||||
|
|
||||||
|
def left_progress(progress_notification, progress) do
|
||||||
|
left = String.ljust("#{arrow(progress)}", @half_window - 1)
|
||||||
|
right = String.ljust(progress_notification, @half_window)
|
||||||
|
|
||||||
|
in_bars(left<>right)
|
||||||
|
end
|
||||||
|
|
||||||
|
def right_progress(progress_notification, progress) do
|
||||||
|
arrow_length = progress - @half_window - 1
|
||||||
|
left = String.ljust(progress_notification, @half_window - 1, ?=)
|
||||||
|
right = String.ljust(arrow(arrow_length), @half_window)
|
||||||
|
|
||||||
|
in_bars(left<>right)
|
||||||
|
end
|
||||||
|
|
||||||
|
defp in_bars(thing) do
|
||||||
|
"|" <> thing <> "|"
|
||||||
|
end
|
||||||
|
|
||||||
|
defp arrow(length) do
|
||||||
|
String.duplicate("=", length) <> ">"
|
||||||
|
end
|
||||||
|
|
||||||
def show_compile_error(error) do
|
def show_compile_error(error) do
|
||||||
IO.puts("")
|
IO.puts("")
|
||||||
format_error(error) |> IO.puts
|
format_error(error) |> IO.puts
|
||||||
|
|||||||
18
test/display_test.exs
Normal file
18
test/display_test.exs
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
defmodule DisplayTest do
|
||||||
|
use ExUnit.Case
|
||||||
|
|
||||||
|
test "puts counter on the right until half the koans are complete" do
|
||||||
|
bar = Display.progress_bar(%{total: 12, current: 3})
|
||||||
|
assert bar == "|==========> (3/12) |"
|
||||||
|
end
|
||||||
|
|
||||||
|
test "puts the counter on the left after half the koans are complete" do
|
||||||
|
bar = Display.progress_bar(%{total: 12, current: 10})
|
||||||
|
assert bar == "|(10/12)========================> |"
|
||||||
|
end
|
||||||
|
|
||||||
|
test "full bar" do
|
||||||
|
bar = Display.progress_bar(%{total: 12, current: 12})
|
||||||
|
assert bar == "|(12/12)===============================>|"
|
||||||
|
end
|
||||||
|
end
|
||||||
Reference in New Issue
Block a user