ruby-multi-progressbar 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b4503aefab48b055748422c629156860afb406eb
4
+ data.tar.gz: 7bfa320e01ea00909a8d2d5df1aeb4959c366d4c
5
+ SHA512:
6
+ metadata.gz: ce67aaf23552b8c0bcc5c54e27072a6ff32070386606e80ab11d1c230d46e24fecb2b061a67886f62b8fa6311aeb038698f7c907118846dc3b0c1af73f20170a
7
+ data.tar.gz: aeca9899def674e523d95bd95c45ac91bc8b7c4f374fdfb9c749105503fd553228b3017661aa3c8199de52e4a67a584486bd6730728206a4a4000e5947e6d619
@@ -0,0 +1,10 @@
1
+ == Ruby-MultiProgressBar
2
+
3
+ Displays multiple progress bars using Ncurses and ruby-progressbar
4
+ Useful for displaying the status of multiple test runs or multipile threads.
5
+
6
+ Uses +ruby-progressbar+ to render the bars.
7
+
8
+ == Credits
9
+
10
+ * Thanks to Peter Jaros for the initial version (https://github.com/Peeja/multi_progress_bar).
@@ -0,0 +1,93 @@
1
+ require 'ncurses'
2
+ require 'ruby-progressbar'
3
+ require 'delegate'
4
+
5
+ module MultiProgressBar
6
+ class << self
7
+ attr_reader :bars
8
+
9
+ # Set up the screen. Always call +MultiProgressBar.start+ before using progress bars.
10
+ def start
11
+ @bars = [].freeze
12
+
13
+ Ncurses.initscr
14
+ Ncurses.curs_set(0)
15
+ Ncurses.start_color
16
+
17
+ (0..7).each { |color_number| Ncurses.init_pair(color_number, color_number, Ncurses::COLOR_BLACK); }
18
+
19
+ @bars_window = Ncurses::WINDOW.new(1, 0, Ncurses.LINES-1, 0)
20
+ @log_window = Ncurses::WINDOW.new(Ncurses.LINES-1, 0, 0, 0)
21
+ @log_window.scrollok(true)
22
+
23
+ trap("SIGWINCH") do
24
+ Ncurses.endwin
25
+ Ncurses.refresh
26
+
27
+ refresh_window_positions
28
+
29
+ @bars.each do |bar|
30
+ bar.width = @bars_window.getmaxx
31
+ bar.show
32
+ end
33
+ end
34
+ end
35
+
36
+ # Restore the terminal to normal function. Always call this before exiting.
37
+ def end
38
+ # Give an extra line below the output for the shell to prompt on.
39
+ add_bar(nil)
40
+
41
+ Ncurses.endwin
42
+ end
43
+
44
+ # Write +text+ to the space above the progress bars.
45
+ def log(text)
46
+ text = text.to_s
47
+
48
+ # Parse ANSI escape codes.
49
+ text.scan(/([^\e]*)(?:\e\[(\d+.))?/) do |normal_text, code|
50
+ @log_window.addstr(normal_text)
51
+ case code
52
+ when /3(\d)m/
53
+ @log_window.attron(Ncurses.COLOR_PAIR($1.to_i))
54
+ when /0m/
55
+ @log_window.attron(Ncurses.COLOR_PAIR(7))
56
+ end
57
+ end
58
+ @log_window.addstr("\n")
59
+ @log_window.refresh
60
+ end
61
+
62
+ def width #:nodoc:
63
+ @bars_window.getmaxx
64
+ end
65
+
66
+ def refresh_window_positions
67
+ @bars_window.mvwin(Ncurses.LINES-bars.size, @bars_window.getbegx)
68
+ @bars_window.resize(bars.size, @bars_window.getmaxx)
69
+ @bars_window.refresh
70
+
71
+ @log_window.resize(Ncurses.LINES-bars.size, @log_window.getmaxx)
72
+ @log_window.refresh
73
+ end
74
+
75
+ def add_bar(bar) #:nodoc:
76
+ @bars += [bar]
77
+ refresh_window_positions
78
+ end
79
+
80
+ def update_bar(bar, rendered_bar) #:nodoc:
81
+ @bars_window.move(bars.index(bar), 0)
82
+ @bars_window.attron(Ncurses.COLOR_PAIR(bar.color)) if bar.color
83
+ @bars_window.addstr(rendered_bar)
84
+ @bars_window.attroff(Ncurses.COLOR_PAIR(bar.color)) if bar.color
85
+ @bars_window.refresh
86
+ end
87
+ end
88
+ end
89
+
90
+ require 'ruby-multi-progressbar/version'
91
+ require 'ruby-multi-progressbar/bar_renderer'
92
+ require 'ruby-multi-progressbar/bar'
93
+ require 'ruby-multi-progressbar/total_bar'
@@ -0,0 +1,69 @@
1
+ module MultiProgressBar
2
+ # Represents a progress bar at the bottom of the screen.
3
+ #
4
+ # +Bar+ exposes the same interface as +ProgressBar+ from the +progressbar+
5
+ # gem which backs its display.
6
+ #
7
+ # file_progress = MultiProgressBar::Bar.new("file_1", 2596)
8
+ # file_progress.inc # Increment value by 1.
9
+ # file_progress.inc(10) # Increment value by 10.
10
+ # file_progress.set(30) # Set value to 30.
11
+ #
12
+ # # Change bar format.
13
+ # file_progress.format = "%-14s (%s) %3d%% %s"
14
+ # file_progress.format_arguments = [:title, :stat, :percentage, :bar].
15
+ #
16
+ # See the +ruby-progressbar+ gem (http://0xcc.net/ruby-progressbar/index.html.en)
17
+ # for more details.
18
+ #
19
+ # MultiProgressBar::Bar makes two additional format arguments available: :current
20
+ # and :total. These display the current and total values respectively.
21
+ class Bar < DelegateClass(BarRenderer)
22
+ attr_reader :color
23
+
24
+ # Create a new Bar with a +title+ and a +total+ value.
25
+ def initialize(title, total)
26
+ MultiProgressBar.add_bar(self)
27
+
28
+ @observers = []
29
+
30
+ @renderer = BarRenderer.new(title, total, MultiProgressBar.width) do |rendered_bar|
31
+ MultiProgressBar.update_bar(self, rendered_bar)
32
+ end
33
+
34
+ super @renderer
35
+ end
36
+
37
+ # Increment the current value of the bar.
38
+ def inc(step = 1)
39
+ super
40
+ notify_observers
41
+ end
42
+
43
+ # Set the current value of the bar absolutely.
44
+ def set(count)
45
+ super
46
+ notify_observers
47
+ end
48
+
49
+ # Set the progress to 100% and display elapsed time instead of ETA.
50
+ def finish
51
+ super
52
+ notify_observers
53
+ end
54
+
55
+ def color=(color)
56
+ @color = color
57
+ show
58
+ end
59
+
60
+ def observe(&b) #:nodoc:
61
+ @observers << b
62
+ end
63
+
64
+ private
65
+ def notify_observers
66
+ @observers.each { |b| b.call(self) }
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,50 @@
1
+ module MultiProgressBar
2
+ class BarRenderer < ProgressBar #:nodoc:
3
+ attr_writer :total
4
+ attr_accessor :width
5
+
6
+ def initialize(title, total, width, &block)
7
+ @block = block
8
+ @buffer = StringIO.new
9
+ @width = width
10
+ super(title, total, @buffer)
11
+ end
12
+
13
+ def fmt_current
14
+ @current
15
+ end
16
+
17
+ def fmt_total
18
+ @total
19
+ end
20
+
21
+ def show
22
+ super
23
+ @block.call(@buffer.string)
24
+ @buffer.string = ""
25
+ end
26
+
27
+ def title=(new_title)
28
+ @title = new_title
29
+ show
30
+ end
31
+
32
+ def get_width
33
+ @width
34
+ end
35
+
36
+ def restart
37
+ set(0)
38
+ @start_time = Time.now
39
+ @previous_time = @start_time
40
+ end
41
+
42
+ def do_percentage
43
+ if @current.zero?
44
+ 0
45
+ else
46
+ super
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,36 @@
1
+ module MultiProgressBar
2
+ # Works just like +Bar+, but displays the total of other bars.
3
+ # TotalBar#inc and TotalBar#set don't work.
4
+ class TotalBar < Bar
5
+ # Create a new TotalBar. +bars+ is an array of Bar objects, and defaults to
6
+ # all existing bars.
7
+ def initialize(title, bars = MultiProgressBar.bars.dup)
8
+ @bars = bars
9
+
10
+ @bars.each do |bar|
11
+ bar.observe do
12
+ update_total
13
+ end
14
+ end
15
+
16
+ total_total = @bars.inject(0) { |sum, bar| sum + bar.total }
17
+ super title, total_total
18
+ end
19
+
20
+ [:inc, :set].each do |name|
21
+ define_method(name) do
22
+ raise NoMethodError, "NoMethodError: private method `#{name}' called for #{self}"
23
+ end
24
+ end
25
+
26
+ private
27
+ def update_total
28
+ total_current = @bars.inject(0) { |sum, bar| sum + bar.current }
29
+ total_total = @bars.inject(0) { |sum, bar| sum + bar.total }
30
+ finished = @bars.all? { |bar| bar.finished? }
31
+ @renderer.total = total_total
32
+ @renderer.set(total_current)
33
+ @renderer.finish if finished
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,3 @@
1
+ module MultiProgressBar
2
+ VERSION = '1.0.0'
3
+ end
metadata ADDED
@@ -0,0 +1,54 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby-multi-progressbar
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - shaharhd
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-04-11 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: |
14
+ Displays multiple progress bars using Ncurses and ruby-progressbar
15
+ Useful for displaying the status of multiple test runs or multipile threads.
16
+ email: shaharhd@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files:
20
+ - README.md
21
+ files:
22
+ - README.md
23
+ - lib/ruby-multi-progressbar.rb
24
+ - lib/ruby-multi-progressbar/bar.rb
25
+ - lib/ruby-multi-progressbar/bar_renderer.rb
26
+ - lib/ruby-multi-progressbar/total_bar.rb
27
+ - lib/ruby-multi-progressbar/version.rb
28
+ homepage: https://github.com/ShaharHD/ruby-multi-progressbar
29
+ licenses:
30
+ - LICENSE
31
+ metadata: {}
32
+ post_install_message:
33
+ rdoc_options:
34
+ - --charset
35
+ - UTF-8
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - '>='
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ requirements: []
49
+ rubyforge_project: ruby-multi-progressbar
50
+ rubygems_version: 2.2.2
51
+ signing_key:
52
+ specification_version: 4
53
+ summary: Displays multiple progress bars using Ncurses.
54
+ test_files: []