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
14 changes: 14 additions & 0 deletions examples/api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,21 @@ $ lua examples/api/connect.lua
| [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` |
| [watch_pane.lua](watch_pane.lua) | Stream a pane's output over control mode (tmux 3.7 or newer) | `output: watched` |
| [vote_counter.lua](vote_counter.lua) | Send votes and read each tally over control mode (tmux 3.7 or newer) | Three tallies ending at `yes=2 no=1` |
| [unsupported_version.lua](unsupported_version.lua) | Handle a tmux below the minimum | `unsupported_version` and its message |

The [manifest](manifest.json) ties each program to the API symbols it shows
and its exact output.

Pass your own votes to the counter. An `awk` program in the pane keeps the tally.
Lua types each vote, presses Enter and prints the reply from the pane watch.

The counter needs tmux 3.7 or newer on `PATH`. To select a particular
build, set `TMUX_BIN` to its absolute path.

```console
$ lua examples/api/vote_counter.lua no yes no
no -> yes=0 no=1
yes -> yes=1 no=1
no -> yes=1 no=2
```
11 changes: 11 additions & 0 deletions examples/api/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,17 @@
"min_tmux": "3.7",
"stdout": "output: watched\n"
},
{
"id": "vote_counter",
"symbols": [
"libtmux.Watch:next",
"libtmux.Observation:close"
],
"file": "examples/api/vote_counter.lua",
"description": "Send votes to a counter in a pane and read each running tally over control mode.",
"min_tmux": "3.7",
"stdout": "yes -> yes=1 no=0\nno -> yes=1 no=1\nyes -> yes=2 no=1\n"
},
{
"id": "unsupported_version",
"symbols": [
Expand Down
60 changes: 60 additions & 0 deletions examples/api/vote_counter.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
local adapter = require("libtmux.runtime.luv")
---@diagnostic disable-next-line: different-requires
local uv = require("luv")

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

local votes = { ... }
if #votes == 0 then
votes = { "yes", "no", "yes" }
end
for _, vote in ipairs(votes) do
if vote ~= "yes" and vote ~= "no" then
io.stderr:write("usage: lua ", arg[0], " [yes|no ...]\n")
os.exit(2)
end
end

local counter = [[
$0 == "yes" { yes++ }
$0 == "no" { no++ }
{ printf "yes=%d no=%d\n", yes, no; fflush() }
]]

local binary = os.getenv("TMUX_BIN")
must(adapter.run(function(runtime)
local owned = must(runtime:start_server({ binary = binary }):await())
local session = { name = "votes", argv = { "awk", counter } }
local created = must(owned.server:new_session(session):await())
local pane = created.pane
local observation = must(owned.server:observe(created.session):await())
local watch = must(observation:watch_pane(pane):await())

for _, vote in ipairs(votes) do
local deadline = uv.hrtime() / 1e6 + 5000
local send = { process = { deadline = deadline } }
must(pane:send_text(vote, send):await())
must(pane:send_keys({ "Enter" }, send):await())

-- A tally can span events, and the terminal also echoes the vote.
local output = ""
local tally
repeat
local event = must(watch:next({ deadline = deadline }):await())
local bytes = assert(event.data)
assert(#output + #bytes <= 256, "tally exceeded 256 bytes")
output = output .. bytes
tally = ("\n" .. output):match("\n(yes=%d+ no=%d+)\r?\n")
until tally
print(vote .. " -> " .. tally)
end

must(observation:close():await())
must(owned:stop():await())
return true
end))
3 changes: 2 additions & 1 deletion scripts/docs/tests/test_api_examples.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ for symbol in
libtmux.Pane:capture libtmux.Pane:wait_for_text libtmux.Error.code libtmux.Error.message
libtmux.Entity:snapshot libtmux.Window:rename libtmux.Server:explain_panes
libtmux.Server:group libtmux.Server:observe libtmux.Observation:watch_pane
libtmux.Watch:next libtmux.Observation:close
libtmux.Pane:resize libtmux.Window:resize libtmux.Runtime:start_server
libtmux.Runtime:with_server libtmux.OwnedServer.server libtmux.OwnedServer.socket_path
libtmux.OwnedServer:stop]]):gmatch("%S+")
Expand Down Expand Up @@ -66,7 +67,7 @@ end

function M.test_complete_programs_cover_their_existing_targets()
local loaded = api.load_examples(repo.root, TARGETS)
test.assertEquals(#loaded.examples, 16)
test.assertEquals(#loaded.examples, 17)
local seen = {}
for _, example in ipairs(loaded.examples) do
for _, symbol in ipairs(example.symbols) do
Expand Down