Class: Progress

Inherits:
Object
  • Object
show all
Includes:
InstanceMethods, Singleton
Defined in:
lib/progress.rb,
lib/progress/with_progress.rb

Defined Under Namespace

Modules: InstanceMethods Classes: Enhancer, WithProgress

Class Attribute Summary collapse

Attributes included from InstanceMethods

#current, #current_step, #note, #title, #total

Class Method Summary collapse

Methods included from InstanceMethods

#initialize, #step, #step_if_blank, #to_f

Class Attribute Details

.highlight=(value) ⇒ Object (writeonly)

:nodoc:



131
132
133
# File 'lib/progress.rb', line 131

def highlight=(value)
  @highlight = value
end

.lines=(value) ⇒ Object (writeonly)

:nodoc:



131
132
133
# File 'lib/progress.rb', line 131

def lines=(value)
  @lines = value
end

Class Method Details

.note=(s) ⇒ Object



125
126
127
128
129
# File 'lib/progress.rb', line 125

def note=(s)
  if levels.last
    levels.last.note = s
  end
end

.set(value, &block) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/progress.rb', line 96

def set(value, &block)
  if levels.last
    ret = if block
      levels.last.step(value - levels.last.current, &block)
    end
    if levels.last
      levels.last.current = Float(value)
    end
    print_message
    self.note = nil
    ret
  elsif block
    block.call
  end
end

.start(title = nil, total = nil) ⇒ Object

start progress indication

Procedural example

Progress.start('Test', 1000)
1000.times{ Progress.step }
Progress.stop

Block example

Progress.start('Test', 1000) do
  1000.times{ Progress.step }
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

To output progress as lines (not trying to stay on line)

Progress.lines = true

To force highlight

Progress.highlight = true


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

def start(title = nil, total = nil)
  if levels.empty?
    @started_at = Time.now
    @eta = nil
  end
  levels << new(title, total)
  print_message(true)
  if block_given?
    begin
      yield
    ensure
      stop
    end
  end
end

.step(num = 1, den = 1, &block) ⇒ Object



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

def step(num = 1, den = 1, &block)
  if levels.last
    set(levels.last.current + Float(num) / den, &block)
  elsif block
    block.call
  end
end

.stopObject



112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/progress.rb', line 112

def stop
  if levels.last
    if levels.last.step_if_blank || levels.length == 1
      print_message(true)
      set_title(nil)
    end
    levels.pop
    if levels.empty?
      io.puts
    end
  end
end