Class: Pvectl::Commands::Get::WatchLoop
- Inherits:
-
Object
- Object
- Pvectl::Commands::Get::WatchLoop
- 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.
Constant Summary collapse
- DEFAULT_INTERVAL =
Default refresh interval in seconds.
2- MIN_INTERVAL =
Minimum allowed refresh interval in seconds.
1
Instance Attribute Summary collapse
-
#interval ⇒ Integer
readonly
The effective refresh interval.
Instance Method Summary collapse
-
#clear_screen ⇒ void
Clears the terminal screen using ANSI escape codes.
-
#initialize(interval: DEFAULT_INTERVAL, output: $stdout) ⇒ WatchLoop
constructor
Creates a new WatchLoop.
-
#print_header ⇒ void
Prints the watch mode header with timestamp.
-
#run { ... } ⇒ void
Runs the watch loop, executing the block repeatedly.
-
#running? ⇒ Boolean
Checks if the loop is currently running.
-
#setup_signal_handler ⇒ void
Sets up SIGINT handler for graceful termination.
-
#sleep_interruptible(duration) ⇒ void
Sleeps for the specified duration, allowing interruption.
-
#stop ⇒ void
Stops the watch loop.
-
#tty? ⇒ Boolean
Checks if output is a TTY.
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
#interval ⇒ Integer (readonly)
27 28 29 |
# File 'lib/pvectl/commands/get/watch_loop.rb', line 27 def interval @interval end |
Instance Method Details
#clear_screen ⇒ void
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 |
#print_header ⇒ void
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 = 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.
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_handler ⇒ void
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 |
#stop ⇒ void
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 |