Class: Terminal::ProgressBar

Inherits:
Object
  • Object
show all
Defined in:
lib/terminal/progressbar.rb,
lib/terminal/progressbar/version.rb,
lib/terminal/progressbar/singleton_class.rb

Defined Under Namespace

Classes: Error, InvalidPointingError

Constant Summary collapse

CR =
"\r"
EOL =
"\n"
STOP =
'|'
SPACE =
' '
DECORATION_LENGTH =
"100% #{STOP}#{STOP}".length
OptArg =

Returns:

  • (Class)
OptionalArgument.define {
  opt(:body_char, must: true,
                  condition: ->v { v.length == 1 },
                  adjuster: ->v { v.to_str.dup.freeze },
                  aliases: [:mark])
  opt(:max_count, default: 100,
                  condition: AND(Integer, ->v { v >= 1 }),
                  adjuster: ->v { v.to_int })
  opt(:max_width, default: $stderr.winsize.last,
                  condition: AND(Integer, ->v { v >= 1 }),
                  adjuster: ->v { v.to_int })
  opt(:output,    default: $stderr,
                  condition: AND(CAN(:print), CAN(:flush)))
}
VERSION =
'0.2.0'

Instance Attribute Summary collapse

Change Pointer collapse

Useful wrapper for constructors collapse

Instance Method Summary collapse

Constructor Details

#initialize(**options) ⇒ ProgressBar

Returns a new instance of ProgressBar.

Parameters:

  • options (Hash)

Options Hash (**options):

  • :body_char (String, #to_str) — default: also :mark
  • :max_count (Integer, #to_int)
  • :max_width (Integer, #to_int)
  • :output (IO, StringIO, #print, #flush)


46
47
48
49
50
51
52
53
54
# File 'lib/terminal/progressbar.rb', line 46

def initialize(**options)
  opts = OptArg.parse(options)

  @body_char = opts.body_char
  @max_count = opts.max_count
  @max_width = opts.max_width
  @output = opts.output
  @pointer = 0
end

Instance Attribute Details

#max_countObject (readonly)

Returns the value of attribute max_count.



22
23
24
# File 'lib/terminal/progressbar.rb', line 22

def max_count
  @max_count
end

#max_widthObject (readonly)

Returns the value of attribute max_width.



22
23
24
# File 'lib/terminal/progressbar.rb', line 22

def max_width
  @max_width
end

#outputObject (readonly)

Returns the value of attribute output.



22
23
24
# File 'lib/terminal/progressbar.rb', line 22

def output
  @output
end

#pointerObject Also known as: current_count

Returns the value of attribute pointer.



22
23
24
# File 'lib/terminal/progressbar.rb', line 22

def pointer
  @pointer
end

Class Method Details

.auto(interval_sec, **options) {|instance| ... } ⇒ void

This method returns an undefined value.

Parameters:

  • interval_sec (Float)
  • options (Hash)

Yields:

  • (instance)

Yield Parameters:

Yield Returns:

  • (void)


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/terminal/progressbar/singleton_class.rb', line 29

def auto(interval_sec, **options)
  interval_sec = Float(interval_sec)
  printing_thread = nil

  run(options) do |instance|
    printing_thread = Thread.new do
      loop do
        if instance.finished?
          break
        else
          instance.flush
          sleep(interval_sec)
        end
      end
    end

    yield instance
  end
ensure
  printing_thread.join if printing_thread
  nil
end

.run(**options) {|instance| ... } ⇒ void

This method returns an undefined value.

Parameters:

  • options (Hash)

Yields:

  • (instance)

Yield Parameters:

Yield Returns:

  • (void)


14
15
16
17
18
19
20
21
# File 'lib/terminal/progressbar/singleton_class.rb', line 14

def run(**options)
  instance = new(options)
  instance.flush
  yield instance
ensure
  instance.finish!
  nil
end

Instance Method Details

#barString

Returns:

  • (String)


77
78
79
# File 'lib/terminal/progressbar.rb', line 77

def bar
  "#{@body_char * current_bar_width}#{bar_padding}"
end

#bar_paddingString (private)

Returns:

  • (String)


159
160
161
# File 'lib/terminal/progressbar.rb', line 159

def bar_padding
  SPACE * (max_bar_width - current_bar_width)
end

#body_charString

Returns:

  • (String)


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

def body_char
  @body_char.dup
end

#current_bar_widthInteger

Returns:

  • (Integer)


67
68
69
# File 'lib/terminal/progressbar.rb', line 67

def current_bar_width
  (percentage == 0) ? 0 : (max_bar_width * rational).to_int
end

#decrement(step = 1) ⇒ step

Parameters:

  • step (Integer, #to_int) (defaults to: 1)

Returns:

  • (step)


128
129
130
131
# File 'lib/terminal/progressbar.rb', line 128

def decrement(step=1)
  increment(-step)
  step
end

#fast_forwardvoid Also known as: finish

This method returns an undefined value.



140
141
142
143
# File 'lib/terminal/progressbar.rb', line 140

def fast_forward
  @pointer = @max_count
  nil
end

#finished?Boolean Also known as: end?

Returns:

  • (Boolean)


99
100
101
# File 'lib/terminal/progressbar.rb', line 99

def finished?
  @pointer == @max_count
end

#flushvoid

This method returns an undefined value.



87
88
89
90
91
# File 'lib/terminal/progressbar.rb', line 87

def flush
  @output.print(line)
  @output.print(finished? ? EOL : CR)
  @output.flush
end

#increment(step = 1) ⇒ step

Parameters:

  • step (Integer, #to_int) (defaults to: 1)

Returns:

  • (step)

Raises:



118
119
120
121
122
123
124
# File 'lib/terminal/progressbar.rb', line 118

def increment(step=1)
  new_pointer = @pointer + step.to_int
  raise InvalidPointingError unless pointable?(new_pointer)

  @pointer = new_pointer
  step
end

#lineString

Returns:

  • (String)


82
83
84
# File 'lib/terminal/progressbar.rb', line 82

def line
  "#{percentage.to_s.rjust(3)}% #{STOP}#{bar}#{STOP}"
end

#max_bar_widthInteger

Returns:

  • (Integer)


62
63
64
# File 'lib/terminal/progressbar.rb', line 62

def max_bar_width
  max_width - DECORATION_LENGTH
end

#percentageFixnum

Returns 1..100.

Returns:

  • (Fixnum)

    1..100



72
73
74
# File 'lib/terminal/progressbar.rb', line 72

def percentage
  ((rational * (100 / @max_count)) * 100).to_int
end

#pointable?(point) ⇒ Boolean

Parameters:

  • point (Integer, #to_int)

Returns:

  • (Boolean)


94
95
96
97
# File 'lib/terminal/progressbar.rb', line 94

def pointable?(point)
  int = point.to_int
  (int >= 0) && (int <= @max_count)
end

#rationalRational (private)

Returns pointer / max_count.

Returns:

  • (Rational)

    pointer / max_count



164
165
166
# File 'lib/terminal/progressbar.rb', line 164

def rational
  Rational(@pointer, @max_count)
end

#rewindvoid

This method returns an undefined value.



134
135
136
137
# File 'lib/terminal/progressbar.rb', line 134

def rewind
  @pointer = 0
  nil
end