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.



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

def current
  @current
end

#format=(value) ⇒ Object (writeonly)

Sets the attribute format

Parameters:

  • value

    the value to set the attribute format to.



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

def format=(value)
  @format = value
end

#format_arguments=(value) ⇒ Object (writeonly)

Sets the attribute format_arguments

Parameters:

  • value

    the value to set the attribute format_arguments to.



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

def format_arguments=(value)
  @format_arguments = value
end

#start_timeObject

Returns the value of attribute start_time.



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

def start_time
  @start_time
end

#titleObject (readonly)

Returns the value of attribute title.



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

def title
  @title
end

#totalObject (readonly)

Returns the value of attribute total.



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

def total
  @total
end

Instance Method Details

#clearObject



163
164
165
166
167
# File 'lib/cli/progressbar.rb', line 163

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

#file_transfer_modeObject



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

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

#finishObject



169
170
171
172
173
# File 'lib/cli/progressbar.rb', line 169

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

#finished?Boolean

Returns:

  • (Boolean)


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

def finished?
  @finished_p
end

#haltObject



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

def halt
  @finished_p = true
  show
end

#inc(step = 1) ⇒ Object



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

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

#inspectObject



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

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

#set(count) ⇒ Object



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

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

  @current = count
  show_if_needed
  @previous = @current
end