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_path) ⇒ Object



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

def call(rb_path)
  raise "File not found: #{rb_path}" unless File.exist?(rb_path)

  warn "Watching #{rb_path} (Ctrl+C to stop)..."
  last_mtime = File.mtime(rb_path)
  run(rb_path)

  loop do
    sleep POLL_INTERVAL
    mtime = File.mtime(rb_path)
    next if mtime == last_mtime

    last_mtime = mtime
    warn "\n--- #{rb_path} changed, re-running ---"
    run(rb_path)
  end
rescue Interrupt
  warn "\nStopped watching."
end