Class: ProgressBar

Inherits:
Object
  • Object
show all
Defined in:
lib/progressbar.rb,
lib/web/progressbar4web.rb

Overview

コンソール用のプログレスバーはWEB UIでは使えないため置き換える

Defined Under Namespace

Classes: OverRangeError

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ ProgressBar

Returns a new instance of ProgressBar.



9
10
11
12
13
14
15
# File 'lib/progressbar.rb', line 9

def initialize(max, interval = 1, width = 50, char = "*")
  @max = max == 0 ? 1.0 : max.to_f
  @interval = interval
  @width = width
  @char = char
  @counter = 0
end

Class Method Details

.push_server=(server) ⇒ Object



12
13
14
# File 'lib/web/progressbar4web.rb', line 12

def self.push_server=(server)
  @@push_server = server
end

Instance Method Details

#calc_ratio(num) ⇒ Object



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

def calc_ratio(num)
  num / @max
end

#clearObject



30
31
32
33
# File 'lib/progressbar.rb', line 30

def clear
  return if $debug
  STDOUT.print " " * 79 + "\r"
end

#original_initializeProgressBar

Returns a new instance of ProgressBar.

Returns:



16
17
18
19
20
21
22
# File 'lib/web/progressbar4web.rb', line 16

def initialize(max, interval = 1, width = 50, char = "*")
  @max = max == 0 ? 1.0 : max.to_f
  @interval = interval
  @width = width
  @char = char
  @counter = 0
end

#output(num) ⇒ Object



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

def output(num)
  return if $debug
  if num > @max
    raise OverRangeError, "`#{num}` over `#{@max}(max)`"
  end
  @counter += 1
  return unless @counter % @interval == 0
  ratio = calc_ratio(num)
  now = (@width * ratio).round
  rest = @width - now
  STDOUT.print "[" + @char * now + ' ' * rest + "] #{(ratio * 100).round}%\r"
end