Class: Parabot::TmuxManager

Inherits:
Object
  • Object
show all
Includes:
DryRunLogging
Defined in:
lib/parabot/tmux_manager.rb

Instance Method Summary collapse

Constructor Details

#initialize(dry_run: false) ⇒ TmuxManager

Returns a new instance of TmuxManager.



12
13
14
# File 'lib/parabot/tmux_manager.rb', line 12

def initialize(dry_run: false)
  @dry_run = dry_run
end

Instance Method Details

#find_claude_sessionObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/parabot/tmux_manager.rb', line 16

def find_claude_session
  # First try to find by name pattern
  sessions = list_sessions
  claude_session = sessions.find { |session| session.match?(/claude/i) }
  return claude_session if claude_session

  # Then check for Claude processes in any session
  sessions.each do |session|
    panes = list_panes(session)
    claude_pane = panes.find { |pane| pane[:command].match?(/claude/i) }
    return session if claude_pane
  end

  nil
rescue SystemCallError
  nil
end

#find_claude_target(session) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/parabot/tmux_manager.rb', line 34

def find_claude_target(session)
  # First check current window for Claude
  current_window = current_window_index(session)
  claude_pane = find_claude_pane_in_window(session, current_window)
  return "#{current_window}:#{claude_pane}" if claude_pane

  # Check for window named claude
  windows = list_windows(session)
  claude_window = windows.find { |w| w[:name].match?(/claude/i) }

  if claude_window
    claude_pane = find_claude_pane_in_window(session, claude_window[:index])
    return "#{claude_window[:index]}:#{claude_pane || 0}"
  end

  # Check all windows for Claude process
  windows.each do |window|
    next if window[:index] == current_window

    claude_pane = find_claude_pane_in_window(session, window[:index])
    return "#{window[:index]}:#{claude_pane}" if claude_pane
  end

  # Default to current window, first pane
  "#{current_window}:0"
rescue SystemCallError
  "0:0"
end

#list_sessionsObject



123
124
125
126
127
128
129
130
131
132
# File 'lib/parabot/tmux_manager.rb', line 123

def list_sessions
  result = run_command("tmux list-sessions")
  return [] unless result.success?

  result.out.lines.map do |line|
    line.split(":").first.strip
  end
rescue SystemCallError
  []
end

#send_to_claude(message) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/parabot/tmux_manager.rb', line 63

def send_to_claude(message)
  session = find_claude_session
  raise TmuxError, "No tmux session with Claude found" unless session

  target = find_claude_target(session)
  logger.info("Found Claude in session '#{session}', target '#{target}'")

  # Send the message
  logger.info("Sending message to Claude...")
  send_keys(session, target, "Escape")
  wait_for_claude(0.1)
  send_keys(session, target, message)
  wait_for_claude(0.2)
  send_keys(session, target, "Enter")
  wait_for_claude(0.1)
  send_keys(session, target, "Enter")

  logger.info("Message sent successfully")
  true
rescue SystemCallError => e
  raise TmuxError, "Failed to send message to Claude: #{e.message}"
end

#start_claude_session(system_prompt: nil) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/parabot/tmux_manager.rb', line 86

def start_claude_session(system_prompt: nil)
  current_session = current_session_name
  raise TmuxError, "Not in a tmux session. Please run this command from within tmux." unless current_session

  logger.info("Creating vertical pane (30% width)...")

  # Create vertical pane on the left (30% of screen)
  result = run_command("tmux split-window -h -l 30% -c '#{Dir.pwd}' -b")
  raise TmuxError, "Failed to create tmux pane" unless result.success?

  # Get the newly created pane
  new_pane = run_command('tmux display-message -p \'#{pane_index}\'').out.strip

  # Rename the pane for easier identification
  run_command("tmux select-pane -t #{new_pane} -T claude")

  # Start claude with system prompt if provided
  claude_command = if system_prompt
                     escaped_prompt = system_prompt.shellescape
                     "claude --system-prompt #{escaped_prompt}"
                   else
                     "claude"
                   end

  # Send keys to start claude (let tmux use current context like shell version)
  run_command("tmux send-keys -t #{new_pane} #{claude_command.shellescape} Enter")

  # Switch back to original pane
  run_command("tmux select-pane -R")

  logger.info("Claude session started in pane #{new_pane}")
  logger.info("Use 'Ctrl-B + arrow keys' to navigate between panes")
  true
rescue SystemCallError => e
  raise TmuxError, "Failed to start Claude session: #{e.message}"
end