summaryrefslogtreecommitdiff
path: root/internal/tmuxedit/pane.go
blob: 2713994e48cedba0900a1fd04522a88be41d946f (plain)
1
2
3
4
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package tmuxedit

import (
	"fmt"
	"os"
	"os/exec"
	"strings"
)

type tmuxEditDeps struct {
	runCommand       func(string, ...string) ([]byte, error)
	capturePane      func(string) (string, error)
	openEditorPopup  func(string, string, string) (string, error)
	sendKeys         func(string, ...string) error
	sendRepeatedKey  func(string, string, int) error
	sleepAfterClear  func()
	sleepAfterEscape func()
	launchPopup      func(string, string, string, string) error
}

func (d tmuxEditDeps) command(name string, args ...string) ([]byte, error) {
	if d.runCommand != nil {
		return d.runCommand(name, args...)
	}
	return exec.Command(name, args...).Output()
}

// resolveTargetPane determines which tmux pane to target using a fallback
// chain: explicit flag > HEXAI_TMUX_PANE env var > tmux query for active pane.
// Returns the pane ID (e.g. "%5") or an error.
func resolveTargetPane(flagPane string) (string, error) {
	return tmuxEditDeps{}.resolveTargetPane(flagPane)
}

func (d tmuxEditDeps) resolveTargetPane(flagPane string) (string, error) {
	// 1. Explicit --pane flag
	if p := strings.TrimSpace(flagPane); p != "" {
		return p, nil
	}
	// 2. Environment variable
	if p := strings.TrimSpace(os.Getenv("HEXAI_TMUX_PANE")); p != "" {
		return p, nil
	}
	// 3. Query tmux for the active pane in the current window
	return d.queryActivePane()
}

// queryActivePane asks tmux for the active pane ID using display-message.
func queryActivePane() (string, error) {
	return tmuxEditDeps{}.queryActivePane()
}

func (d tmuxEditDeps) queryActivePane() (string, error) {
	out, err := d.command("tmux", "display-message", "-p", "#{pane_id}")
	if err != nil {
		return "", fmt.Errorf("cannot determine tmux pane: %w", err)
	}
	pane := strings.TrimSpace(string(out))
	if pane == "" {
		return "", fmt.Errorf("tmux returned empty pane ID")
	}
	return pane, nil
}