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

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

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.



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)



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

def interval
  @interval
end

Instance Method Details

#clear_screenvoid

This method returns an undefined value.

Clears the terminal screen using ANSI escape codes.



81
82
83
# File 'lib/pvectl/commands/get/watch_loop.rb', line 81

def clear_screen
  @output.print "\e[H\e[2J"
end

This method returns an undefined value.

Prints the watch mode header with timestamp.



88
89
90
91
92
# File 'lib/pvectl/commands/get/watch_loop.rb', line 88

def print_header
  timestamp = Time.now.strftime("%Y-%m-%d %H:%M:%S")
  @output.puts "Every #{@interval}s: #{timestamp}"
  @output.puts
end

#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

Yield Returns:

  • (void)


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.



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

def running?
  @running
end

#setup_signal_handlervoid

This method returns an undefined value.

Sets up SIGINT handler for graceful termination.



104
105
106
107
108
109
110
# File 'lib/pvectl/commands/get/watch_loop.rb', line 104

def setup_signal_handler
  @previous_handler = trap("INT") do
    stop
    # Don't print newline in non-TTY mode to avoid extra output
    @output.puts if tty?
  end
end

#sleep_interruptible(duration) ⇒ void

This method returns an undefined value.

Sleeps for the specified duration, allowing interruption.

Uses small sleep intervals to allow quick response to stop signal.



118
119
120
121
122
123
124
125
# File 'lib/pvectl/commands/get/watch_loop.rb', line 118

def sleep_interruptible(duration)
  remaining = duration
  while remaining > 0 && @running
    chunk = [remaining, 0.1].min
    sleep(chunk)
    remaining -= chunk
  end
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

#tty?Boolean

Checks if output is a TTY.



97
98
99
# File 'lib/pvectl/commands/get/watch_loop.rb', line 97

def tty?
  @output.tty?
end