Class: FSEvents

Inherits:
Object
  • Object
show all
Defined in:
lib/captured/fs_events.rb

Instance Method Summary collapse

Constructor Details

#initialize(dir = "#{HOME}/Desktop") ⇒ FSEvents

Returns a new instance of FSEvents.



5
6
7
# File 'lib/captured/fs_events.rb', line 5

def initialize(dir = "#{HOME}/Desktop")
  @watch_dir = dir
end

Instance Method Details

#runObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/captured/fs_events.rb', line 9

def run
  callback = proc do |stream, ctx, numEvents, paths, marks, eventIDs|

    #p stream
    #p ctx
    #p numEvents
    #p paths.methods
    #p marks.methods
    #p eventIDs

    paths.regard_as('*')
    rpaths = []
    numEvents.times { |i| rpaths << paths[i] }

    yield(*rpaths)
  end

  allocator = OSX::KCFAllocatorDefault
  context   = nil
  path      = [@watch_dir] #[Dir.pwd]
  sinceWhen = OSX::KFSEventStreamEventIdSinceNow
  latency   = 1.0
  flags     = 0

  stream   = OSX::FSEventStreamCreate(allocator, callback, context, path, sinceWhen, latency, flags)
  unless stream
    puts "Failed to create stream"
    exit
  end

  OSX::FSEventStreamScheduleWithRunLoop(stream, OSX::CFRunLoopGetCurrent(), OSX::KCFRunLoopDefaultMode)
  unless OSX::FSEventStreamStart(stream)
    puts "Failed to start stream"
    exit
  end

  puts "Watching #{path}"

  OSX::CFRunLoopRun()
rescue Interrupt
  OSX::FSEventStreamStop(stream)
  OSX::FSEventStreamInvalidate(stream)
  OSX::FSEventStreamRelease(stream)
end