Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ Notable changes to `libtmux`.
A named slot no longer blocks reads of numeric entries. (#139)
- Control: When a layout change closes a pane watch, the diagnostic tells
callers to open a fresh watch on the live observation. (#148)
- `Observation:watch_pane_lines` reads complete lines with an optional reply
prefix and a single deadline across intervening output.

## 0.1.1-1 (2026-11-01)

Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,10 @@ Each event carries the pane's bytes and a sequence number. A slow reader loses
events: the watch ends with `observation_gap` instead of dropping them
silently.

For a program that answers in lines, open `observation:watch_pane_lines(pane)`.
The [calculator example](docs/control.md#wait-for-a-result-line) waits five
seconds for a line starting with `result=`, even while the pane prints progress.

More: [observing a session](docs/control.md).

## Use it from Neovim
Expand Down
59 changes: 55 additions & 4 deletions docs/control.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ endpoint owns at most eight observation clients.
The observation offers:

- `watch_pane(pane, options)`: a ready pane-output watch.
- `watch_pane_lines(pane, options)`: a ready watch for complete lines from a
pane.
- `watch_notifications(options)`: a ready notification watch that keeps
unknown events and raw lines.
- `subscribe_format(pane, field_names, options)`: a typed format watch over
Expand Down Expand Up @@ -104,6 +106,55 @@ Event fields:
other field behavior show in the values. Closing a format watch unregisters
it.

## Wait for a result line

Open a line watch before sending the request. Give the read a literal reply
prefix and a timeout in milliseconds.

<!-- excerpt: examples/api/read_pane_line.lua -->

```lua
local lines = must(observation:watch_pane_lines(pane):await())
must(pane:send_text("sum 19 23"):await())
must(pane:send_keys({ "Enter" }):await())
local line, err = lines:next({ prefix = "result=", timeout = 5000 }):await()
```

The [example](../examples/api/read_pane_line.lua) runs a calculator in a
private server and prints `result=42`. The terminal may echo the request
and the app may print progress lines. Choose a prefix the app reserves for
replies.

```console
$ lua examples/api/read_pane_line.lua
```

- `lines:next()` returns a Request for the next line in order. It joins
output across events and removes the LF and one preceding CR, if present.
Empty lines return an empty string. Other bytes remain as received.
- The first line starts at the first byte received by the watch. If it
opens while a line is being printed, that line can start mid-record.
- With `prefix`, a read consumes earlier lines until it finds a match.
Chatter uses the same deadline. Use `timeout` from submission or an
absolute monotonic `deadline`. With neither option it waits until a
matching line or a closed watch.
- A read that times out returns `deadline_exceeded`. A timeout or
cancellation keeps the current partial line and any unread lines for
the next call. Lines already skipped by a prefix stay consumed.
- One read may be pending per line watch. A second returns
`concurrent_read`. A separate `watch_pane` on the same pane has its own
queue. Closing the line watch normally returns `nil, nil` to its reader.

Line watches allow 64 KiB before each LF, including a CR
(`max_line_bytes`). They queue at most 1,024 framed lines (`max_lines`)
and 1 MiB total (`max_bytes`), including input chunks held during framing,
partial lines and buffer overhead.

The runtime dispatches framing and prefix searches in slices. A long line
gets a fresh slice when its fragments must be joined into one Lua string.
Exceeding a watch's buffer limit or losing coverage closes it with
`observation_gap` and discards its buffered output.

## Bounds and ownership

Defaults:
Expand All @@ -118,10 +169,10 @@ Defaults:
- Each claim, shared attachment and public watch takes one runtime resource
slot. The runtime allows 128 in total. Runtime byte and logical-request
limits also apply, and watch bytes include event metadata.
- Overflow closes the affected watch with `observation_gap`, and the partial
result reports the discarded event count and decoded pane bytes. Other
watches and the shared reader continue. tmux's `%pause` also breaks pane
continuity.
- Overflow closes the affected watch with `observation_gap`. On an event
watch, the partial result reports the discarded event count and decoded
pane bytes. Other watches and the shared reader continue. tmux's `%pause`
also breaks pane continuity.
- There is no silent drop, no output coalescing, and no claim that tmux pause
is lossless backpressure.
- The reader parses bounded slices whether or not watches are consumed.
Expand Down
1 change: 1 addition & 0 deletions examples/api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ to select a different build.
| [query.lua](query.lua) | Select sessions by name | `matched: demo` |
| [explain.lua](explain.lua) | Show what a pane query runs in tmux and what stays local | Two plans, `never` and `auto` |
| [group.lua](group.lua) | Create a session and two windows in one tmux call | `windows: main, build, test` |
| [read_pane_line.lua](read_pane_line.lua) | Wait for a complete `result=` line (tmux 3.7 or newer) | `result=42` |
| [send_keys.lua](send_keys.lua) | Send text and press Enter | `lua input ready` |
| [capture.lua](capture.lua) | Read a completed command's output | `lua capture ready` |
| [wait_for_text.lua](wait_for_text.lua) | Wait for text on the visible screen | `lua wait ready` |
Expand Down
11 changes: 11 additions & 0 deletions examples/api/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,17 @@
"description": "Find sessions by name with a live query and inspect its captured rows.",
"stdout": "matched: demo\n"
},
{
"id": "read_pane_line",
"symbols": [
"libtmux.Observation:watch_pane_lines",
"libtmux.LineWatch:next"
],
"file": "examples/api/read_pane_line.lua",
"description": "Send a request to a pane and wait five seconds for a complete result line.",
"min_tmux": "3.7",
"stdout": "result=42\n"
},
{
"id": "send_keys",
"symbols": [
Expand Down
45 changes: 45 additions & 0 deletions examples/api/read_pane_line.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
local adapter = require("libtmux.runtime.luv")
---@diagnostic disable-next-line: different-requires
local uv = require("luv")

if arg[1] == "--worker" then
io.stdout:setvbuf("no")
local a, b = assert(io.read("*l")):match("^sum (%d+) (%d+)$")
assert(a and b, "expected sum and two integers")
local sum = assert(tonumber(a)) + assert(tonumber(b))
io.write("working\n")
io.write("result=", tostring(sum), "\n")
for _ in io.lines() do
end
return
end

local function must(value, err)
if err ~= nil then
error(tostring(err), 0)
end
return assert(value)
end

local binary = os.getenv("TMUX_BIN")
local script = assert(uv.fs_realpath(arg[0]))
local reply = must(adapter.run(function(runtime)
local owned = must(runtime:start_server({ binary = binary }):await())
local created = must(owned.server
:new_session({
name = "calc",
argv = { assert(uv.exepath()), script, "--worker" },
})
:await())
local pane = created.pane
local observation = must(owned.server:observe(created.session):await())
local lines = must(observation:watch_pane_lines(pane):await())
must(pane:send_text("sum 19 23"):await())
must(pane:send_keys({ "Enter" }):await())
local line, err = lines:next({ prefix = "result=", timeout = 5000 }):await()
must(observation:close():await())
must(owned:stop():await())
return line, err
end))
assert(reply == "result=42", reply)
print(reply)
Loading