bar-of-progress 0.0.1 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 86e98d04262c8491ed88097371ecdd752c51c366
4
- data.tar.gz: e19e795fc5a466a1cea901892b7d514d7e9556cf
3
+ metadata.gz: a00863ba7e6db13e1bbe7790910a334363a45926
4
+ data.tar.gz: e3797c6dc2e946614f8586ac517d6d2c45b28710
5
5
  SHA512:
6
- metadata.gz: e68d1c2e0ddec2e343f2864cd4f6840d03ad5790b57c19df690913e5dbaec4e8fb817655e8dc474d29557dd1dbe19a15221993ed3a54e7493736c969c5f5387c
7
- data.tar.gz: 025d5d3f511d4d0099dfb4517262a840c083f949b70e740224b8d4d589b20e70be9d04198495f2db288a3a1ca1734a512ff8ffc2210e14757570d87e4d7fc08b
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
- Boring, normal progress bars:
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
+ ```
@@ -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
- def initialize
5
- @complete = 100.0
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 = betwixt(((amount / @complete) * 10), 0, 10)
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('●', full_bubbles)}#{chars('◍', partial_bubbles)}#{chars('◌', (10 - full_bubbles - partial_bubbles))}]"
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 betwixt(n, min, max)
35
+ def clamp(n, min, max)
19
36
  [[n, min].max, max].min
20
37
  end
21
38
 
@@ -1,3 +1,3 @@
1
1
  class BarOfProgress
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bar-of-progress
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Justin Searls