Skip to content

Commit

Permalink
Resolving warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
kelvinst committed Sep 4, 2017
1 parent d6cdb84 commit 985f0c0
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 36 deletions.
13 changes: 2 additions & 11 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
language: elixir

elixir:
- 1.3.0
- 1.2.0
-otp_release: 18.0

matrix:
include:
- elixir: 1.1.0
otp_release: 17.3
- elixir: 1.0.5
otp_release: 17.3
elixir: 1.3.0
otp_release: 18.0
2 changes: 1 addition & 1 deletion lib/progress_bar/animation_server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ defmodule ProgressBar.AnimationServer do

# This timer is automatically cancelled when the server goes away.
interval = config[:interval]
Process.send_after(self, :tick, interval)
Process.send_after(self(), :tick, interval)

{config, count + 1}
end
Expand Down
2 changes: 1 addition & 1 deletion lib/progress_bar/bytes.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ defmodule ProgressBar.Bytes do

defp format_without_unit(bytes, bytes_to_determine_unit) do
{divisor, _} = divisor_and_unit(bytes_to_determine_unit)
bytes / divisor |> to_s
to_s(bytes / divisor)
end

defp divisor_and_unit(bytes) when bytes < @mb, do: {@kb, "KB"}
Expand Down
37 changes: 24 additions & 13 deletions lib/progress_bar/determinate.ex
Original file line number Diff line number Diff line change
@@ -1,26 +1,31 @@
defmodule ProgressBar.Determinate do
alias ProgressBar.Bytes

@default_format [
bar: "=",
blank: " ",
left: "|",
right: "|",
percent: true,
bytes: false,
suffix: false,
bar_color: [],
blank_color: [],
width: :auto,
]

def render(current, total, custom_format \\ @default_format)
when current <= total
do
format = Keyword.merge(@default_format, custom_format)
def render(current, total, custom_format) when current <= total do
format =
@default_format
|> Keyword.merge(custom_format)
|> Enum.into(%{})

percent = current / total * 100 |> round

suffix = [
formatted_percent(format[:percent], percent),
bytes(format[:bytes], current, total),
format |> get_suffix() |> formatted_suffix(current, total),
newline_if_complete(current, total),
]

Expand All @@ -34,19 +39,25 @@ defmodule ProgressBar.Determinate do

# Private

defp formatted_percent(false, _), do: ""
defp formatted_percent(true, number) do
" " <> String.rjust(Integer.to_string(number), 3) <> "%"
end
defp get_suffix(%{suffix: false, bytes: true}), do: :bytes
defp get_suffix(%{suffix: suffix}), do: suffix

defp bytes(false, _, _), do: ""
defp bytes(true, total, total) do
" (" <> ProgressBar.Bytes.format(total) <> ")"
defp formatted_percent(false, _) do
""
end
defp bytes(true, current, total) do
" (" <> ProgressBar.Bytes.format(current, total) <> ")"
defp formatted_percent(true, number) do
number
|> Integer.to_string()
|> String.pad_leading(4)
|> Kernel.<>("%")
end

defp formatted_suffix(:count, total, total), do: " (#{total})"
defp formatted_suffix(:count, current, total), do: " (#{current}/#{total})"
defp formatted_suffix(:bytes, total, total), do: " (#{Bytes.format(total)})"
defp formatted_suffix(:bytes, current, total), do: " (#{Bytes.format(current, total)})"
defp formatted_suffix(false, _, _), do: ""

defp newline_if_complete(total, total), do: "\n"
defp newline_if_complete(_, _), do: ""
end
6 changes: 3 additions & 3 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ defmodule ProgressBar.Mixfile do
[
app: :progress_bar,
version: "1.6.1",
elixir: "~> 1.0",
elixir: "~> 1.3",
description: "Command-line progress bars and spinners.",
package: package,
package: package(),
build_embedded: Mix.env == :prod,
start_permanent: Mix.env == :prod,
deps: deps,
deps: deps(),
]
end

Expand Down
4 changes: 2 additions & 2 deletions test/bytes_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ defmodule BytesTest do
end

defp mb_to_bytes(mb) do
mb * @mb |> trunc
trunc(mb * @mb)
end

defp kb_to_bytes(kb) do
kb * @kb |> trunc
trunc(kb * @kb)
end
end
12 changes: 10 additions & 2 deletions test/determinate_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,19 @@ defmodule DeterminateTest do
assert bar =~ IO.chardata_to_string([" ", IO.ANSI.reset])
end

test "bytes: true" do
test "suffix: :bytes" do
mb = 1_000_000
format = [bytes: true, width: @width]
format = [suffix: :bytes, width: @width]
assert_bar ProgressBar.render(0, mb, format) == "| | 0% (0.00/1.00 MB)"
assert_bar ProgressBar.render((mb/2), mb, format) == "|================================================== | 50% (0.50/1.00 MB)"
assert_bar ProgressBar.render(mb, mb, format) == "|====================================================================================================| 100% (1.00 MB)"
end

test "suffix: :count" do
mb = 100
format = [suffix: :count, width: @width]
assert_bar ProgressBar.render(0, mb, format) == "| | 0% (0/100)"
assert_bar ProgressBar.render(50, mb, format) == "|================================================== | 50% (50/100)"
assert_bar ProgressBar.render(mb, mb, format) == "|====================================================================================================| 100% (100)"
end
end
2 changes: 1 addition & 1 deletion test/indeterminate_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ defmodule IndeterminateTest do
test "passes through the function's return value" do
capture_io fn ->
value = ProgressBar.render_indeterminate(fn -> :fun_return end)
send self, value
send self(), value
end

assert_received :fun_return
Expand Down
4 changes: 2 additions & 2 deletions test/spinner_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ defmodule SpinnerTest do
test "passes through the function's return value" do
capture_io fn ->
value = ProgressBar.render_spinner(fn -> :fun_return end)
send self, value
send self(), value
end

assert_received :fun_return
Expand All @@ -88,7 +88,7 @@ defmodule SpinnerTest do

defp split_frames(string) do
string
|> String.strip
|> String.trim()
|> String.split(Utils.ansi_prefix)
|> Enum.reject(&(&1 == ""))
end
Expand Down

0 comments on commit 985f0c0

Please sign in to comment.