Class: ANSI::ProgressBar

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

Overview

Progressbar

Progressbar is a text-based progressbar library.

pbar = Progressbar.new( "Demo", 100 )
100.times { pbar.inc }
pbar.finish

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of ProgressBar.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/ansi/progressbar.rb', line 22

def initialize(title, total, out=STDERR)
  @title = title
  @total = total
  @out   = out

  @bar_length = 80
  @bar_mark = "o"
  @total_overflow = true
  @current = 0
  @previous = 0
  @is_finished = false
  @start_time = Time.now
  @format = "%-14s %3d%% %s %s"
  @format_arguments = [:title, :percentage, :bar, :stat]
  @styles = {}
  #
  yield self if block_given?
  #
  show_progress
end

Instance Attribute Details

#format(format, *arguments) ⇒ Object

Set format and format arguments.



63
64
65
# File 'lib/ansi/progressbar.rb', line 63

def format
  @format
end

#format_argumentsObject

Returns the value of attribute format_arguments.



46
47
48
# File 'lib/ansi/progressbar.rb', line 46

def format_arguments
  @format_arguments
end

#stylesObject

Returns the value of attribute styles.



47
48
49
# File 'lib/ansi/progressbar.rb', line 47

def styles
  @styles
end

Instance Method Details

#bar_mark=(mark) ⇒ Object



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

def bar_mark=(mark)
  @bar_mark = String(mark)[0..0]
end

#clearObject



133
134
135
# File 'lib/ansi/progressbar.rb', line 133

def clear
  @out.print(" " * get_width + eol)
end

#finishObject



88
89
90
91
92
# File 'lib/ansi/progressbar.rb', line 88

def finish
  @current = @total
  @is_finished = true
  show_progress
end

#flushObject



94
95
96
# File 'lib/ansi/progressbar.rb', line 94

def flush
  @out.flush
end

#haltObject



98
99
100
101
# File 'lib/ansi/progressbar.rb', line 98

def halt
  @is_finished = true
  show_progress
end

#inc(step = 1) ⇒ Object



125
126
127
128
129
130
# File 'lib/ansi/progressbar.rb', line 125

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

#inspectObject



137
138
139
# File 'lib/ansi/progressbar.rb', line 137

def inspect
  "(ProgressBar: #{@current}/#{@total})"
end

#resetObject



119
120
121
122
# File 'lib/ansi/progressbar.rb', line 119

def reset
  @current = 0
  @is_finished = false
end

#set(count) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/ansi/progressbar.rb', line 103

def set(count)
  if count < 0
    raise "invalid count less than zero: #{count}"
  elsif count > @total
    if @total_overflow
      @total = count + 1
    else
      raise "invalid count greater than total: #{count}"
    end
  end
  @current = count
  show_progress
  @previous = @current
end

#standard_modeObject



74
75
76
77
# File 'lib/ansi/progressbar.rb', line 74

def standard_mode
  @format = "%-14s %3d%% %s %s"
  @format_arguments = [:title, :percentage, :bar, :stat]
end

#style(options) ⇒ Object

Set ANSI styling options.



69
70
71
# File 'lib/ansi/progressbar.rb', line 69

def style(options)
  @styles = options
end

#title=(str) ⇒ Object



50
51
52
# File 'lib/ansi/progressbar.rb', line 50

def title=(str)
  @title = str
end

#total_overflow=(boolv) ⇒ Object



58
59
60
# File 'lib/ansi/progressbar.rb', line 58

def total_overflow=(boolv)
  @total_overflow = boolv ? true : false
end

#transfer_modeObject Also known as: file_transfer_mode



80
81
82
83
# File 'lib/ansi/progressbar.rb', line 80

def transfer_mode
  @format = "%-14s %3d%% %s %s"
  @format_arguments = [:title, :percentage, :bar, :stat_for_file_transfer]
end