Class: RuVim::FileWatcher::InotifyWatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/ruvim/file_watcher.rb

Constant Summary collapse

IN_MODIFY =
0x00000002
IN_DELETE_SELF =
0x00000400

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, &on_event) ⇒ InotifyWatcher

Returns a new instance of InotifyWatcher.

Raises:

  • (ArgumentError)


120
121
122
123
124
125
126
127
128
129
130
# File 'lib/ruvim/file_watcher.rb', line 120

def initialize(path, &on_event)
  raise ArgumentError, "File does not exist: #{path}" unless File.exist?(path)

  @path = path
  @on_event = on_event
  @offset = File.size(path)
  @thread = nil
  @stop = false
  @inotify_io = nil
  @watch_descriptor = nil
end

Class Method Details

.available?Boolean

Returns:

  • (Boolean)


95
96
97
98
99
100
101
102
103
104
105
# File 'lib/ruvim/file_watcher.rb', line 95

def self.available?
  return @available unless @available.nil?
  @available = begin
    gem "fiddle" if defined?(Gem)
    require "fiddle/import"
    _mod = inotify_module
    true
  rescue LoadError, StandardError
    false
  end
end

.inotify_moduleObject



107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/ruvim/file_watcher.rb', line 107

def self.inotify_module
  @inotify_module ||= begin
    mod = Module.new do
      extend Fiddle::Importer
      dlload "libc.so.6"
      extern "int inotify_init()"
      extern "int inotify_add_watch(int, const char*, unsigned int)"
      extern "int inotify_rm_watch(int, int)"
    end
    mod
  end
end

Instance Method Details

#alive?Boolean

Returns:

  • (Boolean)


145
146
147
# File 'lib/ruvim/file_watcher.rb', line 145

def alive?
  @thread&.alive? || false
end

#backendObject



93
# File 'lib/ruvim/file_watcher.rb', line 93

def backend = :inotify

#startObject



132
133
134
135
136
# File 'lib/ruvim/file_watcher.rb', line 132

def start
  @stop = false
  setup_inotify!
  @thread = Thread.new { watch_loop }
end

#stopObject



138
139
140
141
142
143
# File 'lib/ruvim/file_watcher.rb', line 138

def stop
  @stop = true
  cleanup_inotify!
  @thread&.join(0.5)
  @thread = nil
end