Skip to content

Commit 70c7669

Browse files
committed
Improve progress bar
This improves progress bar by: - Adding progress percentage - Calculating progress bar underline based on the text length of progress bar
1 parent 0920d10 commit 70c7669

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

lib/display.ex

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,13 @@ defmodule Display do
5656
end
5757

5858
defp format(failure, module, name) do
59+
progress_bar = ProgressBar.progress_bar(Tracker.summarize())
60+
progress_bar_underline = String.duplicate("-", String.length(progress_bar))
5961
"""
6062
#{Intro.intro(module, Tracker.visited())}
6163
Now meditate upon #{format_module(module)}
62-
#{ProgressBar.progress_bar(Tracker.summarize())}
63-
----------------------------------------
64+
#{progress_bar}
65+
#{progress_bar_underline}
6466
#{name}
6567
#{Failure.format_failure(failure)}
6668
"""

lib/display/progress_bar.ex

Lines changed: 6 additions & 1 deletion
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

0 commit comments

Comments
 (0)