Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix inconsistent printing of meditation #289

Merged
merged 4 commits into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/elixir.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
name: Elixir CI

on:
workflow_dispatch:
push:
branches: [ master ]
pull_request:
Expand Down
19 changes: 10 additions & 9 deletions lib/display.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,14 @@ defmodule Display do
{:noreply, %{state | clear_screen: false}}
end

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

{:noreply, state}
{:reply, :ok, state}
end

def handle_cast(:clear_screen, state) do
{:noreply, state}
def handle_call(:clear_screen, _from, state) do
{:reply, :ok, state}
end

def invalid_koan(koan, modules) do
Expand All @@ -53,15 +52,17 @@ defmodule Display do
end

def clear_screen do
GenServer.cast(__MODULE__, :clear_screen)
GenServer.call(__MODULE__, :clear_screen)
end

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())}
Now meditate upon #{format_module(module)}
#{ProgressBar.progress_bar(Tracker.summarize())}
----------------------------------------
#{progress_bar}
#{progress_bar_underline}
#{name}
#{Failure.format_failure(failure)}
"""
Expand Down
7 changes: 6 additions & 1 deletion lib/display/progress_bar.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,19 @@ defmodule Display.ProgressBar do

def progress_bar(%{current: current, total: total}) do
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

defp calculate_progress(current, total) do
round(current / total * @progress_bar_length)
end

defp calculate_percentage(current, total) do
Float.round(current / total * 100, 1)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if this would look better without any decimal places?

0 of 12 -> 0% complete

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought it would be useful for people like me who wants to know the progress a little more precisely. But either way it's fine doesn't matter too much.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have a strong opinion. I tend to agree that decimals aren't terribly useful in this context.

end

defp build_arrow(0), do: ""

defp build_arrow(length) do
Expand Down
6 changes: 3 additions & 3 deletions test/display/progress_bar_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ defmodule ProgressBarTest do

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

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

test "full bar" do
bar = ProgressBar.progress_bar(%{total: 12, current: 12})
assert bar == "|=============================>| 12 of 12"
assert bar == "|=============================>| 12 of 12 -> 100.0% complete"
end
end
Loading