Attach and send keys
Needs libtmux for C++ 0.x Install
Get a session handle, send a command to a pane, and capture output. These examples use libtmux from your program; to attach your terminal interactively, see Attaching to tmux.
The excerpt assumes a connected libtmux::Server. Source and
verification identifies the source and its checks.
// No tmux failure is thrown. Every call answers with a value that is either// the result or the reason there isn't one.const auto sessions = server.sessions();if (!sessions.has_value()) { std::fprintf(stderr, "%s\n", sessions.error().diagnostic.c_str()); return 1;}
for (const libtmux::Session& session : *sessions) { std::printf("%s has %lld window(s)\n", std::string{session.name()}.c_str(), session.window_count());}
const libtmux::Session& session = sessions->at(0);
// Build an arrangement without composing a single tmux argument.const auto editor = session.new_window({.name = "editor"});if (!editor.has_value()) { std::fprintf(stderr, "%s\n", editor.error().diagnostic.c_str()); return 1;}
const auto logs = editor->split({.horizontal = true, .percentage = 30});if (!logs.has_value()) { std::fprintf(stderr, "%s\n", logs.error().diagnostic.c_str()); return 1;}
(void)logs->send_text("journalctl -f");(void)logs->send_key("Enter");Finding an existing session instead
For a script that runs repeatedly, look up a session before creating it. Attaching to tmux shows that pattern, and Filtering and querying, in practice covers absent and ambiguous matches.
Where this comes from
The excerpt adapts the connect and build regions of
examples/05-readme.cpp.
The port’s README example check compares the README’s quoted regions with the example file at the same source revision. CTest builds and runs the source program against a private tmux server.
Source inclusion
A file="..." fence reads the named source during the site build.