Core Library0.xAPI Reference

Choose documentation 1

latest

Current version

latest
English

Early release libtmux is released, and its APIs may still change between releases. This site's structure and URLs may change too.

method libtmux-lua latest

Pane.wait_for_text

Source and package
Qualified name
libtmux.Pane:wait_for_text
Module
libtmux
Declared in
Pane
Package
libtmux
Source
lua/libtmux/_internal/entity.lua

Examples#

Wait for a command's output to appear on the visible screen. Use Lua 5.1 or newer with LuaRocks, tmux 3.3a or newer, and a C compiler and CMake for luv on Linux. In a new directory, install the library and its standalone runtime dependency from LuaRocks:

Terminal window
$ luarocks --tree ./rocks install luv &&
luarocks --tree ./rocks install libtmux &&
eval "$(luarocks --tree ./rocks path)"

Save this complete launcher as run.sh. It starts a private tmux daemon and cleans up after success or failure. A failed daemon shutdown reports and retains its socket directory. View source

#!/bin/sh
set -eu
program=${1:?Pass the complete Lua example filename}
binary=$(command -v "${TMUX_BIN:-tmux}")
case "$binary" in
/*) ;;
*) printf '%s\n' 'tmux must resolve to an absolute path' >&2; exit 1 ;;
esac
directory=$(mktemp -d "${TMPDIR:-/tmp}/libtmux-lua-api.XXXXXX")
socket="$directory/tmux.sock"
cleanup() {
status=$?
trap - 0 HUP INT TERM
if [ -S "$socket" ] && ! "$binary" -S "$socket" kill-server; then
printf 'Cannot stop tmux; kept %s\n' "$directory" >&2
exit 1
fi
rm -rf "$directory" || exit 1
exit "$status"
}
trap cleanup 0
trap 'exit 1' HUP INT TERM
unset TMUX TMUX_PANE
export TMUX_BIN="$binary" TMUX_SOCKET="$socket" ENV=/dev/null BASH_ENV=/dev/null
"$binary" -S "$socket" -f /dev/null new-session -d -s bootstrap -n bootstrap /bin/cat
"${LUA_BIN:-lua}" "$program"
"$binary" -S "$socket" has-session -t '=bootstrap'

Save the complete program as wait_for_text.lua. Closing the library connection leaves the daemon running; the launcher owns its lifetime. View source

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 = assert(os.getenv("TMUX_BIN"), "set TMUX_BIN to an absolute tmux executable")
local socket = assert(os.getenv("TMUX_SOCKET"), "set TMUX_SOCKET to the private socket")
must(adapter.run(function(runtime)
local server = must(runtime:connect({ binary = binary, socket_path = socket }):await())
local created = must(server:new_session({ name = "demo", argv = { "/bin/sh" } }):await())
local pane = created.pane
-- The shell echoes what is typed, so print the needle from two pieces.
must(pane:send_text("printf '%s%s\\n' 'lua wait ' 'ready'"):await())
must(pane:send_keys({ "Enter" }):await())
local found = must(pane:wait_for_text("lua wait ready", { timeout = 5000 }):await())
assert(found.text:find("lua wait ready", 1, true))
print("lua wait ready")
must(server:close():await())
return true
end))

Run the saved files. Assertions and operation errors produce a failing exit status:

Terminal window
$ eval "$(luarocks --tree ./rocks path)" &&
sh run.sh wait_for_text.lua

Guides#

Pages that discuss wait_for_text.

Compatibility targets, Getting started, Pane operations

Esc

Type to search.

Reference previewOpen reference