Core LibraryalphaAPI Reference

Choose documentation 3

v0.1.0.alpha.3

Current version

v0.1.0.alpha.3
English

Prerelease This site documents an alpha of libtmux. Its structure, URLs and APIs are subject to change.

method libtmux-ruby v0.1.0.alpha.3

ControlConnection.exchange

Behavior: Control connections.

Source and package
Qualified name
LibTmux::ControlConnection#exchange
Module
LibTmux
Declared in
ControlConnection
Package
libtmux
Source
gems/libtmux/lib/libtmux/control.rb
def exchange: ( ::String, ?timeout: ::Numeric, ?cancel: untyped ) -> ::LibTmux::GuardedReply

Description#

Behavior contract: Control connections.

Examples#

Open a control connection, run one command through it, and read the notification tmux sends when a window is renamed. Use Ruby 4.0.7, Bundler, Git and tmux 3.3a or newer on Unix, with /bin/cat and /bin/sh available. In an empty directory, fetch the documented library revision:

Terminal window
$ git clone https://github.com/libtmux/libtmux-ruby.git libtmux-source && \
git -C libtmux-source checkout d8ce4bbee0ca7748a81929f81f7d8d07f8c51267

Save this dependency file as Gemfile:

source "https://rubygems.org"
gem "libtmux", path: "libtmux-source/gems/libtmux"

Save this complete program as control.rb. Its Server.start block owns a private tmux server and stops it on exit, including after an error. View source

# frozen_string_literal: true
begin
# docs:begin main
require "libtmux"
LibTmux::Server.start do |server|
session = server.new_session(name: "work", command: ["/bin/cat"])
window = session.list_windows.fetch(0)
server.open_control(session: session.ref) do |control|
reply = control.exchange("display-message -p ready", timeout: 5)
puts "reply: #{reply.blocks.last.body.strip}"
events = control.subscribe(max_events: 64, max_bytes: 65_536)
window.rename("renamed")
event = events.next(timeout: 5) until event&.raw&.start_with?("%window-renamed")
puts "event: #{event.raw.strip}"
end
end
# docs:end main
rescue StandardError => error
warn "#{error.class}: #{error.message}"
exit 1
end

Install the runtime dependencies and run the saved program. An operation or cleanup error makes it exit unsuccessfully:

Terminal window
$ bundle config set --local path vendor/bundle && \
bundle install --jobs 2 && \
bundle exec ruby control.rb

Expected output:

reply: ready
event: %window-renamed @0 renamed
Parameters
Esc

Type to search.

Reference preview Open reference