Improve progress bar

This improves progress bar by:
- Adding progress percentage
- Calculating progress bar underline based on the text length of progress bar
This commit is contained in:
Iftakhar Husan
2024-03-01 01:01:14 +02:00
parent 0920d10801
commit 70c76693a6
2 changed files with 10 additions and 3 deletions

View File

@@ -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)
end
defp build_arrow(0), do: ""
defp build_arrow(length) do