progress_bar 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ # A sample Gemfile
2
+ source "http://rubygems.org"
3
+
4
+ gem "highline"
5
+ gem "options"
6
+
7
+ group :development do
8
+ end
9
+
10
+
data/README.mkd ADDED
@@ -0,0 +1,67 @@
1
+
2
+ # ProgressBar
3
+
4
+ *ProgressBar* is a simple Ruby library for displaying progress of
5
+ long-running tasks on the console. It is intended to be easy to use
6
+ first, and configurable for additional needs.
7
+
8
+ # Installation
9
+
10
+ gem install progress_bar
11
+
12
+ # Examples
13
+
14
+ ## The Easy Way
15
+
16
+
17
+ require 'progress_bar'
18
+ bar = ProgressBar.new
19
+
20
+ 100.times do
21
+ sleep 0.1
22
+ bar.increment!
23
+ end
24
+
25
+ Produces output like:
26
+
27
+ [####################################### ] [ 59.00%] [00:06]
28
+
29
+ *Note: It may not be exactly like this. I might have changed the default
30
+ meters between now and when I wrote this readme, and forgotten to update
31
+ it.*
32
+
33
+ ## Setting the Max
34
+
35
+ Usually, the defaults should be fine, the only thing you'll need to
36
+ tweak is the max.
37
+
38
+ bar = ProgressBar.new(1000)
39
+
40
+ ## Larger Steps
41
+
42
+ If you want to process several things, and update less often, you can
43
+ pass a number to `#increment!`
44
+
45
+ bar.increment! 42
46
+
47
+ ## The Configurable Way
48
+
49
+ bar = ProgressBar.new do |cfg|
50
+ cfg.width = 60
51
+ cfg.max = 1000
52
+ cfg.color = true
53
+ cfg.meters = [:bar, :eta]
54
+
55
+ cfg.bar.width = 40
56
+ cfg.bar.fill = '#'
57
+ cfg.bar.empty = ' '
58
+ cfg.bar.prefix = '['
59
+ cfg.bar.suffix = ']'
60
+
61
+ cfg.eta.format = "%H:%M:%S"
62
+ end
63
+
64
+
65
+
66
+
67
+
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,5 @@
1
+
2
+ class ProgressBar
3
+ class Meter
4
+ end
5
+ end
@@ -0,0 +1,31 @@
1
+
2
+ class ProgressBar
3
+ class Bar < Meter
4
+
5
+ attr_reader :count, :max, :width
6
+
7
+ def initialize(count, max, width)
8
+ @count, @max, @width = count, max, width
9
+ end
10
+
11
+ def to_s
12
+ "[" +
13
+ "#" * progress_width +
14
+ " " * remaining_width +
15
+ "]"
16
+ end
17
+
18
+ def bar_width
19
+ width - 2
20
+ end
21
+
22
+ def progress_width
23
+ ((count.to_f / max) * bar_width).ceil
24
+ end
25
+
26
+ def remaining_width
27
+ bar_width - progress_width
28
+ end
29
+
30
+ end
31
+ end
@@ -0,0 +1,24 @@
1
+
2
+ class ProgressBar
3
+ class Counter < Meter
4
+
5
+ attr_reader :count, :max
6
+
7
+ def initialize(count, max)
8
+ @count, @max = count, max
9
+ end
10
+
11
+ def to_s
12
+ "[%#{max_counter_width}i/%i]" % [count, max]
13
+ end
14
+
15
+ def width
16
+ max.to_s.length * 2 + 3
17
+ end
18
+
19
+ def max_counter_width
20
+ max.to_s.length
21
+ end
22
+
23
+ end
24
+ end
@@ -0,0 +1,26 @@
1
+ class ProgressBar
2
+ class Elapsed < Meter
3
+
4
+ include TimeFormatter
5
+
6
+ attr_reader :start
7
+
8
+ def initialize start
9
+ @start = start
10
+ end
11
+
12
+ def elapsed
13
+ Time.now - start
14
+ end
15
+
16
+ def to_s
17
+ "[#{format_interval(elapsed)}]"
18
+ end
19
+
20
+ def width
21
+ format_interval(elapsed).length + 2
22
+ end
23
+
24
+ end
25
+ end
26
+
@@ -0,0 +1,36 @@
1
+ class ProgressBar
2
+ class ETA < Meter
3
+
4
+ include TimeFormatter
5
+
6
+ attr_reader :start, :count, :max
7
+
8
+ def initialize start, count, max
9
+ @start, @count, @max = start, count, max
10
+ end
11
+
12
+ def elapsed
13
+ Time.now - start
14
+ end
15
+
16
+ def average
17
+ if count == 0
18
+ 0
19
+ else
20
+ elapsed / count
21
+ end
22
+ end
23
+
24
+ def remaining
25
+ (max - count) * average
26
+ end
27
+
28
+ def to_s
29
+ "[ETA: #{format_interval(remaining)}]"
30
+ end
31
+
32
+ def width
33
+ format_interval(elapsed).length + 7
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,20 @@
1
+
2
+ class ProgressBar
3
+ class Percentage < Meter
4
+
5
+ attr_reader :count, :max
6
+
7
+ def initialize count, max
8
+ @count, @max = count, max
9
+ end
10
+
11
+ def to_s
12
+ "[%6.2f%%]" % (count.to_f / max * 100)
13
+ end
14
+
15
+ def width
16
+ 9
17
+ end
18
+
19
+ end
20
+ end
@@ -0,0 +1,10 @@
1
+
2
+ %w[
3
+ counter
4
+ bar
5
+ percentage
6
+ elapsed
7
+ eta
8
+ ].each do |meter|
9
+ require File.join(File.dirname(__FILE__), 'meters', meter)
10
+ end
@@ -0,0 +1,15 @@
1
+
2
+ class ProgressBar
3
+ module TimeFormatter
4
+
5
+ def format_interval(interval)
6
+ if interval > 3600
7
+ "%02i:%02i:%02i" % [interval/3600, interval%3600/60, interval%60]
8
+ else
9
+ "%02i:%02i" % [interval/60, interval%60]
10
+ end
11
+ end
12
+
13
+ end
14
+ end
15
+
@@ -0,0 +1,3 @@
1
+ module ProgressBar
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,72 @@
1
+
2
+ require 'options'
3
+ require 'highline'
4
+
5
+ require File.join(File.dirname(__FILE__), 'progress_bar', 'time_formatter')
6
+ require File.join(File.dirname(__FILE__), 'progress_bar', 'meter')
7
+ require File.join(File.dirname(__FILE__), 'progress_bar', 'meters')
8
+
9
+ class ProgressBar
10
+
11
+ attr_accessor :count, :max, :start
12
+
13
+ def initialize *args, &block
14
+ args, options = Options.parse args
15
+
16
+ @count = options[:count] || 0
17
+ @max = options[:max] || 100
18
+ @start = Time.now
19
+
20
+ @hl = HighLine.new
21
+ end
22
+
23
+ def increment!
24
+ self.count = count.succ
25
+ write
26
+ end
27
+
28
+ def write
29
+ clear!
30
+ print to_s
31
+ end
32
+
33
+ def bar(width = terminal_width)
34
+ Bar.new(count, max, width)
35
+ end
36
+
37
+ def counter
38
+ Counter.new(count, max)
39
+ end
40
+
41
+ def percentage
42
+ Percentage.new(count, max)
43
+ end
44
+
45
+ def elapsed
46
+ Elapsed.new(start)
47
+ end
48
+
49
+ def eta
50
+ ETA.new(start, count, max)
51
+ end
52
+
53
+ def to_s
54
+ width = terminal_width -
55
+ (counter.width + 1) -
56
+ (percentage.width + 1) -
57
+ (elapsed.width + 1) -
58
+ (eta.width + 1)
59
+ "#{bar(width)} #{counter} #{percentage} #{elapsed} #{eta}"
60
+ end
61
+
62
+ protected
63
+
64
+ def clear!
65
+ print "\r"
66
+ end
67
+
68
+ def terminal_width
69
+ @hl.output_cols.to_i
70
+ end
71
+
72
+ end
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "progress_bar/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "progress_bar"
7
+ s.version = ProgressBar::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Paul Sasauskas"]
10
+ s.email = ["psadauskas@gmail.com"]
11
+ s.homepage = "http://www.github.com/paul/progress_bar"
12
+ s.summary = %q{Simple Progress Bar for output to a terminal}
13
+ s.description = %q{Give people feedback about long-running tasks without overloading them with information: Use a progress bar, like Curl or Wget!}
14
+
15
+ s.rubyforge_project = "progress_bar"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+ end
@@ -0,0 +1,15 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), 'test_helper'))
2
+
3
+ describe "Counter formatter" do
4
+ before do
5
+ @counter = ProgressBar::Counter.new(50, 100)
6
+ end
7
+
8
+ it 'should format properly' do
9
+ @counter.to_s.must_equal "[ 50/100]"
10
+ end
11
+
12
+ it 'should have the correct width' do
13
+ @counter.width.must_equal 9
14
+ end
15
+ end
@@ -0,0 +1,33 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), 'test_helper'))
2
+
3
+ describe "Elapsed formatter" do
4
+ before do
5
+ start = Time.now - 60
6
+ @elapsed = ProgressBar::Elapsed.new(start)
7
+ end
8
+
9
+ it 'should format properly' do
10
+ @elapsed.to_s.must_equal "[01:00]"
11
+ end
12
+
13
+ it 'should have the correct width' do
14
+ @elapsed.width.must_equal 7
15
+ end
16
+
17
+ describe "long running tasks" do
18
+ before do
19
+ start = Time.now - 6*3600
20
+ @elapsed = ProgressBar::Elapsed.new(start)
21
+ end
22
+
23
+ it 'should format properly' do
24
+ @elapsed.to_s.must_equal "[06:00:00]"
25
+ end
26
+
27
+ it 'should have the correct width' do
28
+ @elapsed.width.must_equal 10
29
+ end
30
+
31
+ end
32
+
33
+ end
data/test/eta_test.rb ADDED
@@ -0,0 +1,44 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), 'test_helper'))
2
+
3
+ describe "ETA formatter" do
4
+ before do
5
+ start = Time.now - 60
6
+ @eta = ProgressBar::ETA.new(start, 50, 100)
7
+ end
8
+
9
+ it 'should format properly' do
10
+ @eta.to_s.must_equal "[ETA: 01:00]"
11
+ end
12
+
13
+ it 'should have the correct width' do
14
+ @eta.width.must_equal 12
15
+ end
16
+
17
+ it 'should be 0 at the end' do
18
+ eta = ProgressBar::ETA.new(Time.now - 5, 100, 100)
19
+ eta.remaining.must_equal 0
20
+ end
21
+
22
+ it 'should not have an error at the start' do
23
+ eta = ProgressBar::ETA.new(Time.now, 0, 100)
24
+ eta.remaining.must_equal 0
25
+ eta.to_s.must_equal "[ETA: 00:00]"
26
+ end
27
+
28
+ describe "long running tasks" do
29
+ before do
30
+ start = Time.now - 6*3600
31
+ @eta = ProgressBar::ETA.new(start, 50, 100)
32
+ end
33
+
34
+ it 'should format properly' do
35
+ @eta.to_s.must_equal "[ETA: 06:00:00]"
36
+ end
37
+
38
+ it 'should have the correct width' do
39
+ @eta.width.must_equal 15
40
+ end
41
+
42
+ end
43
+
44
+ end
@@ -0,0 +1,9 @@
1
+
2
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib/progress_bar'))
3
+
4
+ bar = ProgressBar.new
5
+
6
+ 100.times do
7
+ sleep 0.1
8
+ bar.increment!
9
+ end
@@ -0,0 +1,6 @@
1
+
2
+ require 'minitest/spec'
3
+ require 'minitest/autorun'
4
+
5
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib/progress_bar'))
6
+
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: progress_bar
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.0
6
+ platform: ruby
7
+ authors:
8
+ - Paul Sasauskas
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-04-06 00:00:00 -06:00
14
+ default_executable:
15
+ dependencies: []
16
+
17
+ description: "Give people feedback about long-running tasks without overloading them with information: Use a progress bar, like Curl or Wget!"
18
+ email:
19
+ - psadauskas@gmail.com
20
+ executables: []
21
+
22
+ extensions: []
23
+
24
+ extra_rdoc_files: []
25
+
26
+ files:
27
+ - .gitignore
28
+ - Gemfile
29
+ - README.mkd
30
+ - Rakefile
31
+ - lib/progress_bar.rb
32
+ - lib/progress_bar/meter.rb
33
+ - lib/progress_bar/meters.rb
34
+ - lib/progress_bar/meters/bar.rb
35
+ - lib/progress_bar/meters/counter.rb
36
+ - lib/progress_bar/meters/elapsed.rb
37
+ - lib/progress_bar/meters/eta.rb
38
+ - lib/progress_bar/meters/percentage.rb
39
+ - lib/progress_bar/time_formatter.rb
40
+ - lib/progress_bar/version.rb
41
+ - progress_bar.gemspec
42
+ - test/counter_test.rb
43
+ - test/elapsed_test.rb
44
+ - test/eta_test.rb
45
+ - test/progres_bar_test.rb
46
+ - test/test_helper.rb
47
+ has_rdoc: true
48
+ homepage: http://www.github.com/paul/progress_bar
49
+ licenses: []
50
+
51
+ post_install_message:
52
+ rdoc_options: []
53
+
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: "0"
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: "0"
68
+ requirements: []
69
+
70
+ rubyforge_project: progress_bar
71
+ rubygems_version: 1.6.2
72
+ signing_key:
73
+ specification_version: 3
74
+ summary: Simple Progress Bar for output to a terminal
75
+ test_files:
76
+ - test/counter_test.rb
77
+ - test/elapsed_test.rb
78
+ - test/eta_test.rb
79
+ - test/progres_bar_test.rb
80
+ - test/test_helper.rb