multi_progress_bar 0.1.0 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION.yml CHANGED
@@ -1,5 +1,5 @@
1
1
  ---
2
- :minor: 1
3
- :patch: 0
4
- :build:
2
+ :patch: 3
5
3
  :major: 0
4
+ :build:
5
+ :minor: 1
data/examples/example.rb CHANGED
@@ -10,19 +10,20 @@ begin
10
10
  MultiProgressBar.start
11
11
 
12
12
  # Demo.
13
- make_machine_bar = lambda { MultiProgressBar::Bar.new("(Waiting...)", 100) }
13
+ make_machine_bar = lambda { MultiProgressBar::Bar.new("(Waiting...)", 0) }
14
14
  machine_bars = [make_machine_bar[], make_machine_bar[]]
15
15
  total = MultiProgressBar::TotalBar.new("-Total-")
16
16
 
17
17
  machine_names = ["bleeker", "montrose"]
18
18
 
19
- until machine_bars.all? { |bar| bar.current == bar.total }
19
+ until machine_bars.all? { |bar| bar.title != "(Waiting...)" && bar.current == bar.total }
20
20
  sleep(0.1)
21
21
 
22
22
  # Simulate machines becoming available.
23
23
  machine_bars.each do |bar|
24
24
  if bar.title == "(Waiting...)"
25
25
  bar.title = machine_names.pop if rand(10) == 0
26
+ bar.total = 100
26
27
  else
27
28
  bar.inc(rand(10))
28
29
  end
@@ -12,6 +12,9 @@ module MultiProgressBar
12
12
 
13
13
  Ncurses.initscr
14
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); }
15
18
 
16
19
  @bars_window = Ncurses::WINDOW.new(1, 0, Ncurses.LINES-1, 0)
17
20
  @log_window = Ncurses::WINDOW.new(Ncurses.LINES-1, 0, 0, 0)
@@ -20,13 +23,27 @@ module MultiProgressBar
20
23
 
21
24
  # Restore the terminal to normal function. Always call this before exiting.
22
25
  def end
26
+ # Give an extra line below the output for the shell to prompt on.
27
+ add_bar(nil)
28
+
23
29
  Ncurses.endwin
24
- puts
25
30
  end
26
31
 
27
32
  # Write +text+ to the space above the progress bars.
28
33
  def log(text)
29
- @log_window.addstr("#{text}\n")
34
+ text = text.to_s
35
+
36
+ # Parse ANSI escape codes.
37
+ text.scan(/([^\e]*)(?:\e\[(\d+.))?/) do |normal_text, code|
38
+ @log_window.addstr(normal_text)
39
+ case code
40
+ when /3(\d)m/
41
+ @log_window.attron(Ncurses.COLOR_PAIR($1.to_i))
42
+ when /0m/
43
+ @log_window.attron(Ncurses.COLOR_PAIR(7))
44
+ end
45
+ end
46
+ @log_window.addstr("\n")
30
47
  @log_window.refresh
31
48
  end
32
49
 
@@ -47,7 +64,9 @@ module MultiProgressBar
47
64
 
48
65
  def update_bar(bar, rendered_bar) #:nodoc:
49
66
  @bars_window.move(bars.index(bar), 0)
67
+ @bars_window.attron(Ncurses.COLOR_PAIR(bar.color)) if bar.color
50
68
  @bars_window.addstr(rendered_bar)
69
+ @bars_window.attroff(Ncurses.COLOR_PAIR(bar.color)) if bar.color
51
70
  @bars_window.refresh
52
71
  end
53
72
  end
@@ -15,7 +15,12 @@ module MultiProgressBar
15
15
  #
16
16
  # See the +ruby-progressbar+ gem (http://0xcc.net/ruby-progressbar/index.html.en)
17
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.
18
21
  class Bar < DelegateClass(BarRenderer)
22
+ attr_reader :color
23
+
19
24
  # Create a new Bar with a +title+ and a +total+ value.
20
25
  def initialize(title, total)
21
26
  MultiProgressBar.add_bar(self)
@@ -41,6 +46,17 @@ module MultiProgressBar
41
46
  notify_observers
42
47
  end
43
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
+
44
60
  def observe(&b) #:nodoc:
45
61
  @observers << b
46
62
  end
@@ -1,6 +1,6 @@
1
1
  module MultiProgressBar
2
2
  class BarRenderer < ProgressBar #:nodoc:
3
- attr_writer :title
3
+ attr_writer :total
4
4
 
5
5
  def initialize(title, total, width, &block)
6
6
  @block = block
@@ -9,14 +9,41 @@ module MultiProgressBar
9
9
  super(title, total, @buffer)
10
10
  end
11
11
 
12
+ def fmt_current
13
+ @current
14
+ end
15
+
16
+ def fmt_total
17
+ @total
18
+ end
19
+
12
20
  def show
13
21
  super
14
22
  @block.call(@buffer.string)
15
23
  @buffer.string = ""
16
24
  end
17
25
 
26
+ def title=(new_title)
27
+ @title = new_title
28
+ show
29
+ end
30
+
18
31
  def get_width
19
32
  @width
20
33
  end
34
+
35
+ def restart
36
+ set(0)
37
+ @start_time = Time.now
38
+ @previous_time = @start_time
39
+ end
40
+
41
+ def do_percentage
42
+ if @current.zero?
43
+ 0
44
+ else
45
+ super
46
+ end
47
+ end
21
48
  end
22
49
  end
@@ -26,7 +26,11 @@ module MultiProgressBar
26
26
  private
27
27
  def update_total
28
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
29
32
  @renderer.set(total_current)
33
+ @renderer.finish if finished
30
34
  end
31
35
  end
32
36
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: multi_progress_bar
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter Jaros
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-11-16 00:00:00 -05:00
12
+ date: 2009-11-20 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency