summaryrefslogtreecommitdiff
path: root/cmd/hexai-tmux-edit/main.go
blob: d61f68ae79a897d185788a5ebc48c1c9d9956d54 (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
// hexai-tmux-edit opens a tmux popup with $EDITOR for composing AI agent
// prompts. It captures existing prompt text from the target pane, pre-fills
// the editor, and sends the edited text back via tmux send-keys.
//
// Usage:
//
//	hexai-tmux-edit [--config <path>] [--agent <name>] [--pane <id>]
//
// Tmux keybinding (add to ~/.tmux.conf):
//
//	bind e run-shell -b "cd '#{pane_current_path}' && hexai-tmux-edit --pane '#{pane_id}'"
package main

import (
	"flag"
	"fmt"
	"io"
	"os"
	"strings"

	"codeberg.org/snonux/hexai/internal/appconfig"
	"codeberg.org/snonux/hexai/internal/tmuxedit"
)

type app struct {
	runTmuxEdit func(tmuxedit.Options) error
}

func newApp() *app { return &app{runTmuxEdit: tmuxedit.Run} }

func main() { os.Exit(newApp().runMain(os.Args[1:], os.Stderr)) }

// runMain parses flags from args and runs the tmux edit popup. It returns
// the process exit code; flag errors return 2 (matching stdlib convention),
// runtime failures return 1.
func (a *app) runMain(args []string, stderr io.Writer) int {
	defaultPath := appconfig.DefaultConfigPath()
	fs := flag.NewFlagSet("hexai-tmux-edit", flag.ContinueOnError)
	fs.SetOutput(stderr)
	configPath := fs.String("config", "", fmt.Sprintf("path to config file (default: %s)", defaultPath))
	agent := fs.String("agent", "", "AI agent name (auto-detected if omitted)")
	pane := fs.String("pane", "", "tmux target pane ID (e.g. %5)")
	if err := fs.Parse(args); err != nil {
		return 2
	}

	opts := buildOptions(*configPath, *agent, *pane)
	if err := a.runTmuxEdit(opts); err != nil {
		fmt.Fprintln(stderr, err)
		return 1
	}
	return 0
}

// buildOptions constructs tmuxedit.Options from the parsed flag values,
// trimming whitespace from each field.
func buildOptions(configPath, agent, pane string) tmuxedit.Options {
	return tmuxedit.Options{
		ConfigPath: strings.TrimSpace(configPath),
		Agent:      strings.TrimSpace(agent),
		Pane:       strings.TrimSpace(pane),
	}
}