# xa.panes

Pane providers: where a session’s terminal lives (tmux on a server, iTerm on a Mac).

Everything xa does with a session’s terminal is four verbs: **create** a pane running a
command, **capture** its text, **address** it later, **tear it down**. A
[`PaneProvider`](#xa.panes.PaneProvider) is that, and a [`PaneAddress`](#xa.panes.PaneAddress) (`provider` + `ref`) is the
provider-neutral way to name a pane, so `Session` need not carry tmux-shaped fields.

- [`TmuxPanes`](#xa.panes.TmuxPanes): the default; a thin layer over [`xa.tmux`](xa.tmux.html.md#module-xa.tmux). Its `ref` is the
  tmux session name or a full pane ref (`session:@window.%pane`). Panes survive
  disconnection.
- [`ITermPanes`](#xa.panes.ITermPanes): an iTerm2 tab on a Mac, driven through an injected `osascript`
  runner. Its `ref` is the iTerm session id, valid while iTerm runs. Every value
  reaches AppleScript through `on run argv`, never spliced into the script text, so a
  directory or name containing a quote cannot break out.

```pycon
>>> TmuxPanes().address("work").ref, TmuxPanes().address("work").provider
('work', 'tmux')
>>> str(PaneAddress("iterm", "ABC-1"))
'iterm:ABC-1'
```

### Module Attributes

| [`Runner`](#xa.panes.Runner)         | runs AppleScript with `args` as `argv`.                                                                                                                                                                                               |
|-----------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [`NO_GUI_SESSION`](#xa.panes.NO_GUI_SESSION) | What `osascript` says when the caller is outside the GUI's launchd namespace (an SSH login): only then is the `launchctl asuser` retry made, so a failure that already did something (a tab opened, then an error) is never repeated. |
| [`PROVIDERS`](#xa.panes.PROVIDERS)      | Provider name -> factory (keyword arguments are the provider's own options).                                                                                                                                                          |

### Functions

| [`osascript_runner`](#xa.panes.osascript_runner)(\*[, run, uid, osascript, ...])   | A [`Runner`](#xa.panes.Runner) for `osascript`: directly first, and through `launchctl asuser <uid>` (which needs root) only when the direct call says the caller has no GUI session ([`NO_GUI_SESSION`](#xa.panes.NO_GUI_SESSION)).   |
|-----------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [`pane_provider`](#xa.panes.pane_provider)([name])                              | The provider called `name` (`tmux` or `iterm`), built with `options`.                                                                                                                                                                                                                |

### Classes

| [`ITermPanes`](#xa.panes.ITermPanes)(\*[, profile, run, known_profiles, ps])   | iTerm2 tabs on a Mac.                                                                                                          |
|-------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------|
| [`PaneAddress`](#xa.panes.PaneAddress)(provider, ref)                           | Provider-neutral name of one pane: which provider, and its own reference.                                                      |
| [`PaneProvider`](#xa.panes.PaneProvider)(\*args, \*\*kwargs)                     | Where panes live.                                                                                                              |
| [`TmuxPanes`](#xa.panes.TmuxPanes)(\*[, binary])                              | tmux panes, through [`xa.tmux`](xa.tmux.html.md#module-xa.tmux) (`binary` is the tmux executable). |

### Exceptions

| [`PaneUnsupported`](#xa.panes.PaneUnsupported)   | The provider cannot do this (e.g. stream a pane to a file).   |
|--------------------------------------------------------------------|---------------------------------------------------------------|

### *class* xa.panes.ITermPanes(, profile='Default', run=None, known_profiles=None, ps=None)

Bases: [`object`](https://docs.python.org/3/builtins/functions.html#object)

iTerm2 tabs on a Mac. `run` is the AppleScript runner (default:
[`osascript_runner()`](#xa.panes.osascript_runner)); `known_profiles` optionally lists the profile names
iTerm has, so a typo is an error before the GUI is touched (a typo otherwise opens
a Default tab and looks like it worked).

#### pane_pids(ref)

The pids on the tab’s tty (its login shell and what it runs), found through
iTerm’s `tty` and `ps -t`; `[]` when iTerm no longer has the tab.

* **Return type:**
  [`list`](https://docs.python.org/3/builtins/stdtypes.html#list)[[`int`](https://docs.python.org/3/builtins/functions.html#int)]

### xa.panes.NO_GUI_SESSION *= ('-609', '-600', 'Connection is invalid')*

What `osascript` says when the caller is outside the GUI’s launchd namespace
(an SSH login): only then is the `launchctl asuser` retry made, so a failure that
already did something (a tab opened, then an error) is never repeated.

### xa.panes.PROVIDERS *: [Mapping](https://docs.python.org/3/library/collections.abc.html#collections.abc.Mapping)[[str](https://docs.python.org/3/builtins/stdtypes.html#str), [Callable](https://docs.python.org/3/library/collections.abc.html#collections.abc.Callable)[..., [PaneProvider](#xa.panes.PaneProvider)]]* *= {'iterm': <class 'xa.panes.ITermPanes'>, 'tmux': <class 'xa.panes.TmuxPanes'>}*

Provider name -> factory (keyword arguments are the provider’s own options).

### *class* xa.panes.PaneAddress(provider, ref)

Bases: [`object`](https://docs.python.org/3/builtins/functions.html#object)

Provider-neutral name of one pane: which provider, and its own reference.

### *class* xa.panes.PaneProvider(\*args, \*\*kwargs)

Bases: [`Protocol`](https://docs.python.org/3/library/typing.html#typing.Protocol)

Where panes live. Implementations: [`TmuxPanes`](#xa.panes.TmuxPanes), [`ITermPanes`](#xa.panes.ITermPanes).

#### address(ref)

The [`PaneAddress`](#xa.panes.PaneAddress) of a pane of this provider.

* **Return type:**
  [`PaneAddress`](#xa.panes.PaneAddress)

#### capture(ref, , lines=200)

The last `lines` of the pane’s text, or “” when it cannot be read.

* **Return type:**
  [`str`](https://docs.python.org/3/builtins/stdtypes.html#str)

#### create(name, , command, cwd=None)

Start a pane named `name` running the shell `command`; return its address.

* **Return type:**
  [`PaneAddress`](#xa.panes.PaneAddress)

#### kill(ref)

Tear the pane down.

* **Return type:**
  [`None`](https://docs.python.org/3/builtins/constants.html#None)

#### pane_pids(ref)

The pids of the programs running in the pane (a root of each process tree; the
claude process is one of them or below one). `[]` when the pane is gone.

* **Return type:**
  [`list`](https://docs.python.org/3/builtins/stdtypes.html#list)[[`int`](https://docs.python.org/3/builtins/functions.html#int)]

#### stream_to_file(ref, path)

Append the pane’s output to `path` as it appears (may raise
[`PaneUnsupported`](#xa.panes.PaneUnsupported)).

* **Return type:**
  [`None`](https://docs.python.org/3/builtins/constants.html#None)

### *exception* xa.panes.PaneUnsupported

Bases: [`RuntimeError`](https://docs.python.org/3/builtins/exceptions.html#RuntimeError)

The provider cannot do this (e.g. stream a pane to a file).

### xa.panes.Runner

runs AppleScript with `args` as `argv`.

* **Type:**
  `Runner(script, args) -> stdout`

alias of [`Callable`](https://docs.python.org/3/library/collections.abc.html#collections.abc.Callable)[[[`str`](https://docs.python.org/3/builtins/stdtypes.html#str), [`Sequence`](https://docs.python.org/3/library/collections.abc.html#collections.abc.Sequence)[[`str`](https://docs.python.org/3/builtins/stdtypes.html#str)]], [`str`](https://docs.python.org/3/builtins/stdtypes.html#str)]

### *class* xa.panes.TmuxPanes(, binary='tmux')

Bases: [`object`](https://docs.python.org/3/builtins/functions.html#object)

tmux panes, through [`xa.tmux`](xa.tmux.html.md#module-xa.tmux) (`binary` is the tmux executable).

#### kill(ref)

A bare session name closes that session; a pane ref
(`session:@window.%pane`) closes only that pane, never the workspace.

* **Return type:**
  [`None`](https://docs.python.org/3/builtins/constants.html#None)

### xa.panes.osascript_runner(\*, run=<function run>, uid=None, osascript='osascript', timeout=30.0)

A [`Runner`](#xa.panes.Runner) for `osascript`: directly first, and through
`launchctl asuser <uid>` (which needs root) only when the direct call says the
caller has no GUI session ([`NO_GUI_SESSION`](#xa.panes.NO_GUI_SESSION)). `run` is the subprocess seam.

* **Return type:**
  [`Callable`](https://docs.python.org/3/library/collections.abc.html#collections.abc.Callable)[[[`str`](https://docs.python.org/3/builtins/stdtypes.html#str), [`Sequence`](https://docs.python.org/3/library/collections.abc.html#collections.abc.Sequence)[[`str`](https://docs.python.org/3/builtins/stdtypes.html#str)]], [`str`](https://docs.python.org/3/builtins/stdtypes.html#str)]

### xa.panes.pane_provider(name='tmux', \*\*options)

The provider called `name` (`tmux` or `iterm`), built with `options`.

```pycon
>>> pane_provider("tmux", binary="tmux").name
'tmux'
```

* **Return type:**
  [`PaneProvider`](#xa.panes.PaneProvider)
