Merge pull request #289 from Iftakharpy/master
Some checks failed
Elixir CI / Build and test (push) Has been cancelled
Some checks failed
Elixir CI / Build and test (push) Has been cancelled
Fix inconsistent printing of meditation
This commit is contained in:
1
.github/workflows/elixir.yml
vendored
1
.github/workflows/elixir.yml
vendored
@@ -1,6 +1,7 @@
|
|||||||
name: Elixir CI
|
name: Elixir CI
|
||||||
|
|
||||||
on:
|
on:
|
||||||
|
workflow_dispatch:
|
||||||
push:
|
push:
|
||||||
branches: [ master ]
|
branches: [ master ]
|
||||||
pull_request:
|
pull_request:
|
||||||
|
@@ -21,15 +21,14 @@ defmodule Display do
|
|||||||
{:noreply, %{state | clear_screen: false}}
|
{:noreply, %{state | clear_screen: false}}
|
||||||
end
|
end
|
||||||
|
|
||||||
def handle_cast(:clear_screen, %{clear_screen: true} = state) do
|
def handle_call(:clear_screen, _from, %{clear_screen: true} = state) do
|
||||||
IO.puts(ANSI.clear())
|
ANSI.clear <> ANSI.home |> IO.puts()
|
||||||
IO.puts(ANSI.home())
|
|
||||||
|
|
||||||
{:noreply, state}
|
{:reply, :ok, state}
|
||||||
end
|
end
|
||||||
|
|
||||||
def handle_cast(:clear_screen, state) do
|
def handle_call(:clear_screen, _from, state) do
|
||||||
{:noreply, state}
|
{:reply, :ok, state}
|
||||||
end
|
end
|
||||||
|
|
||||||
def invalid_koan(koan, modules) do
|
def invalid_koan(koan, modules) do
|
||||||
@@ -53,15 +52,17 @@ defmodule Display do
|
|||||||
end
|
end
|
||||||
|
|
||||||
def clear_screen do
|
def clear_screen do
|
||||||
GenServer.cast(__MODULE__, :clear_screen)
|
GenServer.call(__MODULE__, :clear_screen)
|
||||||
end
|
end
|
||||||
|
|
||||||
defp format(failure, module, name) do
|
defp format(failure, module, name) do
|
||||||
|
progress_bar = ProgressBar.progress_bar(Tracker.summarize())
|
||||||
|
progress_bar_underline = String.duplicate("-", String.length(progress_bar))
|
||||||
"""
|
"""
|
||||||
#{Intro.intro(module, Tracker.visited())}
|
#{Intro.intro(module, Tracker.visited())}
|
||||||
Now meditate upon #{format_module(module)}
|
Now meditate upon #{format_module(module)}
|
||||||
#{ProgressBar.progress_bar(Tracker.summarize())}
|
#{progress_bar}
|
||||||
----------------------------------------
|
#{progress_bar_underline}
|
||||||
#{name}
|
#{name}
|
||||||
#{Failure.format_failure(failure)}
|
#{Failure.format_failure(failure)}
|
||||||
"""
|
"""
|
||||||
|
@@ -4,14 +4,19 @@ defmodule Display.ProgressBar do
|
|||||||
|
|
||||||
def progress_bar(%{current: current, total: total}) do
|
def progress_bar(%{current: current, total: total}) do
|
||||||
arrow = calculate_progress(current, total) |> build_arrow
|
arrow = calculate_progress(current, total) |> build_arrow
|
||||||
|
progress_percentage = calculate_percentage(current, total)
|
||||||
|
|
||||||
"|" <> String.pad_trailing(arrow, @progress_bar_length) <> "| #{current} of #{total}"
|
"|" <> String.pad_trailing(arrow, @progress_bar_length) <> "| #{current} of #{total} -> #{progress_percentage}% complete"
|
||||||
end
|
end
|
||||||
|
|
||||||
defp calculate_progress(current, total) do
|
defp calculate_progress(current, total) do
|
||||||
round(current / total * @progress_bar_length)
|
round(current / total * @progress_bar_length)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp calculate_percentage(current, total) do
|
||||||
|
Float.round(current / total * 100, 1)
|
||||||
|
end
|
||||||
|
|
||||||
defp build_arrow(0), do: ""
|
defp build_arrow(0), do: ""
|
||||||
|
|
||||||
defp build_arrow(length) do
|
defp build_arrow(length) do
|
||||||
|
@@ -5,16 +5,16 @@ defmodule ProgressBarTest do
|
|||||||
|
|
||||||
test "empty bar" do
|
test "empty bar" do
|
||||||
bar = ProgressBar.progress_bar(%{total: 12, current: 0})
|
bar = ProgressBar.progress_bar(%{total: 12, current: 0})
|
||||||
assert bar == "| | 0 of 12"
|
assert bar == "| | 0 of 12 -> 0.0% complete"
|
||||||
end
|
end
|
||||||
|
|
||||||
test "puts counter on the right until half the koans are complete" do
|
test "puts counter on the right until half the koans are complete" do
|
||||||
bar = ProgressBar.progress_bar(%{total: 12, current: 3})
|
bar = ProgressBar.progress_bar(%{total: 12, current: 3})
|
||||||
assert bar == "|=======> | 3 of 12"
|
assert bar == "|=======> | 3 of 12 -> 25.0% complete"
|
||||||
end
|
end
|
||||||
|
|
||||||
test "full bar" do
|
test "full bar" do
|
||||||
bar = ProgressBar.progress_bar(%{total: 12, current: 12})
|
bar = ProgressBar.progress_bar(%{total: 12, current: 12})
|
||||||
assert bar == "|=============================>| 12 of 12"
|
assert bar == "|=============================>| 12 of 12 -> 100.0% complete"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Reference in New Issue
Block a user