Permalink
Browse files

accept multiple paths, but single callback

  • Loading branch information...
1 parent cf76526 commit b02063e3a4243c58000a269ede0a435fe1bd7cc9 @ttilley ttilley committed Feb 3, 2011
Showing with 21 additions and 17 deletions.
  1. +21 −17 lib/rb-fsevent/fsevent.rb
View
38 lib/rb-fsevent/fsevent.rb
@@ -1,34 +1,34 @@
class FSEvent
- attr_reader :path, :callback, :pipe
-
- def watch(path, &callback)
- @path = path
+ attr_reader :paths, :callback, :pipe
+
+ def watch(paths, &callback)
+ @paths = paths.kind_of?(Enumerable) ? paths : [paths]
@callback = callback
end
-
+
def run
launch_bin
listen
end
-
+
def stop
if pipe
Process.kill("KILL", pipe.pid)
pipe.close
end
rescue IOError
end
-
+
private
-
+
def bin_path
File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'bin'))
end
-
+
def launch_bin
- @pipe = IO.popen("#{bin_path}/fsevent_watch #{shellescape(path)}")
+ @pipe = IO.popen("#{bin_path}/fsevent_watch #{shellescaped_paths}")
end
-
+
def listen
while !pipe.eof?
if line = pipe.readline
@@ -39,23 +39,27 @@ def listen
rescue Interrupt, IOError
stop
end
-
+
+ def shellescaped_paths
+ @paths.map {|path| shellescape(path)}.join(' ')
+ end
+
# for Ruby 1.8.6 support
def shellescape(str)
# An empty argument will be skipped, so return empty quotes.
return "''" if str.empty?
-
+
str = str.dup
-
+
# Process as a single byte sequence because not all shell
# implementations are multibyte aware.
str.gsub!(/([^A-Za-z0-9_\-.,:\/@\n])/n, "\\\\\\1")
-
+
# A LF cannot be escaped with a backslash because a backslash + LF
# combo is regarded as line continuation and simply ignored.
str.gsub!(/\n/, "'\n'")
-
+
return str
end
-
+
end

0 comments on commit b02063e

Please sign in to comment.