console_progress_bar 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 TODO: Write your name
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # ConsoleProgressBar
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'console_progress_bar'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install console_progressbar
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'console_progress_bar/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "console_progress_bar"
8
+ spec.version = ConsoleProgressBar::VERSION
9
+ spec.authors = ["Mehmet Emin İNAÇ"]
10
+ spec.email = ["mehmetemininac@gmail.com"]
11
+ spec.description = %q{useful progress bar gem for console usage}
12
+ spec.summary = %q{Progress bar implementation for console scripts which you can show elapsed time and remaining time with animation or progressbar or current percent of total transactions}
13
+ spec.homepage = "http://github.com/mehmetemininac/progressbar"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ end
@@ -0,0 +1,47 @@
1
+ module ConsoleProgressBar
2
+
3
+ class Animator
4
+
5
+ def initialize(total_count = 100, with_elapsed_time = false, with_remaining_time = false, increment_size = 1)
6
+ @total = total_count
7
+ @current = 0
8
+ @recent = -1
9
+ @increment_size = increment_size
10
+ @output = "-|-"
11
+ @with_elapsed_time = with_elapsed_time
12
+ @with_remaining_time = with_remaining_time
13
+ @start = Time.now
14
+ @prev_time = Time.now
15
+ @avarage_inceament_time = 0
16
+ @prev_extra_length = 0
17
+ @output_length = 0
18
+ end
19
+
20
+ def animate
21
+ @current += @increment_size
22
+ if @current >= @total
23
+ finished
24
+ else
25
+ @now = (@current * 1.0 / @total * 100).to_i
26
+ if @recent != @now
27
+ @recent = @now
28
+ shift_back(@output_length + @prev_extra_length)
29
+ @prev_extra_length = 0
30
+ case @now % 3
31
+ when 0 then @output = "-|-"
32
+ when 1 then @output = "-/-"
33
+ when 2 then @output = "-\\-"
34
+ end
35
+ print @output
36
+ @output_length = @output.length
37
+ end
38
+ prepare_extras
39
+ end
40
+ end
41
+
42
+ include PrintTools
43
+ include Extras
44
+
45
+ end
46
+
47
+ end
@@ -0,0 +1,55 @@
1
+ module ConsoleProgressBar
2
+
3
+ class Bar
4
+
5
+ def initialize(total_count = 100, with_elapsed_time = false, with_remaining_time = false, increment_size = 1, width = 20)
6
+ @total = total_count
7
+ @current = 0
8
+ @recent = -1
9
+ @width = width
10
+ @increment_size = increment_size
11
+ @output = "|"
12
+ @width.times do
13
+ @output += " "
14
+ end
15
+ @output += "|"
16
+ @with_elapsed_time = with_elapsed_time
17
+ @with_remaining_time = with_remaining_time
18
+ @start = Time.now
19
+ @prev_time = Time.now
20
+ @avarage_inceament_time = 0
21
+ @prev_extra_length = 0
22
+ @output_length = 0
23
+ end
24
+
25
+ def draw
26
+ @current += @increment_size
27
+ if @current >= @total
28
+ finished
29
+ else
30
+ @now = (@current * 1.0 / @total * @width).to_i
31
+ if @recent != @now
32
+ @recent = @now
33
+ shift_back(@output_length + @prev_extra_length)
34
+ @prev_extra_length = 0
35
+ @output = "|"
36
+ @now.times do
37
+ @output += "#"
38
+ end
39
+ (@width - @now).times do
40
+ @output += " "
41
+ end
42
+ @output += "|"
43
+ print @output
44
+ @output_length = @output.length
45
+ end
46
+ prepare_extras
47
+ end
48
+ end
49
+
50
+ include PrintTools
51
+ include Extras
52
+
53
+ end
54
+
55
+ end
@@ -0,0 +1,43 @@
1
+ module ConsoleProgressBar
2
+
3
+ class Counter
4
+
5
+ def initialize(total_count = 100, with_elapsed_time = false, with_remaining_time = false, increment_size = 1)
6
+ @total = total_count
7
+ @current = 0
8
+ @recent = -1
9
+ @increment_size = increment_size
10
+ @output = "0%"
11
+ @with_elapsed_time = with_elapsed_time
12
+ @with_remaining_time = with_remaining_time
13
+ @start = Time.now
14
+ @prev_time = Time.now
15
+ @avarage_inceament_time = 0
16
+ @prev_extra_length = 0
17
+ @output_length = 0
18
+ end
19
+
20
+ def increase
21
+ @current += @increment_size
22
+ if @current >= @total
23
+ finished
24
+ else
25
+ @now = (@current * 1.0 / @total * 100).to_i
26
+ if @recent != @now
27
+ @recent = @now
28
+ shift_back(@output_length + @prev_extra_length)
29
+ @prev_extra_length = 0
30
+ @output = @now.to_s + "%"
31
+ print @output
32
+ @output_length = @output.length
33
+ end
34
+ prepare_extras
35
+ end
36
+ end
37
+
38
+ include PrintTools
39
+ include Extras
40
+
41
+ end
42
+
43
+ end
@@ -0,0 +1,33 @@
1
+ module ConsoleProgressBar
2
+
3
+ module Extras
4
+
5
+ private
6
+ def prepare_extras
7
+ extra_infos = ""
8
+ extra_infos += calculate_elapsed_time if @with_elapsed_time
9
+ extra_infos += calculate_remaining_time if @with_remaining_time
10
+ shift_back(@prev_extra_length)
11
+ print extra_infos
12
+ @prev_extra_length = extra_infos.length
13
+ end
14
+
15
+ def calculate_elapsed_time
16
+ elapsed_time = (Time.now - @start).to_i
17
+ minutes, seconds = elapsed_time.divmod(60)
18
+ hours, minutes = minutes.divmod(60)
19
+ " Elapsed Time = %02d:%02d:%02d" % [hours, minutes, seconds]
20
+ end
21
+
22
+ def calculate_remaining_time
23
+ tmp = Time.now - @prev_time
24
+ @avarage_inceament_time = (@avarage_inceament_time + tmp) / 2.0
25
+ minutes, seconds = (@avarage_inceament_time * (@total - @current)).divmod(60)
26
+ hours, minutes = minutes.divmod(60)
27
+ @prev_time = Time.now
28
+ " Time Remaining = %02d:%02d:%02d" % [hours, minutes, seconds]
29
+ end
30
+
31
+ end
32
+
33
+ end
@@ -0,0 +1,25 @@
1
+ module ConsoleProgressBar
2
+
3
+ module PrintTools
4
+
5
+ private
6
+ def shift_back(length)
7
+ length.times do
8
+ print "\b"
9
+ end
10
+ end
11
+
12
+ def finished
13
+ shift_back(@output_length + @prev_extra_length)
14
+ @end ||= (Time.now - @start).round(2)
15
+ @output = "100% Finished in #{@end} seconds"
16
+ ((@output_length + @prev_extra_length) - @output.length).times do
17
+ @output += " "
18
+ end
19
+ @output_length = @output.length
20
+ print @output
21
+ end
22
+
23
+ end
24
+
25
+ end
@@ -0,0 +1,3 @@
1
+ module ConsoleProgressBar
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,40 @@
1
+ require_relative "console_progress_bar/version"
2
+ require_relative "console_progress_bar/print_tools"
3
+ require_relative "console_progress_bar/extras"
4
+ require_relative "console_progress_bar/counter"
5
+ require_relative "console_progress_bar/animator"
6
+ require_relative "console_progress_bar/bar"
7
+
8
+ module ConsoleProgressBar
9
+
10
+ class ProgressBar
11
+
12
+ def counter(options = {})
13
+ init_options(options)
14
+ ConsoleProgressBar::Counter.new(@total, @with_elapsed_time, @with_remaining_time, @increment_size)
15
+ end
16
+
17
+ def animator(options = {})
18
+ init_options(options)
19
+ ConsoleProgressBar::Animator.new(@total, @with_elapsed_time, @with_remaining_time, @increment_size)
20
+ end
21
+
22
+ def bar(options = {})
23
+ init_options(options)
24
+ ConsoleProgressBar::Bar.new(@total, @with_elapsed_time, @with_remaining_time, @increment_size, @width)
25
+ end
26
+
27
+ private
28
+ def init_options(options = {})
29
+ unless options.nil?
30
+ @total = options[:total].nil? ? 100 : options[:total]
31
+ @with_elapsed_time = options[:with_elapsed_time].nil? ? false : options[:with_elapsed_time]
32
+ @with_remaining_time = options[:with_remaining_time].nil? ? false : options[:with_remaining_time]
33
+ @increment_size = options[:increment_size].nil? ? 1 : options[:increment_size]
34
+ @width = options[:width].nil? ? 20 : options[:width]
35
+ end
36
+ end
37
+
38
+ end
39
+
40
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: console_progress_bar
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - "Mehmet Emin \xC4\xB0NA\xC3\x87"
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2013-10-25 00:00:00 +03:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: bundler
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ~>
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 1
29
+ - 3
30
+ version: "1.3"
31
+ type: :development
32
+ version_requirements: *id001
33
+ - !ruby/object:Gem::Dependency
34
+ name: rake
35
+ prerelease: false
36
+ requirement: &id002 !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ segments:
41
+ - 0
42
+ version: "0"
43
+ type: :development
44
+ version_requirements: *id002
45
+ description: useful progress bar gem for console usage
46
+ email:
47
+ - mehmetemininac@gmail.com
48
+ executables: []
49
+
50
+ extensions: []
51
+
52
+ extra_rdoc_files: []
53
+
54
+ files:
55
+ - .gitignore
56
+ - Gemfile
57
+ - LICENSE.txt
58
+ - README.md
59
+ - Rakefile
60
+ - console_progress_bar.gemspec
61
+ - lib/console_progress_bar.rb
62
+ - lib/console_progress_bar/animator.rb
63
+ - lib/console_progress_bar/bar.rb
64
+ - lib/console_progress_bar/counter.rb
65
+ - lib/console_progress_bar/extras.rb
66
+ - lib/console_progress_bar/print_tools.rb
67
+ - lib/console_progress_bar/version.rb
68
+ has_rdoc: true
69
+ homepage: http://github.com/mehmetemininac/progressbar
70
+ licenses:
71
+ - MIT
72
+ post_install_message:
73
+ rdoc_options: []
74
+
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ segments:
82
+ - 0
83
+ version: "0"
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ segments:
89
+ - 0
90
+ version: "0"
91
+ requirements: []
92
+
93
+ rubyforge_project:
94
+ rubygems_version: 1.3.6
95
+ signing_key:
96
+ specification_version: 3
97
+ summary: Progress bar implementation for console scripts which you can show elapsed time and remaining time with animation or progressbar or current percent of total transactions
98
+ test_files: []
99
+