Class: Progress

Inherits:
Object
  • Object
show all
Extended by:
ClassMethods
Includes:
Singleton
Defined in:
lib/progress.rb,
lib/progress/eta.rb,
lib/progress/beeper.rb,
lib/progress/elapsed_time.rb,
lib/progress/class_methods.rb,
lib/progress/with_progress.rb

Overview

Procedural example

Progress.start('Test', 1000)
1000.times do
  Progress.step do
    # do something
  end
end
Progress.stop

Block example

Progress.start('Test', 1000) do
  1000.times do
    Progress.step do
      # do something
    end
  end
end

Step must not always be one

symbols = []
Progress.start('Input 100 symbols', 100) do
  while symbols.length < 100
    input = gets.scan(/\S/)
    symbols += input
    Progress.step input.length
  end
end

Enclosed block example

[1, 2, 3].each_with_progress('1 2 3') do |one_of_1_2_3|
  10.times_with_progress('10') do |one_of_10|
    sleep(0.001)
  end
end

Defined Under Namespace

Modules: ClassMethods, ElapsedTime Classes: Beeper, Eta, WithProgress

Instance Attribute Summary collapse

Attributes included from ClassMethods

#io

Instance Method Summary collapse

Methods included from ClassMethods

extended, highlight=, highlight?, io_tty?, running?, start, stay_on_line=, stay_on_line?, stop, terminal_title=, terminal_title?, without_beeper

Constructor Details

#initialize(total, title) ⇒ Progress

Returns a new instance of Progress.



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/progress.rb', line 56

def initialize(total, title)
  if !total.is_a?(Numeric) && (title.nil? || title.is_a?(Numeric))
    total, title = title, total
  end
  total = total && total != 0 ? Float(total) : 1.0

  @total = total
  @current = 0.0
  @title = title
  @mutex = Mutex.new
end

Instance Attribute Details

#currentObject (readonly)

Returns the value of attribute current.



52
53
54
# File 'lib/progress.rb', line 52

def current
  @current
end

#noteObject

Returns the value of attribute note.



54
55
56
# File 'lib/progress.rb', line 54

def note
  @note
end

#titleObject (readonly)

Returns the value of attribute title.



53
54
55
# File 'lib/progress.rb', line 53

def title
  @title
end

#totalObject (readonly)

Returns the value of attribute total.



51
52
53
# File 'lib/progress.rb', line 51

def total
  @total
end

Instance Method Details

#set(new_current, note) ⇒ Object



89
90
91
92
93
94
95
96
97
# File 'lib/progress.rb', line 89

def set(new_current, note)
  @step = new_current - @current
  @note = note
  ret = yield if block_given?
  @mutex.synchronize do
    @current = new_current
  end
  ret
end

#step(step, note) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/progress.rb', line 74

def step(step, note)
  unless step.is_a?(Numeric)
    step, note = nil, step
  end
  step = 1 if step.nil?

  @step = step
  @note = note
  ret = yield if block_given?
  @mutex.synchronize do
    @current += step
  end
  ret
end

#to_f(inner) ⇒ Object



68
69
70
71
72
# File 'lib/progress.rb', line 68

def to_f(inner)
  inner = 1.0 if inner > 1.0
  inner *= @step if @step
  (current + inner) / total
end