A status bar for downloads, etc., using ASCII characters
Useful data members:
currval
most recent value that has been set
currfill
most recent number of "filled" bars
redraw
true if gauge needs to be redrawn after the last change in value
:currfill |
[R] |
|
:currval |
[R] |
|
:fill |
[W] |
|
:fillc |
[W] |
|
:headc |
[W] |
|
:hsize |
[R] |
|
:redraw |
[R] |
|
:restc |
[W] |
|
new(hsize, fill = nil, maxval = 100)
|
Basic setup.
- hsize
- # of chars wide, NOT counting containing brackets (2 chars).
- fill
- If not nil, string of length three containing, in order:
0. fill character for "done" part
1. arrowhead character
2. fill character for "remainging" part
If nil, the default value if "##.".
- maxval
- Update value for case when operation is done; defaults to to 100 for a
percentage guage. Nothing prevents setting the value higher than this, but
the results will not be pretty.
# File util/statusbar.rb, line 64
def initialize(hsize, fill = nil, maxval = 100)
@fill = fill || '##.'
@fillc = @fill[0,1]
@headc = @fill[1,1]
@restc = @fill[2,1]
@hsize = hsize
@currval = 0
@currfill = 0
@maxval = maxval
@redraw = true
@thebar = "[" + ("." * hsize) + "]"
end
Update bar with new progress value. The readable member attribute
"redraw" is set to true if the image of the bar changed.
# File util/statusbar.rb, line 78
def update(newval)
@redraw = false
if newval != @currval
@currval = newval
nfill = (@currval * @hsize) / @maxval
if nfill != @currfill
@redraw = true
@currfill = nfill
headc = @currfill < @hsize ? @headc : @fillc
if @currfill == 0
@thebar = "[" + @restc * @hsize + "]"
elsif @currfill == 1
@thebar = "[" + headc + @restc * (@hsize - @currfill) + "]"
else
@thebar = "[" + @fillc * (@currfill - 1) +
headc + @restc * (@hsize - @currfill) + "]"
end
end
end
end
# File util/statusbar.rb, line 98
def to_s()
@thebar
end