Class: ProgressBar

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

Overview

Ruby/ProgressBar - a text progress bar library

Copyright © 2001 Satoru Takabayashi <[email protected]>

All rights reserved.
This is free software with ABSOLUTELY NO WARRANTY.

You can redistribute it and/or modify it under the terms of Ruby’s licence.

Constant Summary collapse

VERSION =
"0.3"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of ProgressBar.



18
19
20
21
22
23
24
25
26
27
# File 'lib/testjour/progressbar.rb', line 18

def initialize (title, total, out = STDERR)
  @title = title
  @total = total
  @out = out
  @current = 0
  @previous = 0
  @is_finished = false
  @start_time = Time.now
  show_progress
end

Instance Attribute Details

#colorerObject

Returns the value of attribute colorer.



15
16
17
# File 'lib/testjour/progressbar.rb', line 15

def colorer
  @colorer
end

#title=(value) ⇒ Object (writeonly)

Sets the attribute title

Parameters:

  • value

    the value to set the attribute title to.



16
17
18
# File 'lib/testjour/progressbar.rb', line 16

def title=(value)
  @title = value
end

Instance Method Details

#bar(percentage) ⇒ Object



65
66
67
68
69
# File 'lib/testjour/progressbar.rb', line 65

def bar(percentage)
  @bar = "=" * 41
  len = percentage * (@bar.length + 1) / 100
  sprintf("[%.*s%s%*s]", len, @bar, ">", [@bar.size - len, 0].max, "")
end

#elapsedObject



52
53
54
55
# File 'lib/testjour/progressbar.rb', line 52

def elapsed
  elapsed = Time.now - @start_time
  sprintf("Time: %s", format_time(elapsed))
end

#eolObject



61
62
63
# File 'lib/testjour/progressbar.rb', line 61

def eol
  if @is_finished then "\n" else "\r" end
end

#etaObject

ETA stands for Estimated Time of Arrival.



42
43
44
45
46
47
48
49
50
# File 'lib/testjour/progressbar.rb', line 42

def eta
  if @current == 0
    "ETA:  --:--:--"
  else
    elapsed = Time.now - @start_time
    eta = elapsed * @total / @current - elapsed;
    sprintf("ETA: %s", format_time(eta))
  end
end

#finishObject



102
103
104
105
106
# File 'lib/testjour/progressbar.rb', line 102

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

#format_time(t) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/testjour/progressbar.rb', line 33

def format_time (t)
  t = t.to_i
  sec = t % 60
  min  = (t / 60) % 60
  hour = t / 3600
  sprintf("%02d:%02d:%02d", hour, min, sec);
end

#inc(step = 1) ⇒ Object



117
118
119
120
121
122
# File 'lib/testjour/progressbar.rb', line 117

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

#inspectObject



29
30
31
# File 'lib/testjour/progressbar.rb', line 29

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

#set(count) ⇒ Object



108
109
110
111
112
113
114
115
# File 'lib/testjour/progressbar.rb', line 108

def set (count)
  if count < 0 || count > @total
    raise "invalid count: #{count} (total: #{total})"
  end
  @current = count
  show_progress
  @previous = @current
end

#show(percentage) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/testjour/progressbar.rb', line 71

def show (percentage)
  output = sprintf("%-25s %3d%% %s %s%s", 
  @title[0,25], 
  percentage, 
  bar(percentage),
  time,
  eol
  )
  
  unless @colorer.nil?
    output = colorer.call(output)
  end
    
  @out.print(output)
end

#show_progressObject



87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/testjour/progressbar.rb', line 87

def show_progress
  if @total.zero?
    cur_percentage = 100
    prev_percentage = 0
  else
    cur_percentage  = (@current  * 100 / @total).to_i
    prev_percentage = (@previous * 100 / @total).to_i
  end

  if cur_percentage > prev_percentage || @is_finished
    show(cur_percentage)
  end
end

#timeObject



57
58
59
# File 'lib/testjour/progressbar.rb', line 57

def time
  if @is_finished then elapsed else eta end
end