Class: SwarmCLI::V3::ActivityIndicator

Inherits:
Object
  • Object
show all
Defined in:
lib/swarm_cli/v3/activity_indicator.rb

Overview

Renders the activity indicator above the separator line.

Three states: :idle, :working, :done.

  • idle — nothing rendered, #footer_lines is 0.

  • working — breathing “Working…” animation, ticker thread running. Optionally shows a status suffix (e.g. “🧠 retrieval (133ms)”).

  • done — static “Done (Xs)” message, no ticker. Stays in the footer until the next #start call clears it.

The state machine:

idle  start  working  stop  done  start  working 

Examples:

indicator = ActivityIndicator.new(on_tick: -> { display.redraw })
indicator.start
indicator.render     # => "\n⏳ Working... (2s)\n"
indicator.status = "🧠 retrieval: 133ms"
indicator.render     # => "\n⏳ Working... (2s • 🧠 retrieval: 133ms)\n"
indicator.stop
indicator.render     # => "\n• Done (2s)\n"

Constant Summary collapse

TICK_INTERVAL =
0.15

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(on_tick: nil) ⇒ ActivityIndicator

Returns a new instance of ActivityIndicator.

Parameters:

  • on_tick (#call, nil) (defaults to: nil)

    callback invoked on every ticker tick



30
31
32
33
34
35
36
37
# File 'lib/swarm_cli/v3/activity_indicator.rb', line 30

def initialize(on_tick: nil)
  @on_tick = on_tick
  @state = :idle
  @work_start_time = nil
  @done_elapsed = nil
  @status = nil
  @ticker_thread = nil
end

Instance Attribute Details

#status=(value) ⇒ void (writeonly)

This method returns an undefined value.

Set a status suffix shown on the working line.

Parameters:

  • text (String, nil)

    status text (e.g. “🧠 retrieval (133ms)”)



72
73
74
# File 'lib/swarm_cli/v3/activity_indicator.rb', line 72

def status=(value)
  @status = value
end

Instance Method Details

Number of extra footer lines this indicator contributes.

Returns:

  • (Integer)

    0 when idle, 2 when working or done



99
100
101
# File 'lib/swarm_cli/v3/activity_indicator.rb', line 99

def footer_lines
  @state == :idle ? 0 : 2
end

#renderString?

Render the indicator line for the current state.

Returns:

  • (String, nil)

    rendered indicator, or nil when idle



87
88
89
90
91
92
93
94
# File 'lib/swarm_cli/v3/activity_indicator.rb', line 87

def render
  case @state
  when :working
    render_working
  when :done
    render_done
  end
end

#startvoid

This method returns an undefined value.

Begin the working state and start the ticker thread.

Transitions from any state (idle or done) to working.



44
45
46
47
48
49
50
# File 'lib/swarm_cli/v3/activity_indicator.rb', line 44

def start
  @done_elapsed = nil
  @status = nil
  @work_start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  @state = :working
  start_ticker
end

#stopvoid

This method returns an undefined value.

End the working state and transition to done.

The done state keeps the indicator visible in the footer with elapsed time until the next #start call.



58
59
60
61
62
63
64
65
66
# File 'lib/swarm_cli/v3/activity_indicator.rb', line 58

def stop
  return unless @state == :working

  stop_ticker
  @done_elapsed = elapsed_seconds
  @state = :done
  @status = nil
  @work_start_time = nil
end

#visible?Boolean

Returns true if indicator occupies footer space (working or done).

Returns:

  • (Boolean)

    true if indicator occupies footer space (working or done)



80
81
82
# File 'lib/swarm_cli/v3/activity_indicator.rb', line 80

def visible?
  @state != :idle
end

#working?Boolean

Returns true if currently in the working state.

Returns:

  • (Boolean)

    true if currently in the working state



75
76
77
# File 'lib/swarm_cli/v3/activity_indicator.rb', line 75

def working?
  @state == :working
end