Class: Prremote::Commands::Watch

Inherits:
Object
  • Object
show all
Defined in:
lib/prremote/commands/watch.rb

Constant Summary collapse

POLL_INTERVAL =
0.5

Instance Method Summary collapse

Constructor Details

#initialize(port:, baud:) ⇒ Watch

Returns a new instance of Watch.



8
9
10
11
# File 'lib/prremote/commands/watch.rb', line 8

def initialize(port:, baud:)
  @port = port
  @baud = baud
end

Instance Method Details

#call(*rb_paths) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/prremote/commands/watch.rb', line 13

def call(*rb_paths)
  rb_paths.each { |f| raise "File not found: #{f}" unless File.exist?(f) }

  label = rb_paths.map { |f| File.basename(f) }.join(', ')
  warn "Watching #{label} (Ctrl+C to stop)..."
  mtimes = rb_paths.map { |f| File.mtime(f) }
  run(*rb_paths)

  loop do
    sleep POLL_INTERVAL
    new_mtimes = rb_paths.map { |f| File.mtime(f) }
    next if new_mtimes == mtimes

    mtimes = new_mtimes
    warn "\n--- #{label} changed, re-running ---"
    run(*rb_paths)
  end
rescue Interrupt
  warn "\nStopped watching."
end