Class: NattyUI::Progress

Inherits:
Temporary show all
Defined in:
lib/natty-ui/progress.rb

Overview

A Temporary element that displays an progress indicator.

Instances are created by Features#progress. When a max value is set the progress is displayed as a percentage bar that fills as #value increases. Without a max, an open-ended dot animation grows.

All Features methods are available on this element, making it possible to nest output or sub-tasks inside a progress block.

Examples:

Bounded progress (percentage bar)

ui.progress 'Loading files', max: files.size do |bar|
  files.each { load(it); bar.step }
end

Open-ended progress (dot animation)

ui.progress 'Connecting…' do
  loop { ui.step; break if connected? }
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#maxNumeric? (readonly)

Maximum value set at creation time.

Returns:

  • (Numeric)

    the maximum value

  • (nil)

    when no maximum was set (open-ended mode)



29
30
31
# File 'lib/natty-ui/progress.rb', line 29

def max
  @max
end

#valueNumeric

Current progress value.

When #max is set the value is clamped to 0..max. When there is no maximum the value is clamped to a minimum of 0. Setting the same value twice is a no-op.

Examples:

bar.value += 42

Returns:

  • (Numeric)

    current value



41
42
43
# File 'lib/natty-ui/progress.rb', line 41

def value
  @value
end

Instance Method Details

#step(*title, count: 1, **popts) ⇒ Progress

Increments the progress value and optionally updates the title.

Examples:

Increment by 1

bar.step

Increment by a custom amount

bar.step count: 5

Increment and update the title

bar.step 'Processing item 3', count: 1.5

Parameters:

  • count (Numeric) (defaults to: 1)

    amount to add

  • title (#to_s, ...)

    new title text

  • popts (Hash)

    title print options, see Features#puts

Returns:



92
93
94
# File 'lib/natty-ui/progress.rb', line 92

def step(*title, count: 1, **popts)
  update(*title, value: @value + count, **popts)
end

#update(*title, value: nil, **popts) ⇒ Progress

Updates the progress title and/or value.

Any number of the positional title arguments and the value: keyword may be omitted independently.

Examples:

Update title only

bar.update 'Finalising…'

Update value only

bar.update value: 50

Update both

bar.update 'Step 2 of 3', value: 66

Parameters:

  • title (#to_s, ...)

    new title text

  • value (Numeric) (defaults to: nil)

    new progress value

  • popts (Hash)

    title print options, see Features#puts

Returns:



70
71
72
73
74
75
# File 'lib/natty-ui/progress.rb', line 70

def update(*title, value: nil, **popts)
  return if @done != 0
  draw_title(*title, **popts) unless title.empty?
  self.value = value if value
  self
end