Class: ProgressBar

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

Overview

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

Defined Under Namespace

Classes: OverRangeError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, **opt) ⇒ ProgressBar

Returns a new instance of ProgressBar.



12
13
14
15
16
17
18
19
# File 'lib/progressbar.rb', line 12

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

Instance Attribute Details

#ioObject (readonly)

Returns the value of attribute io.



10
11
12
# File 'lib/progressbar.rb', line 10

def io
  @io
end

Class Method Details

.push_server=(server) ⇒ Object



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

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

Instance Method Details

#calc_ratio(num) ⇒ Object



39
40
41
# File 'lib/progressbar.rb', line 39

def calc_ratio(num)
  num / @max
end

#clearObject



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

def clear
  return if silent?
  io.stream.print "\e[2K\r" # 行削除して行頭へ移動
end

#original_initializeProgressBar

Returns a new instance of ProgressBar.

Returns:



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

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

#output(num) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/progressbar.rb', line 21

def output(num)
  return if silent?
  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
  io.stream.print format("[%s%s] %d%%\r", @char * now, " " * rest, (ratio * 100).round)
end

#silent?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/progressbar.rb', line 43

def silent?
  ENV["NAROU_ENV"] == "test" || !io.tty? || io.silent?
end