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
6 changes: 6 additions & 0 deletions examples/api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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` |
Expand All @@ -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` |
Expand Down
29 changes: 29 additions & 0 deletions examples/api/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down
31 changes: 31 additions & 0 deletions examples/api/move_window.lua
Original file line number Diff line number Diff line change
@@ -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))
31 changes: 31 additions & 0 deletions examples/api/navigate.lua
Original file line number Diff line number Diff line change
@@ -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))
26 changes: 26 additions & 0 deletions examples/api/set_layout.lua
Original file line number Diff line number Diff line change
@@ -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))
6 changes: 4 additions & 2 deletions scripts/docs/tests/test_api_examples.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down