Class: Pvectl::Commands::Get::WatchLoop

Inherits:
Object
  • Object
show all
Defined in:
lib/pvectl/commands/get/watch_loop.rb

Overview

Handles continuous watch mode for the get command.

Repeatedly executes a block at specified intervals, clearing the screen between iterations when running in a TTY. Handles SIGINT for graceful termination.

Examples:

Basic usage

loop = WatchLoop.new(interval: 5)
loop.run { puts Time.now }

With custom interval (clamped to minimum)

loop = WatchLoop.new(interval: 0.5)  # Will be clamped to 1 second

Constant Summary collapse

DEFAULT_INTERVAL =

Default refresh interval in seconds.

2
MIN_INTERVAL =

Minimum allowed refresh interval in seconds.

1

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(interval: DEFAULT_INTERVAL, output: $stdout) ⇒ WatchLoop

Creates a new WatchLoop.

Parameters:

  • interval (Integer, Float) (defaults to: DEFAULT_INTERVAL)

    refresh interval in seconds Values below MIN_INTERVAL are automatically clamped.

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

    output stream for TTY detection (default: $stdout)



34
35
36
37
38
# File 'lib/pvectl/commands/get/watch_loop.rb', line 34

def initialize(interval: DEFAULT_INTERVAL, output: $stdout)
  @interval = [interval.to_i, MIN_INTERVAL].max
  @output = output
  @running = false
end

Instance Attribute Details

#intervalInteger (readonly)

Returns the effective refresh interval.

Returns:

  • (Integer)

    the effective refresh interval



27
28
29
# File 'lib/pvectl/commands/get/watch_loop.rb', line 27

def interval
  @interval
end

Instance Method Details

#run { ... } ⇒ void

This method returns an undefined value.

Runs the watch loop, executing the block repeatedly.

Examples:

loop.run do
  models = handler.list
  puts format(models)
end

Yields:

  • Block to execute on each iteration



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/pvectl/commands/get/watch_loop.rb', line 50

def run
  @running = true
  setup_signal_handler

  while @running
    clear_screen if tty?
    print_header if tty?
    yield
    sleep_interruptible(@interval)
  end
end

#running?Boolean

Checks if the loop is currently running.

Returns:

  • (Boolean)

    true if running



72
73
74
# File 'lib/pvectl/commands/get/watch_loop.rb', line 72

def running?
  @running
end

#stopvoid

This method returns an undefined value.

Stops the watch loop.



65
66
67
# File 'lib/pvectl/commands/get/watch_loop.rb', line 65

def stop
  @running = false
end