Module: TimeTap::Watcher

Extended by:
Watcher
Included in:
Watcher
Defined in:
lib/time_tap/watcher.rb

Instance Method Summary collapse

Instance Method Details

#keep_watching(editor) ⇒ Object



5
6
7
8
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
# File 'lib/time_tap/watcher.rb', line 5

def keep_watching editor
  last = nil

  # TODO: pick which application the user wants...
  editor = editor.new

  File.open(File.join(TimeTap.config[:root], ".tap_history"), 'a') do |history|
    history.sync = true
    loop do
      exit if $stop
      begin
        if editor.is_running? && !(path = editor.current_path).blank?
          mtime = File.stat(path).mtime
          current = [path, mtime]
          
          unless current == last
            # The following equals to this shell code:
            #   `echo \`date +%s\`: \`pwd\``
            history << "#{mtime.to_i}: #{path}\n"
            last = [path, mtime]
          end
        end
      rescue Editors::EditorError
        # do nothing
      rescue
        puts Time.now.to_s
        puts $!.to_s
        puts $!.backtrace.join("\n")

        File.open(File.join(TimeTap.config[:root], ".tap_errors"), "w") do |file|
          file.puts Time.now.to_s
          file.puts $!.to_s
          file.puts $!.backtrace.join("\n")
        end

        raise if $!.kind_of?(SignalException)
      end
      sleep 30
    end
  end
end