Class: CommandLine::ProgressBar

Inherits:
Object
  • Object
show all
Defined in:
lib/cli/progressbar.rb

Constant Summary collapse

VERSION =
"0.9"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(title, total, out = STDERR) ⇒ ProgressBar

Returns a new instance of ProgressBar.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/cli/progressbar.rb', line 15

def initialize (title, total, out = STDERR)
  @title = title
  @total = total
  @out = out
  @terminal_width = 80
  @bar_mark = '='
  @current = 0
  @previous = 0
  @finished_p = false
  @start_time = Time.now
  @previous_time = @start_time
  @title_width = 24
  @format = "%-#{@title_width}s %3d%% %s %s"
  @format_arguments = [:title, :percentage, :bar, :stat]
  clear
  show
end

Instance Attribute Details

#currentObject (readonly)

Returns the value of attribute current.



33
34
35
# File 'lib/cli/progressbar.rb', line 33

def current
  @current
end

#start_timeObject

Returns the value of attribute start_time.



35
36
37
# File 'lib/cli/progressbar.rb', line 35

def start_time
  @start_time
end

#titleObject (readonly)

Returns the value of attribute title.



32
33
34
# File 'lib/cli/progressbar.rb', line 32

def title
  @title
end

#totalObject (readonly)

Returns the value of attribute total.



34
35
36
# File 'lib/cli/progressbar.rb', line 34

def total
  @total
end

Instance Method Details

#clearObject



160
161
162
163
164
# File 'lib/cli/progressbar.rb', line 160

def clear
  @out.print "\r"
  @out.print(" " * (terminal_width(80) - 1))
  @out.print "\r"
end

#file_transfer_modeObject



176
177
178
# File 'lib/cli/progressbar.rb', line 176

def file_transfer_mode
  @format_arguments = [:title, :percentage, :bar, :stat_for_file_transfer]
end

#finishObject



166
167
168
169
170
# File 'lib/cli/progressbar.rb', line 166

def finish
  @current = @total
  @finished_p = true
  show
end

#finished?Boolean

Returns:

  • (Boolean)


172
173
174
# File 'lib/cli/progressbar.rb', line 172

def finished?
  @finished_p
end

#format=(format) ⇒ Object



180
181
182
# File 'lib/cli/progressbar.rb', line 180

def format= (format)
  @format = format
end

#format_arguments=(arguments) ⇒ Object



184
185
186
# File 'lib/cli/progressbar.rb', line 184

def format_arguments= (arguments)
  @format_arguments = arguments
end

#haltObject



188
189
190
191
# File 'lib/cli/progressbar.rb', line 188

def halt
  @finished_p = true
  show
end

#inc(step = 1) ⇒ Object



193
194
195
196
197
198
# File 'lib/cli/progressbar.rb', line 193

def inc (step = 1)
  @current += step
  @current = @total if @current > @total
  show_if_needed
  @previous = @current
end

#inspectObject



209
210
211
# File 'lib/cli/progressbar.rb', line 209

def inspect
  "#<ProgressBar:#{@current}/#{@total}>"
end

#set(count) ⇒ Object



200
201
202
203
204
205
206
207
# File 'lib/cli/progressbar.rb', line 200

def set (count)
  count = 0 if count < 0
  count = @total if count > @total

  @current = count
  show_if_needed
  @previous = @current
end