Class: ANSI::ProgressBar
- Inherits:
-
Object
- Object
- ANSI::ProgressBar
- Defined in:
- lib/ansi/progressbar.rb
Overview
Progressbar
Progressbar is a text-based progressbar library.
= Progressbar.new( "Demo", 100 )
100.times { .inc }
.finish
Instance Attribute Summary collapse
-
#format(format, *arguments) ⇒ Object
Set format and format arguments.
-
#format_arguments ⇒ Object
Returns the value of attribute format_arguments.
-
#styles ⇒ Object
Returns the value of attribute styles.
Instance Method Summary collapse
- #bar_mark=(mark) ⇒ Object (also: #barmark=, #mark=)
- #clear ⇒ Object
- #finish ⇒ Object
- #flush ⇒ Object
- #halt ⇒ Object
- #inc(step = 1) ⇒ Object
-
#initialize(title, total, out = STDERR) ⇒ ProgressBar
constructor
A new instance of ProgressBar.
- #inspect ⇒ Object
- #reset ⇒ Object
- #set(count) ⇒ Object
- #standard_mode ⇒ Object
-
#style(options) ⇒ Object
Set ANSI styling options.
- #title=(str) ⇒ Object
- #total_overflow=(boolv) ⇒ Object
- #transfer_mode ⇒ Object (also: #file_transfer_mode)
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 = "|" @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.
68 69 70 |
# File 'lib/ansi/progressbar.rb', line 68 def format @format end |
#format_arguments ⇒ Object
Returns the value of attribute format_arguments.
46 47 48 |
# File 'lib/ansi/progressbar.rb', line 46 def format_arguments @format_arguments end |
#styles ⇒ Object
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 Also known as: barmark=, mark=
54 55 56 |
# File 'lib/ansi/progressbar.rb', line 54 def (mark) @bar_mark = String(mark)[0..0] end |
#clear ⇒ Object
138 139 140 |
# File 'lib/ansi/progressbar.rb', line 138 def clear @out.print(" " * get_width + eol) end |
#finish ⇒ Object
93 94 95 96 97 |
# File 'lib/ansi/progressbar.rb', line 93 def finish @current = @total @is_finished = true show_progress end |
#flush ⇒ Object
99 100 101 |
# File 'lib/ansi/progressbar.rb', line 99 def flush @out.flush end |
#halt ⇒ Object
103 104 105 106 |
# File 'lib/ansi/progressbar.rb', line 103 def halt @is_finished = true show_progress end |
#inc(step = 1) ⇒ Object
130 131 132 133 134 135 |
# File 'lib/ansi/progressbar.rb', line 130 def inc(step = 1) @current += step @current = @total if @current > @total show_progress @previous = @current end |
#inspect ⇒ Object
142 143 144 |
# File 'lib/ansi/progressbar.rb', line 142 def inspect "(ProgressBar: #{@current}/#{@total})" end |
#reset ⇒ Object
124 125 126 127 |
# File 'lib/ansi/progressbar.rb', line 124 def reset @current = 0 @is_finished = false end |
#set(count) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/ansi/progressbar.rb', line 108 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_mode ⇒ Object
79 80 81 82 |
# File 'lib/ansi/progressbar.rb', line 79 def standard_mode @format = "%-14s %3d%% %s %s" @format_arguments = [:title, :percentage, :bar, :stat] end |
#style(options) ⇒ Object
Set ANSI styling options.
74 75 76 |
# File 'lib/ansi/progressbar.rb', line 74 def style() @styles = end |
#title=(str) ⇒ Object
50 51 52 |
# File 'lib/ansi/progressbar.rb', line 50 def title=(str) @title = str end |
#total_overflow=(boolv) ⇒ Object
60 61 62 |
# File 'lib/ansi/progressbar.rb', line 60 def total_overflow=(boolv) @total_overflow = boolv ? true : false end |
#transfer_mode ⇒ Object Also known as: file_transfer_mode
85 86 87 88 |
# File 'lib/ansi/progressbar.rb', line 85 def transfer_mode @format = "%-14s %3d%% %s %s" @format_arguments = [:title, :percentage, :bar, :stat_for_file_transfer] end |