From 5e79e965fa8ea5b3a29f4629495e76b82ace76e7 Mon Sep 17 00:00:00 2001 From: Tony Narlock Date: Sat, 5 Dec 2026 11:59:49 -0600 Subject: [PATCH] Examples(feat): Navigate and arrange windows MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit why: Readers need a complete program they can run to see how a tmux session links to a window and its pane. what: - Follow the session’s window link in a snapshot. - Run move-window and select-layout in separate examples and check the expected output of all three programs. --- examples/api/README.md | 6 +++++ examples/api/manifest.json | 29 ++++++++++++++++++++++ examples/api/move_window.lua | 31 ++++++++++++++++++++++++ examples/api/navigate.lua | 31 ++++++++++++++++++++++++ examples/api/set_layout.lua | 26 ++++++++++++++++++++ scripts/docs/tests/test_api_examples.lua | 6 +++-- 6 files changed, 127 insertions(+), 2 deletions(-) create mode 100644 examples/api/move_window.lua create mode 100644 examples/api/navigate.lua create mode 100644 examples/api/set_layout.lua diff --git a/examples/api/README.md b/examples/api/README.md index eb0ecd3..bc3f908 100644 --- a/examples/api/README.md +++ b/examples/api/README.md @@ -20,6 +20,9 @@ Run one complete program: $ lua examples/api/connect.lua ``` +The window examples find tmux on `PATH`. Set `TMUX_BIN` to an absolute path +to select a different build. + | Program | Task | Expected output | | --- | --- | --- | | [start_server.lua](start_server.lua) | Start a private server, use it and stop it | `sessions: 0, then 1` | @@ -28,6 +31,9 @@ $ lua examples/api/connect.lua | [snapshot.lua](snapshot.lua) | List sessions, windows, and panes | Two of each | | [new_session.lua](new_session.lua) | Create a session | `session: demo` | | [new_window.lua](new_window.lua) | Create a window in that session | `window: logs` | +| [navigate.lua](navigate.lua) | Follow a session's window link to a pane | `work / editor / pane 0` | +| [move_window.lua](move_window.lua) | Move a window to another session | `logs moved to ops:5` | +| [set_layout.lua](set_layout.lua) | Stack two panes vertically | `panes stacked vertically` | | [resize.lua](resize.lua) | Resize a window and a pane | `window 100x30, pane 30 wide` | | [refresh.lua](refresh.lua) | Read a window again after renaming it | `editor build` | | [query.lua](query.lua) | Select sessions by name | `matched: demo` | diff --git a/examples/api/manifest.json b/examples/api/manifest.json index dd27940..f41bd9d 100644 --- a/examples/api/manifest.json +++ b/examples/api/manifest.json @@ -65,6 +65,35 @@ "description": "Create a named window in a particular session.", "stdout": "window: logs\n" }, + { + "id": "navigate", + "symbols": [ + "libtmux.SnapshotSession.window_links", + "libtmux.SnapshotWindowLink.window", + "libtmux.SnapshotWindow.panes" + ], + "file": "examples/api/navigate.lua", + "description": "Follow a session's window link to its window and pane in one snapshot.", + "stdout": "work / editor / pane 0\n" + }, + { + "id": "move_window", + "symbols": [ + "libtmux.WindowLink:move" + ], + "file": "examples/api/move_window.lua", + "description": "Move a window link to an index in another session and verify its placement.", + "stdout": "logs moved to ops:5\n" + }, + { + "id": "set_layout", + "symbols": [ + "libtmux.Window:layout" + ], + "file": "examples/api/set_layout.lua", + "description": "Apply an even vertical layout and verify the positions of both panes.", + "stdout": "panes stacked vertically\n" + }, { "id": "resize", "symbols": [ diff --git a/examples/api/move_window.lua b/examples/api/move_window.lua new file mode 100644 index 0000000..292e66a --- /dev/null +++ b/examples/api/move_window.lua @@ -0,0 +1,31 @@ +local adapter = require("libtmux.runtime.luv") + +local function must(value, err) + if err ~= nil then + error(tostring(err), 0) + end + return value +end + +local binary = os.getenv("TMUX_BIN") + +must(adapter.run(function(runtime) + local owned = must(runtime:start_server({ binary = binary }):await()) + local source = must(owned.server:new_session({ name = "work", argv = { "/bin/cat" } }):await()) + local target = must(owned.server:new_session({ name = "ops", argv = { "/bin/cat" } }):await()) + local logs = must(source.session:new_window({ name = "logs", argv = { "/bin/cat" } }):await()) + + must(logs.window_link:move({ session = target.session, index = 5 }):await()) + local state = must(owned.server:snapshot({ strict = true }):await()) + local link = must(state.window_links + :where({ + window_id = logs.window:reference().id, + }) + :one()) + assert(link.session == state.sessions:where({ name = "ops" }):one()) + assert(link.index == 5) + print("logs moved to ops:" .. link.index) + + must(owned:stop():await()) + return true +end)) diff --git a/examples/api/navigate.lua b/examples/api/navigate.lua new file mode 100644 index 0000000..1e24a51 --- /dev/null +++ b/examples/api/navigate.lua @@ -0,0 +1,31 @@ +local adapter = require("libtmux.runtime.luv") + +local function must(value, err) + if err ~= nil then + error(tostring(err), 0) + end + return value +end + +local binary = os.getenv("TMUX_BIN") + +must(adapter.run(function(runtime) + local owned = must(runtime:start_server({ binary = binary }):await()) + must(owned.server + :new_session({ + name = "work", + window_name = "editor", + argv = { "/bin/cat" }, + }) + :await()) + + local snapshot = must(owned.server:snapshot({ strict = true }):await()) + local session = must(snapshot.sessions:one()) + local window = assert(session.window_links[1].window) + local pane = assert(window.panes[1]) + assert(pane.window == window) + print(session.name .. " / " .. window.name .. " / pane " .. pane.index) + + must(owned:stop():await()) + return true +end)) diff --git a/examples/api/set_layout.lua b/examples/api/set_layout.lua new file mode 100644 index 0000000..934b12c --- /dev/null +++ b/examples/api/set_layout.lua @@ -0,0 +1,26 @@ +local adapter = require("libtmux.runtime.luv") + +local function must(value, err) + if err ~= nil then + error(tostring(err), 0) + end + return value +end + +local binary = os.getenv("TMUX_BIN") + +must(adapter.run(function(runtime) + local owned = must(runtime:start_server({ binary = binary }):await()) + local created = must(owned.server:new_session({ name = "work", argv = { "/bin/cat" } }):await()) + local second = must(created.pane:split({ direction = "right", argv = { "/bin/cat" } }):await()) + must(created.window:layout({ named = "even-vertical" }):await()) + + local first = must(created.pane:snapshot():await()) + local other = must(second.pane:snapshot():await()) + assert(first.left == other.left and first.width == other.width) + assert(first.top ~= other.top) + print("panes stacked vertically") + + 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 9fcdb42..fd4469c 100644 --- a/scripts/docs/tests/test_api_examples.lua +++ b/scripts/docs/tests/test_api_examples.lua @@ -17,7 +17,9 @@ for symbol in 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+") + libtmux.OwnedServer:stop libtmux.SnapshotSession.window_links + libtmux.SnapshotWindowLink.window libtmux.SnapshotWindow.panes + libtmux.WindowLink:move libtmux.Window:layout]]):gmatch("%S+") do TARGETS[symbol] = true end @@ -67,7 +69,7 @@ end function M.test_complete_programs_cover_their_existing_targets() local loaded = api.load_examples(repo.root, TARGETS) - test.assertEquals(#loaded.examples, 17) + test.assertEquals(#loaded.examples, 20) local seen = {} for _, example in ipairs(loaded.examples) do for _, symbol in ipairs(example.symbols) do