nativeterm

package
v0.192.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 23, 2026 License: Apache-2.0 Imports: 16 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package nativeterm opens a command in the user's own terminal application — Ghostty, iTerm2, Terminal.app, Windows Terminal — rather than in the dashboard's web terminal.

The dashboard's xterm is a fine place to glance at a session and an awkward place to live in: its own key bindings, its own copy and paste, no tabs of the user's own (owner request, 2026-10-04). So a session can be carried on in the terminal the user already has, and Caprock goes on watching it the way it watches any session somebody started themselves (Phase 0): through the hooks and the transcript, not through a PTY it holds.

Every terminal is driven through its own documented interface, read from that terminal rather than guessed (see .ai/02-architecture.md § Native terminals for which ones were run on a real machine and which are written from their documentation):

  • Terminal.app opens a `.command` file in a new window, run by the user's login shell — no AppleScript, so no Automation prompt.
  • iTerm2 is told over AppleScript to open a window and type the command into the user's own shell there. It also opens `.command` files, but asks "OK to run …?" every time.
  • Ghostty, WezTerm and kitty take the command as arguments; on macOS through `open -na <app> --args`, which is how their docs start a window from a script.
  • Warp runs commands only from a launch configuration, so one is written and opened by its URI.
  • On Linux every terminal takes a command after a flag; on Windows, Windows Terminal takes `-d <dir>` and a command line, and cmd and PowerShell are started in a console of their own.

Nothing here decides whether a session may be opened; that is the caller's (internal/api), because it depends on who owns the process.

Index

Constants

This section is empty.

Variables

View Source
var ErrNoTerminal = errors.New("no terminal")

ErrNoTerminal is returned when no terminal Caprock can drive is installed, or the one asked for is not.

View Source
var ErrUnsafe = errors.New("unsafe")

ErrUnsafe is returned for a command word or directory that cannot be passed on safely.

Functions

func AppleScriptString

func AppleScriptString(s string) string

AppleScriptString quotes s as an AppleScript string literal.

func Display

func Display(goos, cwd string, argv []string) string

Display is the command as the user would type it, for the response and the button's tooltip.

func Env

func Env(base []string) []string

Env is the environment a terminal is started with: the user's login-shell one, without the variables that would make what runs inside it think it is nested in another Claude Code session or in Caprock's own PTY.

func IDs

func IDs(goos string) []string

IDs lists every terminal id this build knows on goos, installed or not — what the setting may be set to.

func Line

func Line(cwd string, argv []string) string

Line is the command as one POSIX shell line: `cd <dir> && <argv>`.

func Run

func Run(ctx context.Context, p Plan, env []string) error

Run carries a plan out: writes its file, starts its process with env, and for a plan that waits, reports the process's failure in its own words.

func Shell

func Shell(env []string) string

Shell is the user's login shell named by env, or "" when it names none.

func ShellQuote

func ShellQuote(s string) string

ShellQuote quotes s for a POSIX shell. Plain words are left bare so the command reads the way a person would type it; anything else is put in single quotes, inside which nothing is special but the quote itself.

func YAMLString

func YAMLString(s string) string

YAMLString quotes s as a YAML double-quoted scalar.

Types

type File

type File struct {
	Path    string
	Content string
}

File is written before the plan runs.

type Opener

type Opener struct {
	// DataDir holds the short-lived files some terminals are opened with.
	DataDir string
	// Env is the user's login-shell environment (internal/userenv).
	Env func() []string
	// Preferred is the terminal the user picked in settings, "" for none.
	Preferred func() string
}

Opener is what the daemon hands the API: it detects terminals against the user's environment and opens a command in one.

func (*Opener) List

func (o *Opener) List() ([]Terminal, string)

List is the installed terminals, most preferred first, and the id of the one used when none is named: the user's choice if it is installed, else the first found.

func (*Opener) Open

func (o *Opener) Open(ctx context.Context, id, cwd string, argv []string, before func() error) (Terminal, string, error)

Open runs argv in cwd in the terminal with this id, or the preferred one. before runs after the launch is planned and before it starts. The string returned is the command as the user would type it, also on failure, so the screen can offer it to copy.

type Plan

type Plan struct {
	Command string
	Args    []string
	// Dir is the working directory of Command itself (Windows consoles and
	// Linux terminals that have no directory flag of their own).
	Dir string
	// File is written, 0700, before Command runs.
	File *File
	// NewConsole starts Command in a console window of its own (Windows).
	NewConsole bool
	// Wait says Command returns once the window is open (open, osascript),
	// so its exit status is the answer. Otherwise Command is the terminal
	// itself and keeps running; it is started and let go.
	Wait bool
}

Plan is the process that opens the window, and the file it needs first.

func Build

func Build(goos string, t Terminal, req Request) (Plan, error)

Build turns a request into the process that opens it in t, on goos.

type Probe

type Probe struct {
	GOOS string
	// Env is the environment the terminal would be started with — the user's
	// login-shell one, so PATH is the PATH their own terminal has.
	Env []string
	// Home is the user's home directory, for ~/Applications.
	Home string
	// IsDir reports whether a path is a directory (an app bundle).
	IsDir func(string) bool
	// IsExec reports whether a path is an executable file.
	IsExec func(string) bool
}

Probe is how Detect looks at the machine; swapped by tests.

func DefaultProbe

func DefaultProbe(env []string) Probe

DefaultProbe looks at this machine.

type Request

type Request struct {
	// Cwd is the directory the command runs in. Empty means the home
	// directory, for a command that does not need one.
	Cwd string
	// Argv is the command, already split: ["claude", "--resume", "<id>"].
	// Every word must be plain (see safeWord): it is typed into shells and
	// command lines on three operating systems, and an id with a quote in it
	// is not an id.
	Argv []string
	// Shell is the user's login shell (POSIX), used where a terminal is given
	// a command rather than a shell to type it into.
	Shell string
	// Scratch is a private directory for the files some terminals are opened
	// with (Terminal.app's .command, Warp's launch configuration's twin).
	Scratch string
	// Name is a unique stem for those files.
	Name string
}

Request is what to open.

type Terminal

type Terminal struct {
	// ID is the stable name the API and the setting use ("ghostty").
	ID string `json:"id"`
	// Name is what the button says ("Ghostty").
	Name string `json:"name"`
	// contains filtered or unexported fields
}

Terminal is one terminal application found on this machine.

func Detect

func Detect(p Probe) []Terminal

Detect lists the terminals installed, most preferred first.

func Pick

func Pick(ts []Terminal, preferred string) (Terminal, bool)

Pick returns the preferred terminal if it is installed, else the first one found. ok is false when there is none.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL