Skip to content

Commit 2921a74

Browse files
authored
Merge pull request #289 from Iftakharpy/master
Fix inconsistent printing of meditation
2 parents 0eb56c2 + be4b4fd commit 2921a74

File tree

4 files changed

+20
-13
lines changed

4 files changed

+20
-13
lines changed

.github/workflows/elixir.yml

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
name: Elixir CI
22

33
on:
4+
workflow_dispatch:
45
push:
56
branches: [ master ]
67
pull_request:

lib/display.ex

+10-9
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,14 @@ defmodule Display do
2121
{:noreply, %{state | clear_screen: false}}
2222
end
2323

24-
def handle_cast(:clear_screen, %{clear_screen: true} = state) do
25-
IO.puts(ANSI.clear())
26-
IO.puts(ANSI.home())
24+
def handle_call(:clear_screen, _from, %{clear_screen: true} = state) do
25+
ANSI.clear <> ANSI.home |> IO.puts()
2726

28-
{:noreply, state}
27+
{:reply, :ok, state}
2928
end
3029

31-
def handle_cast(:clear_screen, state) do
32-
{:noreply, state}
30+
def handle_call(:clear_screen, _from, state) do
31+
{:reply, :ok, state}
3332
end
3433

3534
def invalid_koan(koan, modules) do
@@ -53,15 +52,17 @@ defmodule Display do
5352
end
5453

5554
def clear_screen do
56-
GenServer.cast(__MODULE__, :clear_screen)
55+
GenServer.call(__MODULE__, :clear_screen)
5756
end
5857

5958
defp format(failure, module, name) do
59+
progress_bar = ProgressBar.progress_bar(Tracker.summarize())
60+
progress_bar_underline = String.duplicate("-", String.length(progress_bar))
6061
"""
6162
#{Intro.intro(module, Tracker.visited())}
6263
Now meditate upon #{format_module(module)}
63-
#{ProgressBar.progress_bar(Tracker.summarize())}
64-
----------------------------------------
64+
#{progress_bar}
65+
#{progress_bar_underline}
6566
#{name}
6667
#{Failure.format_failure(failure)}
6768
"""

lib/display/progress_bar.ex

+6-1
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,19 @@ defmodule Display.ProgressBar do
44

55
def progress_bar(%{current: current, total: total}) do
66
arrow = calculate_progress(current, total) |> build_arrow
7+
progress_percentage = calculate_percentage(current, total)
78

8-
"|" <> String.pad_trailing(arrow, @progress_bar_length) <> "| #{current} of #{total}"
9+
"|" <> String.pad_trailing(arrow, @progress_bar_length) <> "| #{current} of #{total} -> #{progress_percentage}% complete"
910
end
1011

1112
defp calculate_progress(current, total) do
1213
round(current / total * @progress_bar_length)
1314
end
1415

16+
defp calculate_percentage(current, total) do
17+
Float.round(current / total * 100, 1)
18+
end
19+
1520
defp build_arrow(0), do: ""
1621

1722
defp build_arrow(length) do

test/display/progress_bar_test.exs

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@ defmodule ProgressBarTest do
55

66
test "empty bar" do
77
bar = ProgressBar.progress_bar(%{total: 12, current: 0})
8-
assert bar == "| | 0 of 12"
8+
assert bar == "| | 0 of 12 -> 0.0% complete"
99
end
1010

1111
test "puts counter on the right until half the koans are complete" do
1212
bar = ProgressBar.progress_bar(%{total: 12, current: 3})
13-
assert bar == "|=======> | 3 of 12"
13+
assert bar == "|=======> | 3 of 12 -> 25.0% complete"
1414
end
1515

1616
test "full bar" do
1717
bar = ProgressBar.progress_bar(%{total: 12, current: 12})
18-
assert bar == "|=============================>| 12 of 12"
18+
assert bar == "|=============================>| 12 of 12 -> 100.0% complete"
1919
end
2020
end

0 commit comments

Comments
 (0)