mirror of
https://github.com/donlucasx/xscapes.git
synced 2026-10-20 08:43:32 +02:00
xscapes was decided on 09-01 with the repo, and the rename stopped at the module path because going further meant touching live state: the env vars, ~/.config/asciiscapes/, and the marker on twelve hooks already installed in his settings.json. This finishes it. The marker was the whole risk. It is uninstall's only handle on its own work, so changing the constant alone would have left twelve hooks that nothing could see -- uninstall reporting zero, install adding a second copy beside each, and a person editing JSON by hand to remove a tool that said it had gone. install.go now WRITES `# xscapes:v1` and RECOGNISES `# asciiscapes:v1`, and the existing stale-path branch migrates them. The plan says "migrated from asciiscapes:v1" rather than "re-pointed", because the two are different amounts of risk and the reviewer should be told which. Proven by emptying legacyMarkers: the tests go red, and the failure shows the orphan exactly -- Stop ends up with two hooks, one of them invisible. Settings were renamed with a fallback rather than a hard cut, because a rename is the cheapest way there is to build a knob nothing reads: the value looks applied, the picture does not change, and the measurement is wrong with nothing saying so. These are variables typed by hand while judging a frame, so muscle memory is the expected input. internal/envx keeps ASCIISCAPES_* working and names it on stderr once, from the top of main -- the last moment certainly safe to write, since -live, claude and inside take the alternate screen shortly after. hook and statusline are exempt: one runs on every tool call into /dev/null, the other owns a line of the agent's chrome. Writing the test for the warning found it firing on a variable exported empty, which is inert, and a warning that cries wolf on a clean environment is one nobody reads. Left saying asciiscapes on purpose, and these are not leftovers: the legacyMarkers entry, envx's legacyPrefix, the verbatim quotes in _FEEDBACK.md, origin-chat.md's decision pass, and the superseded bullets in RESUME.md. Rewriting a record of what was true then would only make it lie. Live state moved with it: ~/.config/asciiscapes -> ~/.config/xscapes (53 spools, 17 backups), and the twelve hooks migrated with a diff of exactly 24 lines, all marker, nothing else touched. Two spools written into the old path by the still-old binary during the swap were merged back in timestamp order rather than appended. Also drops a duplicated `xscapes tune` line in the help, which predates this and was only visible once the name was read twice. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01R1u8vp8AgrTuaCCcLv6HVi
193 lines
6.5 KiB
Go
193 lines
6.5 KiB
Go
package main
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"os"
|
|
"os/exec"
|
|
"strings"
|
|
"syscall"
|
|
)
|
|
|
|
// The launcher. `xscapes claude` is the one command that has to work on a
|
|
// machine where nothing is set up, because it is the whole product from the
|
|
// user's side: agent on the left, scape on the right, one line to start.
|
|
//
|
|
// Lucas runs Terminal.app with no tmux session of his own, so the launcher
|
|
// cannot assume it is already inside one -- it bootstraps tmux when there is
|
|
// none, joins the current window when there is, and says something useful
|
|
// rather than failing when tmux is missing entirely.
|
|
//
|
|
// It never takes the agent's terminal. The agent gets the pane the user typed
|
|
// into and keeps stdin, stdout and its own TTY; the scape is what moves.
|
|
|
|
// scapeWidth is the scape's share of the window. The design target is 80
|
|
// columns and it degrades to 40, so on a typical 200-column terminal 38% is
|
|
// about 76 -- enough for the full composition while leaving the agent the
|
|
// wider half, which is the one being read.
|
|
const scapeWidth = "38%"
|
|
|
|
func runClaudeLauncher(args []string) {
|
|
fs := flag.NewFlagSet("claude", flag.ExitOnError)
|
|
print := fs.Bool("print", false, "print the commands and exit, changing nothing")
|
|
width := fs.String("width", scapeWidth, "the scape pane's share of the window")
|
|
agent := fs.String("agent", "claude", "the agent command to run beside the scape")
|
|
// Everything after -- belongs to the agent, not to us.
|
|
fs.Parse(args)
|
|
agentArgs := fs.Args()
|
|
|
|
self, err := os.Executable()
|
|
if err != nil {
|
|
fmt.Fprintln(os.Stderr, "xscapes: cannot find my own binary:", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
if _, err := exec.LookPath(*agent); err != nil {
|
|
fmt.Fprintf(os.Stderr, "xscapes: %s is not on your PATH\n", *agent)
|
|
os.Exit(1)
|
|
}
|
|
|
|
tmux, err := exec.LookPath("tmux")
|
|
if err != nil {
|
|
noTmux(self, *agent, agentArgs, *print)
|
|
return
|
|
}
|
|
|
|
// The scape waits for the session: both halves start at once and the
|
|
// agent cannot announce itself until it is up.
|
|
scape := []string{self, "-live", "-await"}
|
|
|
|
if os.Getenv("TMUX") != "" {
|
|
// Already inside tmux. Split this window, put the scape in the new
|
|
// pane, and hand this pane to the agent.
|
|
split := []string{tmux, "split-window", "-h", "-d", "-l", *width}
|
|
split = append(split, scape...)
|
|
run := append([]string{*agent}, agentArgs...)
|
|
if *print {
|
|
fmt.Println(shellJoin(split))
|
|
fmt.Println("exec " + shellJoin(run))
|
|
return
|
|
}
|
|
if out, err := exec.Command(split[0], split[1:]...).CombinedOutput(); err != nil {
|
|
fmt.Fprintf(os.Stderr, "xscapes: tmux split failed: %v\n%s", err, out)
|
|
os.Exit(1)
|
|
}
|
|
// Replace this process with the agent so it owns the pane outright --
|
|
// no wrapper sitting between the user's keystrokes and the agent.
|
|
execAgent(*agent, agentArgs)
|
|
return
|
|
}
|
|
|
|
// No tmux session yet: build one. The agent goes in the first pane
|
|
// because that is the pane the user will be typing into.
|
|
name := "xscapes"
|
|
newSess := []string{tmux, "new-session", "-d", "-s", name}
|
|
newSess = append(newSess, *agent)
|
|
newSess = append(newSess, agentArgs...)
|
|
|
|
split := []string{tmux, "split-window", "-h", "-d", "-l", *width, "-t", name + ":0"}
|
|
split = append(split, scape...)
|
|
|
|
focus := []string{tmux, "select-pane", "-t", name + ":0.0"}
|
|
attach := []string{tmux, "attach-session", "-t", name}
|
|
|
|
if *print {
|
|
for _, c := range [][]string{newSess, split, focus, attach} {
|
|
fmt.Println(shellJoin(c))
|
|
}
|
|
return
|
|
}
|
|
|
|
// A session of this name may be left over from a previous run. Joining it
|
|
// would drop the user into a dead agent, so take a fresh name instead of
|
|
// killing something that might still be alive.
|
|
if exec.Command(tmux, "has-session", "-t", name).Run() == nil {
|
|
for i := 2; ; i++ {
|
|
try := fmt.Sprintf("%s-%d", name, i)
|
|
if exec.Command(tmux, "has-session", "-t", try).Run() != nil {
|
|
name = try
|
|
break
|
|
}
|
|
}
|
|
newSess[4] = name
|
|
split[len(split)-4] = name + ":0"
|
|
focus[3] = name + ":0.0"
|
|
attach[3] = name
|
|
}
|
|
|
|
for _, c := range [][]string{newSess, split, focus} {
|
|
if out, err := exec.Command(c[0], c[1:]...).CombinedOutput(); err != nil {
|
|
fmt.Fprintf(os.Stderr, "xscapes: %s failed: %v\n%s", c[1], err, out)
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
// attach must REPLACE this process: tmux wants the terminal, and running
|
|
// it as a child would leave two programs reading one keyboard.
|
|
execAgent(attach[0], attach[1:])
|
|
}
|
|
|
|
// noTmux is the fallback. The brief allows exactly one dependency, tmux via
|
|
// brew, so this does not try to reinvent a pane manager: on macOS it opens a
|
|
// second Terminal window for the scape and hands this one to the agent, and
|
|
// anywhere else it says what to install.
|
|
func noTmux(self, agent string, agentArgs []string, print bool) {
|
|
if _, err := exec.LookPath("osascript"); err != nil {
|
|
fmt.Fprintln(os.Stderr, `xscapes: tmux is not installed.
|
|
|
|
brew install tmux
|
|
|
|
Or run the two halves in two terminals yourself:
|
|
|
|
xscapes -live -await`)
|
|
os.Exit(1)
|
|
}
|
|
|
|
script := fmt.Sprintf(`tell application "Terminal"
|
|
do script "%s -live -await"
|
|
activate
|
|
end tell`, strings.ReplaceAll(self, `"`, `\"`))
|
|
|
|
if print {
|
|
fmt.Printf("osascript -e %q\n", script)
|
|
fmt.Println("exec " + shellJoin(append([]string{agent}, agentArgs...)))
|
|
return
|
|
}
|
|
if out, err := exec.Command("osascript", "-e", script).CombinedOutput(); err != nil {
|
|
fmt.Fprintf(os.Stderr, "xscapes: could not open a second Terminal window: %v\n%s", err, out)
|
|
fmt.Fprintln(os.Stderr, "\nInstall tmux for the side-by-side layout:\n\n brew install tmux")
|
|
os.Exit(1)
|
|
}
|
|
execAgent(agent, agentArgs)
|
|
}
|
|
|
|
// execAgent replaces this process with the agent. Exec rather than spawn: a
|
|
// wrapper process in the middle would have to shuttle signals, resizes and the
|
|
// TTY between the terminal and the agent, and the brief is explicit that the
|
|
// agent's terminal is never to be seized.
|
|
func execAgent(name string, args []string) {
|
|
path, err := exec.LookPath(name)
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "xscapes: %s is not on your PATH\n", name)
|
|
os.Exit(1)
|
|
}
|
|
argv := append([]string{name}, args...)
|
|
if err := syscall.Exec(path, argv, os.Environ()); err != nil {
|
|
fmt.Fprintf(os.Stderr, "xscapes: could not start %s: %v\n", name, err)
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
// shellJoin renders a command the way a person would have to type it, for
|
|
// -print. Quoting only what needs it keeps the output copy-pasteable.
|
|
func shellJoin(argv []string) string {
|
|
out := make([]string, len(argv))
|
|
for i, a := range argv {
|
|
if a == "" || strings.ContainsAny(a, " \t\"'$`\\*?[]{}()|&;<>#~") {
|
|
out[i] = "'" + strings.ReplaceAll(a, "'", `'\''`) + "'"
|
|
continue
|
|
}
|
|
out[i] = a
|
|
}
|
|
return strings.Join(out, " ")
|
|
}
|