Class: BatCave::Command::SmartEdit

Inherits:
Clamp::Command
  • Object
show all
Defined in:
lib/batcave/command/smart-edit.rb

Instance Method Summary collapse

Instance Method Details

#capture_tmuxObject



55
56
57
58
59
60
61
62
# File 'lib/batcave/command/smart-edit.rb', line 55

def capture_tmux
  Stud::Temporary.file do |fd|
    # Capture the current screen plus 50 lines of history
    system("tmux capture-pane -S -50")
    system("tmux save-buffer -b 0 #{fd.path}")
    return fd.read
  end
end

#executeObject



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/batcave/command/smart-edit.rb', line 6

def execute
  rspec_failure = /^ *Failure\/Error/
  rspec_stack_trace = /^ *# (?<path>[^:]+):(?<line>[0-9]+):/
  text = capture_tmux
  if text =~ rspec_failure
    lines = text.split("\n")
    # first failure/error
    index = lines.find_index { |l| l =~ rspec_failure }
    # then find all lines that look like stack traces.
    traces = []
    last_line = index
    lines[index..-1].each_with_index do |line, i| 
      m = rspec_stack_trace.match(line)
      if m
        traces << Hash[m.names.zip(m.captures)]
      elsif traces.count > 0 && line =~ /^ *$/
        # already found traces, and a blank line means the end of the trace.
        last_line = i
        break
      end
    end

    if traces.any?
      # Find the most-recently modified file.
      candidate = traces.sort_by { |t| File.stat(t["path"]).mtime }.reverse.first
      Stud::Temporary.file do |fd|
        lines[index .. last_line + index].each do |line|
          fd.puts(line)
        end
        fd.flush

        system("tmux split-window -l 7 'cat #{fd.path}; sleep 3000 ' \\; last-pane")
        system("#{ENV["EDITOR"]} #{candidate["path"]} +#{candidate["line"]}")
        system("tmux last-pane \\; kill-pane")
      end
    end
  end

  return 0
end

#screen?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/batcave/command/smart-edit.rb', line 51

def screen?
  return ENV["STY"]
end

#tmux?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/batcave/command/smart-edit.rb', line 47

def tmux?
  return ENV["TMUX"]
end