Class: Factorix::Progress::Presenter

Inherits:
Object
  • Object
show all
Defined in:
lib/factorix/progress/presenter.rb

Overview

Progress presenter implementation

This class provides a simple progress presentation interface using tty-progressbar.

Instance Method Summary collapse

Constructor Details

#initialize(title: "Progress", output: $stderr) ⇒ Presenter

Create a new progress presenter

Parameters:

  • title (String) (defaults to: "Progress")

    title of the progress presenter

  • output (IO) (defaults to: $stderr)

    output stream for the progress presenter



15
16
17
18
19
# File 'lib/factorix/progress/presenter.rb', line 15

def initialize(title: "Progress", output: $stderr)
  @title = title
  @output = output
  @tty_bar = nil
end

Instance Method Details

#finishvoid

This method returns an undefined value.

Mark the progress presenter as finished



64
# File 'lib/factorix/progress/presenter.rb', line 64

def finish = @tty_bar&.finish

#increase_total(increment) ⇒ void

This method returns an undefined value.

Increase the total count dynamically

Parameters:

  • increment (Integer)

    amount to add to current total



54
55
56
57
58
59
# File 'lib/factorix/progress/presenter.rb', line 54

def increase_total(increment)
  return unless @tty_bar

  current_total = @tty_bar.total || 0
  @tty_bar.update(total: current_total + increment)
end

#start(total: nil, format: nil) ⇒ void

This method returns an undefined value.

Start the progress presentation with a specific total

Parameters:

  • total (Integer) (defaults to: nil)

    total size/count for progress tracking

  • format (String) (defaults to: nil)

    progress presenter format string (default: generic format)



26
27
28
29
30
31
32
33
34
35
# File 'lib/factorix/progress/presenter.rb', line 26

def start(total: nil, format: nil)
  format ||= total.nil? ? "#{@title} [:bar] :current" : "#{@title} [:bar] :percent :current/:total"
  @tty_bar = TTY::ProgressBar.new(
    format,
    total:,
    output: @output,
    frequency: 1, # Always update (important for testing with StringIO)
    force: true   # Force output even when not a TTY
  )
end

#update(current = nil) ⇒ void

This method returns an undefined value.

Update the progress presenter to a specific value

Parameters:

  • current (Integer) (defaults to: nil)

    current progress value



41
42
43
44
45
46
47
48
# File 'lib/factorix/progress/presenter.rb', line 41

def update(current=nil)
  if current
    @tty_bar&.current = current
  else
    # For indeterminate progress, just advance
    @tty_bar&.advance
  end
end