bar-of-progress 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +18 -1
- data/lib/bar-of-progress.rb +23 -6
- data/lib/bar_of_progress/version.rb +1 -1
- data/spec/lib/bar_of_progress_spec.rb +16 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a00863ba7e6db13e1bbe7790910a334363a45926
|
4
|
+
data.tar.gz: e3797c6dc2e946614f8586ac517d6d2c45b28710
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2aa5d021eb62a147844782eb370cacede39cab87f902be562fbaa3e1c1df893d5027486ee8ee27f5d963149a53e42c698a20ca95b9034ad00e685e76a1432769
|
7
|
+
data.tar.gz: d99025534b7856ac55cff2e20cdee7bc7b5f8479194c5f94cd38cb27a2e3d9805bf2e655fb36084959c7da5816db2f4f9623f72247e643a0292cfe46dc4c430c
|
data/README.md
CHANGED
@@ -6,7 +6,7 @@ Everyone knows that progress bars are one of the hard, unsolved problems of comp
|
|
6
6
|
|
7
7
|
## API
|
8
8
|
|
9
|
-
|
9
|
+
### Default progress bars
|
10
10
|
|
11
11
|
``` ruby
|
12
12
|
bar = BarOfProgress.new #=> defaults to completeness == "100"
|
@@ -17,3 +17,20 @@ bar.progress(50) #=> "[●●●●●◌◌◌◌◌]"
|
|
17
17
|
|
18
18
|
bar.progress(49) #=> "[●●●●◍◌◌◌◌◌]"
|
19
19
|
```
|
20
|
+
|
21
|
+
### Custom progress bars
|
22
|
+
|
23
|
+
``` ruby
|
24
|
+
bar = BarOfProgress.new(
|
25
|
+
:total => 115.5, #=> default (will be converted to BigDecimal): 100
|
26
|
+
:length => 14, #=> default (will be converted to Fixnum): 10
|
27
|
+
:braces => %w{( )}, #=> default: ["[", "]"]
|
28
|
+
:complete_indicator => "■", #=> default: "●"
|
29
|
+
:partial_indicator => "▤", #=> default: "◍"
|
30
|
+
:incomplete_indicator => "□", #=> default: "◌"
|
31
|
+
:precision => 18 #=> default (determines what is considered 'partial'): 20
|
32
|
+
)
|
33
|
+
|
34
|
+
bar.progress(30) #=> "(■■■▤□□□□□□□□□□)"
|
35
|
+
|
36
|
+
```
|
data/lib/bar-of-progress.rb
CHANGED
@@ -1,21 +1,38 @@
|
|
1
1
|
require "bar_of_progress/version"
|
2
2
|
|
3
|
+
require 'bigdecimal'
|
4
|
+
require 'bigdecimal/util'
|
5
|
+
|
3
6
|
class BarOfProgress
|
4
|
-
|
5
|
-
|
7
|
+
DEFAULTS = {
|
8
|
+
:total => 100,
|
9
|
+
:length => 10,
|
10
|
+
:braces => %w{[ ]},
|
11
|
+
:complete_indicator => "●",
|
12
|
+
:partial_indicator => "◍",
|
13
|
+
:incomplete_indicator => "◌",
|
14
|
+
:precision => 20
|
15
|
+
}
|
16
|
+
|
17
|
+
def initialize(options = {})
|
18
|
+
@options = DEFAULTS.merge(options)
|
19
|
+
|
20
|
+
#massage data because eww.
|
21
|
+
@options[:total] = @options[:total].to_d
|
22
|
+
@options[:length] = @options[:length].to_i
|
6
23
|
end
|
7
24
|
|
8
25
|
def progress(amount = 0)
|
9
|
-
bubbles =
|
26
|
+
bubbles = clamp(((amount.to_d / @options[:total]) * @options[:length]), 0, @options[:length]).to_d
|
10
27
|
full_bubbles = bubbles.floor
|
11
|
-
partial_bubbles = bubbles % 1 == 0 ? 0 : 1
|
12
|
-
"[#{chars(
|
28
|
+
partial_bubbles = bubbles.truncate(@options[:precision]) % 1 == 0 ? 0 : 1
|
29
|
+
"#{@options[:braces][0]}#{chars(@options[:complete_indicator], full_bubbles)}#{chars(@options[:partial_indicator], partial_bubbles)}#{chars(@options[:incomplete_indicator], (@options[:length] - full_bubbles - partial_bubbles))}#{@options[:braces][1]}"
|
13
30
|
end
|
14
31
|
|
15
32
|
private
|
16
33
|
|
17
34
|
# This method is amazing.
|
18
|
-
def
|
35
|
+
def clamp(n, min, max)
|
19
36
|
[[n, min].max, max].min
|
20
37
|
end
|
21
38
|
|
@@ -12,4 +12,20 @@ describe BarOfProgress do
|
|
12
12
|
Then { subject.progress(100) == "[●●●●●●●●●●]" }
|
13
13
|
Then { subject.progress(101) == "[●●●●●●●●●●]" }
|
14
14
|
end
|
15
|
+
|
16
|
+
context "awesome bejazzled custom progress bar" do
|
17
|
+
subject { BarOfProgress.new(
|
18
|
+
:total => 115.5,
|
19
|
+
:length => 14,
|
20
|
+
:braces => %w{( )},
|
21
|
+
:complete_indicator => "■",
|
22
|
+
:partial_indicator => "▤",
|
23
|
+
:incomplete_indicator => "□"
|
24
|
+
) }
|
25
|
+
|
26
|
+
Then { subject.progress == "(□□□□□□□□□□□□□□)" }
|
27
|
+
Then { subject.progress(30) == "(■■■▤□□□□□□□□□□)" }
|
28
|
+
Then { subject.progress(33) == "(■■■■□□□□□□□□□□)" }
|
29
|
+
Then { subject.progress(115.5) == "(■■■■■■■■■■■■■■)" }
|
30
|
+
end
|
15
31
|
end
|