Skip to content

Commit

Permalink
Merge remote-tracking branch 'kelvinst/count-suffix'
Browse files Browse the repository at this point in the history
  • Loading branch information
henrik committed Dec 21, 2018
2 parents 2118d44 + 1dc0807 commit 570e38a
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 36 deletions.
17 changes: 15 additions & 2 deletions README.md
Expand Up @@ -129,12 +129,25 @@ Instead of:

…============| 100%

#### `bytes: true`
#### `suffix: :count`

This option causes the values to be printed on the suffix of your progress bar.

``` elixir
ProgressBar.render(9_751, 10_000, suffix: :count)
```

Output:

…========= | 97% (9751/10000)


#### `suffix: :bytes`

This option causes the values to be treated as bytes of data, showing those amounts next to the bar.

``` elixir
ProgressBar.render(2_034_237, 2_097_152, bytes: true)
ProgressBar.render(2_034_237, 2_097_152, suffix: :bytes)
```

Output:
Expand Down
2 changes: 1 addition & 1 deletion examples/00-gif.exs
Expand Up @@ -33,7 +33,7 @@ format = [
blank_color: [IO.ANSI.magenta],
bar: "█",
blank: "░",
bytes: true,
suffix: :bytes,
]

Enum.each 0..1_000, fn (i) ->
Expand Down
2 changes: 1 addition & 1 deletion examples/04-bytes.exs
Expand Up @@ -2,6 +2,6 @@ IO.puts ""
IO.puts "Bytes of data:"

Enum.each 0..3_000, fn (i) ->
ProgressBar.render((i*1000), 3_000_000, bytes: true)
ProgressBar.render((i*1000), 3_000_000, suffix: :bytes)
:timer.sleep 1
end
33 changes: 16 additions & 17 deletions lib/progress_bar/determinate.ex
@@ -1,26 +1,27 @@
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
width: :auto,
]

def render(current, total, custom_format \\ @default_format)
when current <= total do
def render(current, total, custom_format \\ @default_format) when current <= total do
format = Keyword.merge(@default_format, custom_format)

percent = (current / total * 100) |> round

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

ProgressBar.BarFormatter.write(
Expand All @@ -34,20 +35,18 @@ defmodule ProgressBar.Determinate do
# Private

defp formatted_percent(false, _), do: ""

defp formatted_percent(true, number) do
" " <> String.pad_leading(Integer.to_string(number), 3) <> "%"
end

defp bytes(false, _, _), do: ""

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

defp bytes(true, current, total) do
" (" <> ProgressBar.Bytes.format(current, total) <> ")"
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: ""
Expand Down
27 changes: 12 additions & 15 deletions test/determinate_test.exs
Expand Up @@ -114,23 +114,20 @@ 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(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

assert_bar(
ProgressBar.render(mb, mb, format) ==
"|====================================================================================================| 100% (1.00 MB)"
)
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

0 comments on commit 570e38a

Please sign in to comment.