From d7f3165a3dd897f2135fbd51e4cfe633617427fc Mon Sep 17 00:00:00 2001 From: Tony Narlock Date: Sat, 5 Dec 2026 11:59:41 -0600 Subject: [PATCH] Examples(feat): Count votes in a tmux pane why: A Lua caller can send several votes and read the result through a live control-mode watch. what: - Add the runnable vote counter to the installed examples and check its final count. --- examples/api/README.md | 14 ++++++ examples/api/manifest.json | 11 +++++ examples/api/vote_counter.lua | 60 ++++++++++++++++++++++++ scripts/docs/tests/test_api_examples.lua | 3 +- 4 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 examples/api/vote_counter.lua diff --git a/examples/api/README.md b/examples/api/README.md index 799b62e1..eb0ecd37 100644 --- a/examples/api/README.md +++ b/examples/api/README.md @@ -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 +``` diff --git a/examples/api/manifest.json b/examples/api/manifest.json index 4b201f5e..dd279402 100644 --- a/examples/api/manifest.json +++ b/examples/api/manifest.json @@ -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": [ diff --git a/examples/api/vote_counter.lua b/examples/api/vote_counter.lua new file mode 100644 index 00000000..53389f64 --- /dev/null +++ b/examples/api/vote_counter.lua @@ -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)) diff --git a/scripts/docs/tests/test_api_examples.lua b/scripts/docs/tests/test_api_examples.lua index 1256f727..9fcdb425 100644 --- a/scripts/docs/tests/test_api_examples.lua +++ b/scripts/docs/tests/test_api_examples.lua @@ -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+") @@ -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