Skip to content

About

OCaml tmux client library for Linux, with immutable snapshots, typed queries, blocking and Eio clients, and a workspace CLI.

Topics

Resources

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

178 Commits

Folders and files

Repository files navigation

libtmux for OCaml

libtmux for OCaml

OCaml libtmux.org license

Drive tmux from OCaml: create sessions, windows and panes, capture an immutable graph, query it locally, and observe output through an Eio or blocking client.

Guide · API · Examples · Workspace CLI · Compatibility · CI

Needs OCaml 5, Linux and tmux 3.3a or newer. The model and queries use the standard library alone.

Install from source

Not on opam yet. With opam and tmux installed, clone the repository:

$ git clone https://github.com/libtmux/libtmux-ocaml.git
$ cd libtmux-ocaml

Create a local compiler switch; the global one is untouched:

$ opam switch create \
    --deps-only \
    --no-switch \
    . ocaml-base-compiler.5.5.2

Install the libraries and the Eio entry point:

$ opam install . eio_main.1.6

Activate the switch in this shell:

$ eval "$(opam env --switch=. --set-switch)"

More: installation guide, for use from another Dune project.

Start and stop a private server

with_private_daemon: a tmux daemon the program owns

One call starts tmux on a fresh socket in its own directory, runs your function and then stops the daemon and removes the directory, also after an exception. Libtmux_eio.with_private_daemon does the same under Eio cancellation, and Client.kill_server stops a daemon early. The daemon starts with no session, so the bootstrap program creates the first one with Workspace.submit_bootstrap:

let snapshot =
  get
    (Libtmux_unix.with_private_daemon ~binary (fun endpoint ->
         let execution =
           bootstrap ~binary ~version:(local_version binary)
             ~directory:(Sys.getcwd ()) endpoint
         in
         let snapshot =
           protect
             ~finally:(fun () -> get (Workspace.close execution))
             (fun () ->
               let receipt = get (Workspace.await execution) in
               require_success receipt;
               let client = Option.get (Workspace.client execution) in
               get (Client.capture client))
         in
         (match Endpoint.kind endpoint with
         | Endpoint.Socket_path socket -> Printf.printf "socket=%s\n" socket
         | Endpoint.Named_socket _ -> ());
         snapshot))
$ opam exec -- dune exec examples/showcase_bootstrap.exe -- tmux
socket=/tmp/libtmux-ocaml-c75807/server
bootstrapped: 1 session, 1 window, 1 pane

Pass ~config_file to run the daemon with your own tmux configuration, or ~session to start it with one session. More: bootstrap guide.

Read a real tmux graph

Client.capture: sessions, window links and panes

One program starts a private server, builds two sessions that share a window, captures the graph and removes everything it made. Client.create_session and Client.link_window build the graph; Client.capture reads it:

open Libtmux
module Client = Libtmux_unix.Client

let get = function
  | Ok value -> value
  | Error error -> failwith (Format.asprintf "%a" Error.pp error)

let capture_graph client (initial : Created.t) spawn =
  let linked =
    get
      (Client.create_session client ~name:"linked" ~window_name:"linked-home"
         ~spawn ())
  in
  let pane =
    Snapshot.find_pane
      (get (Client.capture client))
      (Pane.Target.id linked.pane)
    |> Option.get
  in
  let text = "showcase ready" in
  ignore (get (Client.wait_for_text ~timeout:0.5 client pane ~text));
  get
    (Client.link_window client initial.window ~session:linked.session ~index:7);
  get (Client.capture client)
$ opam exec -- dune exec examples/showcase_graph.exe -- tmux
socket=/tmp/libtmux-ocaml-0cd3a1/server
linked/0 linked-home: 1 pane(s)
linked/7 editor: 1 pane(s)
showcase/0 editor: 1 pane(s)
graph: 2 sessions, 2 windows, 3 links, 2 panes

A window can belong to several sessions: Window_link.index is per session, Window.t is shared. The complete program also starts the server and removes its directory. More: graph guide.

Filter captured values

Snapshot: filter with native OCaml functions

Plain functions, no tmux I/O:

open Libtmux

let active_ids snapshot =
  Snapshot.panes snapshot |> Iarray.to_seq |> Seq.filter Pane.active
  |> Seq.map Pane.id |> List.of_seq

Query.matching: apply an inspectable expression

An Expr can be inspected, serialized and reused:

let roomy_terminals : Pane.t Expr.t =
  Expr.all
    [
      Expr.Bool.is_true Pane.Fields.active;
      Expr.Text.is_in Pane.Fields.current_command
        [ "cat"; "bash"; "zsh"; "nvim" ];
      Expr.Int.ge Pane.Fields.width 80;
    ]

let roomy_ids snapshot =
  Snapshot.panes snapshot |> Iarray.to_seq
  |> Query.matching roomy_terminals
  |> Seq.map Pane.id |> List.of_seq

A text operator on Pane.Fields.width fails to type-check. Matching keeps input order. The complete query program takes a tmux binary and a socket path. More: query guide.

Plan.panes: what tmux would filter and what stays local

Plan.panes splits a filter into a tmux source filter and a local remainder, without I/O. Plan.explanation, Plan.source_filter and Plan.residual show the split; Require_exact rejects a filter that cannot be lowered:

open Libtmux

let get = function
  | Ok value -> value
  | Error error -> failwith (Format.asprintf "%a" Error.pp error)

let show name expression =
  let plan = get (Plan.panes expression) in
  Printf.printf "%s: %s\n" name (Plan.explanation plan);
  Printf.printf "  source filter: %s\n"
    (Option.value (Plan.source_filter plan) ~default:"nothing");
  Printf.printf "  local filter: %s\n" (Expr.describe (Plan.residual plan))

let () =
  let active : Pane.t Expr.t = Expr.Bool.is_true Pane.Fields.active in
  let editors : Pane.t Expr.t =
    Expr.all
      [ active; Expr.Text.is_in Pane.Fields.current_command [ "nvim"; "vim" ] ]
  in
  show "active" active;
  show "editors" editors;
  match Plan.panes ~pushdown:Plan.Require_exact editors with
  | Error error -> Format.printf "rejected: %a@." Error.pp error
  | Ok _ -> failwith "an inexact plan was accepted"
$ opam exec -- dune exec examples/query_plan.exe
active: exact pane-flag source filter; retain local evaluation
  source filter: #{==:#{pane_active},1}
  local filter: pane_active = true
editors: capture complete observations and evaluate locally
  source filter: nothing
  local filter: all [pane_active = true; pane_current_command in ["nvim"; "vim"]]
rejected: unsupported: exact pushdown supports only pane flags and Boolean composition

Capture clients still read the whole graph and filter locally; they do not run plans yet. More: query guide.

Create a session and split its panes

Client.create_session: start a session and build its layout

Client.split_pane and Client.create_window add to it; Spawn.make sets the directory, command and environment. From the layout program:

open Libtmux
module Client = Libtmux_unix.Client

let get = function
  | Ok value -> value
  | Error error -> failwith (Format.asprintf "%a" Error.pp error)

let build client ~name ~directory =
  let spawn =
    Spawn.make ~cwd:directory
      ~environment:[ ("LIBTMUX_PROJECT", name) ]
      (Spawn.Exec { program = "cat"; args = [] })
  in
  let session =
    get (Client.create_session client ~name ~window_name:"editor" ~spawn ())
  in
  let right =
    get (Client.split_pane client session.pane ~direction:Geometry.Right ~spawn)
  in
  let logs =
    get (Client.create_window client session.session ~name:"logs" ~spawn)
  in
  get (Client.set_pane_title client right.pane ~title:"build output");
  get
    (Client.set_option client (Options.Session session.session) ~name:"@project"
       (Options.Text name));
  (session, right, logs)

An existing session name fails; nothing is replaced. More: operations and recovery.

showcase_layout.ml splits panes and applies a layout on a private server it starts and removes:

$ opam exec -- dune exec examples/showcase_layout.exe -- tmux
socket=/tmp/libtmux-ocaml-0cd3a1/server
layout: 3 horizontal panes

Send keys, read a pane and resize

Client.send_text: type into a pane, read it back and resize

Client.send_keys presses named keys, Client.capture_text reads rows, and Client.resize_pane and Client.resize_window set sizes in cells. The keys program splits a pane that prints showcase ready and uses all four:

open Libtmux
module Client = Libtmux_unix.Client

let get = function
  | Ok value -> value
  | Error error -> failwith (Format.asprintf "%a" Error.pp error)

let find client target =
  Snapshot.find_pane (get (Client.capture client)) (Pane.Target.id target)
  |> Option.get

let wait client pane text =
  ignore (get (Client.wait_for_text ~timeout:0.5 client pane ~text))

let drive client (initial : Created.t) spawn =
  let right =
    get (Client.split_pane client initial.pane ~direction:Geometry.Right ~spawn)
  in
  let pane = find client right.pane in
  wait client pane "showcase ready";
  let text = "literal: $HOME; #{pane_id}" in
  get (Client.send_text client right.pane ~text);
  get (Client.send_keys client right.pane [ Key.Enter ]);
  wait client pane text;
  let screen = get (Client.capture_text client pane ~start:0 ~end_:1) in
  Printf.printf "sent: %s\ncaptured: %S\n" text screen.bytes;
  get (Client.resize_window client initial.window ~width:100 ~height:30);
  get (Client.resize_pane client right.pane ~width:20);
  let resized = find client right.pane in
  Printf.printf "resized: %dx%d\n" (Pane.width resized) (Pane.height resized)
$ opam exec -- dune exec examples/showcase_send_keys.exe -- tmux
socket=/tmp/libtmux-ocaml-0cd3a1/server
sent: literal: $HOME; #{pane_id}
captured: "showcase ready\nliteral: $HOME; #{pane_id}\n"
resized: 20x30

send_text presses no Enter, and capture_text reads visible rows, not scrollback. More: operations, including kill_pane, kill_window, kill_session, read_option and set_hook.

Change settings, rearrange panes and steer clients

Client.set_option: options, environment and hooks

Client.set_environment and Client.set_hook take the same scopes. Each has a matching unset_*, read_* and capture_* call. The options program sets, reads and removes a server option, an environment variable and a hook:

let integer client scope name =
  match get (Client.read_option client scope name) with
  | Some (Options.Scalar (Options.Integer value)) -> Int64.to_int value
  | _ -> failwith (name ^ " is not an integer")

let tune_options client (initial : Created.t) =
  let server = Options.Server and session = Options.Session initial.session in
  get
    (Client.set_option client server ~name:"escape-time" (Options.Integer 13L));
  assert (integer client server "escape-time" = 13);
  (* Unsetting restores tmux's default, which differs between releases. *)
  get (Client.unset_option client server "escape-time");
  assert (integer client server "escape-time" <> 13);
  let name = "@project" in
  get (Client.set_option client session ~name (Options.Text "ocaml"));
  assert (
    Options.lookup (get (Client.capture_options client session)) name <> None);
  get (Client.clear_option_array client server "command-alias")

let tune_environment client =
  let global = Environment.Global and name = "LIBTMUX_DEMO" in
  let lookup () =
    Environment.lookup (get (Client.capture_environment client global)) name
  in
  get (Client.set_environment client global ~name "one");
  assert (lookup () = Some (Environment.Value "one"));
  (* A removal marker masks a global value; unsetting drops the entry. *)
  get (Client.remove_environment client global name);
  assert (lookup () = Some Environment.Removed);
  get (Client.unset_environment client global name);
  assert (lookup () = None)
$ opam exec -- dune exec examples/showcase_options.exe -- tmux
socket=/tmp/libtmux-ocaml-3f9a1c/server
configured: options, hooks and environment

Client.remove_environment stores a marker that masks a global value; unsetting drops the entry. More: options.

Client.swap_panes: swap, zoom, move and break panes

Client.rotate_panes, Client.zoom_pane, Client.move_pane and Client.break_pane change a window's arrangement; the panes program also respawns a pane and clears its history:

let find client target =
  Snapshot.find_pane (get (Client.capture client)) (Pane.Target.id target)
  |> Option.get

let panes_in client (window : Window.target) =
  Window.panes
    (Snapshot.find_window
       (get (Client.capture client))
       (Window.Target.id window)
    |> Option.get)

let arrange client (initial : Created.t) spawn =
  let right =
    get (Client.split_pane client initial.pane ~direction:Geometry.Right ~spawn)
  in
  let below =
    get (Client.split_pane client right.pane ~direction:Geometry.Below ~spawn)
  in
  assert (Iarray.length (panes_in client initial.window) = 3);
  (* Swap two panes, rotate all three and choose the active one. *)
  let left target = Pane.left (find client target) in
  let before = left initial.pane in
  get (Client.swap_panes client initial.pane right.pane);
  assert (left initial.pane <> before);
  get (Client.rotate_panes client initial.window `Forward);
  get (Client.select_pane client right.pane);
  assert (Pane.active (find client right.pane));
  (* Zoom one pane over the window, restore it and cycle the layout. *)
  get (Client.zoom_pane client right.pane);
  let zoomed = find client right.pane in
  assert (Pane.width zoomed = Window.width (Pane.window zoomed));
  get (Client.zoom_pane client right.pane);
  get (Client.cycle_layout client initial.window `Next);
  (* Move a pane beside another, then break it into a new window. *)
  get
    (Client.move_pane client below.pane ~beside:initial.pane
       ~direction:Geometry.Below);
  let broken =
    get
      (Client.break_pane client below.pane ~name:"broken"
         ~session:initial.session ~index:5)
  in
  assert (broken.window_index = 5);
  assert (Iarray.length (panes_in client initial.window) = 2)
$ opam exec -- dune exec examples/showcase_panes.exe -- tmux
socket=/tmp/libtmux-ocaml-8d2e47/server
arranged: swap, rotate, zoom, cycle, move, break, respawn

tmux can constrain geometry; capture again to read the result.

Client.switch_client_at: steer another client

A function ending in _at addresses the client bearing a name when the command runs. The clients program steers a second control client with Client.resize_control_client_at, Client.refresh_client_at and Client.request_detach_client_at:

let same a b = Client_name.to_string a = Client_name.to_string b

let names client =
  Snapshot.clients (get (Client.capture client))
  |> Iarray.to_list
  |> List.map Client_observation.current_name

let observed client name =
  Snapshot.clients (get (Client.capture client))
  |> Iarray.to_seq
  |> Seq.find (fun observation ->
      same (Client_observation.current_name observation) name)

(* A second control client is the one this program steers. *)
let connect_second ~binary client =
  let before = names client in
  let second =
    get (Client.connect ~binary (Source.endpoint (Client.source client)))
  in
  let name =
    List.find (fun name -> not (List.exists (same name) before)) (names client)
  in
  (second, name)

let steer client name (other : Created.t) =
  get (Client.resize_control_client_at client name ~width:100 ~height:30);
  get (Client.refresh_client_at client name);
  get (Client.switch_client_at client name ~session:other.session);
  let steered = Option.get (observed client name) in
  assert (Client_observation.width steered = 100);
  assert (
    Session.Id.equal
      (Session.id (Option.get (Client_observation.session steered)))
      (Session.Target.id other.session));
  get (Client.request_detach_client_at client name);
  assert (observed client name = None)
$ opam exec -- dune exec examples/showcase_clients.exe -- tmux
socket=/tmp/libtmux-ocaml-b61c05/server
steered: resized, refreshed, switched and detached

A request to detach confirms that tmux processed it, not that the peer exited.

Send literal input and observe the reply with Eio

Client.subscribe: subscribe before sending input

The clients drive tmux through persistent control-mode connections, and Client.subscribe streams a pane's output over them. Client.send_text types into the pane; Client.wait_for_text checks the screen. The Eio program starts its own server:

$ opam exec -- dune exec examples/showcase_watch.exe -- tmux
socket=/tmp/libtmux-ocaml-f76d47/server
observed: changed

send_text types literally and never interprets shell syntax. Your application owns Eio_main; the library starts no scheduler. A slow subscriber never blocks another. More: streaming and cancellation.

Packages and current limits

Package API or command guide Use Example
libtmux Libtmux Immutable graph and typed queries Describe a query
libtmux-unix Libtmux_unix Blocking Linux client and scoped cancellation Capture a graph
libtmux-eio Libtmux_eio Eio client, retained operations and subscriptions Send text and observe output
libtmux-json Libtmux_json Bounded query and workspace JSON decoding Round-trip a query
libtmux-workspace Libtmux_workspace Configuration, plans and execution journals Preview a native workspace
libtmux-workspace-cli Workspace commands Validate, preview and apply JSON workspaces Check a configuration

libtmux: the immutable graph and typed queries

Libtmux models captured sessions, window links and panes, and evaluates queries over them with the standard library alone.

API reference · Guides · README

libtmux-unix: the blocking Linux client

Libtmux_unix connects to a daemon, captures the graph and runs operations on the calling thread, with scoped cancellation.

API reference · README

libtmux-eio: the Eio client

Libtmux_eio adds retained operations and output subscriptions on Eio.

API reference · README

libtmux-json: bounded JSON documents

Libtmux_json decodes and encodes query and workspace documents within explicit bounds.

API reference · README

libtmux-workspace: configuration and plans

Libtmux_workspace validates a workspace, plans it against a captured graph and journals its execution.

API reference · Workspace docs · README

libtmux-workspace-cli: the tmux-workspace command

tmux-workspace validates, previews and applies JSON workspaces from the shell.

Workspace docs · Command guide · README

What it leaves out

  • Live objects. Session.t, Window.t and Pane.t are immutable values from one Snapshot.t. Capture again with Client.capture, or subscribe with Client.subscribe, to observe changes.
  • A terminal emulator. Client.capture_text reads visible rows rendered by tmux, and Client.subscribe streams pane output bytes; neither runs an in-process terminal or TUI.
  • Implicit reconnection or ~/.tmux.conf edits. Clients bind targets to one connection generation, never reconnect on your behalf, and never edit user configuration files.
  • macOS, Windows and remote daemons. The I/O packages require Linux pidfds, a shared PID namespace with the daemon, and local scratch storage.

Support and status

  • Not on opam yet. Install from source; remaining adoption gates are tracked in status and compatibility.
  • OCaml 5 and tmux 3.3a through 3.8 on Linux. CI runs on OCaml 5.5.2 against real tmux 3.3a and 3.8 servers without mocks.
  • Standard-library core. libtmux and libtmux-workspace have no third-party runtime dependencies; Yojson, Eio and Cmdliner stay in their optional packages (package boundaries).

Guides · API docs · Testing · Benchmarks · Contributing · Changelog · Logo assets · AI policy

Licensed under MIT. Part of the libtmux.org family alongside Python libtmux, sibling language ports, and tmux.

About

OCaml tmux client library for Linux, with immutable snapshots, typed queries, blocking and Eio clients, and a workspace CLI.

Topics

Resources

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages