Core Library0.xAPI reference

Choose documentation 3

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.

MoreLess about this notice

method libtmux-dotnet

Server.CaptureSnapshotAsync

Reads the server and answers a handle carrying what it found.

Source and package
Qualified name
LibTmux.Server.CaptureSnapshotAsync
Namespace
LibTmux
Declared in
Server
Package
LibTmux
Source
src/LibTmux/Server.Snapshots.cs

Description

A handle that has not yet found a live server discovers one first, because a scope hands back the unmaterialized endpoint it started.

Every depth performs a fresh guarded read. Separate reads are not an atomic observation; detected topology contradictions fail the attempt without retrying or publishing partial relations.

Parameters
Returns

A handle whose relations are the ones this reading found.

Errors

Examples

Capture a new immutable hierarchy, check optional pane state, and distinguish an empty captured relation from one that has not been read. Use Linux or macOS with tmux 3.3a or newer, the .NET 10 SDK or newer, and the .NET 8 and 10 runtimes. In an empty directory, save this project as Example.csproj. It references the 0.1.1 release of LibTmux on NuGet: View source

Example.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>net8.0;net10.0</TargetFrameworks>
<LangVersion>12.0</LangVersion>
<Nullable>enable</Nullable>
<ImplicitUsings>disable</ImplicitUsings>
<EnableDefaultCompileItems>false</EnableDefaultCompileItems>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<AnalysisLevel>10-recommended</AnalysisLevel>
</PropertyGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<PackageReference Include="LibTmux" Version="[0.1.1]" />
</ItemGroup>
</Project>

Save this complete program as Program.cs. It creates a private server; await using disposes its owned scopes after success, cancellation or failure. The complete file contains explicit imports, a top-level async entry point and a cancellation deadline. View source

Program.csComplete program
using System;
using System.IO;
using System.Linq;
using System.Threading;
using LibTmux;
if (OperatingSystem.IsWindows())
{
throw new PlatformNotSupportedException("This example requires tmux on Linux or macOS.");
}
using CancellationTokenSource deadline = new(TimeSpan.FromSeconds(10));
CancellationToken token = deadline.Token;
string? testTmux = Environment.GetEnvironmentVariable("LIBTMUX_TEST_TMUX");
string? configured = !string.IsNullOrWhiteSpace(testTmux)
? testTmux
: Environment.GetEnvironmentVariable("LIBTMUX_TMUX");
string binary = string.IsNullOrWhiteSpace(configured) ? "tmux" : configured;
using TemporaryDirectory socketRoot = new();
ServerConnectionOptions options = new()
{
SocketPath = Path.Combine(socketRoot.FullName, "tmux.sock"),
ConfigurationFile = "/dev/null",
TmuxBinaryPath = binary,
};
await using OwnedServerScope owned = await Server.CreateOwnedAsync(options, token);
Server server;
await using (OwnedSessionScope session = await owned.Value.CreateOwnedSessionAsync(
new NewSessionRequest { Name = "demo", Command = "/bin/cat" }, token))
{
server = await Server.ConnectAsync(options, token);
if (server.Sessions.IsCaptured)
{
throw new InvalidOperationException("Connecting must not capture the hierarchy.");
}
Console.WriteLine("Sessions have not been captured.");
Server captured = await server.CaptureSnapshotAsync(SnapshotDepth.Panes, token);
if (!captured.Sessions.IsCaptured)
{
throw new InvalidOperationException("The requested sessions were not captured.");
}
Session capturedSession = captured.Sessions.Single();
if (!capturedSession.ActivePane.IsCaptured)
{
throw new InvalidOperationException("The active pane was not captured.");
}
Pane pane = capturedSession.ActivePane.Value;
if (pane.CurrentPath is null || pane.CurrentCommand is null)
{
throw new InvalidOperationException("The running pane should report its path and command.");
}
Console.WriteLine("The captured active pane has a path and command.");
if (server.Sessions.IsCaptured)
{
throw new InvalidOperationException("Capturing must return a new immutable handle.");
}
Console.WriteLine("The original handle is still uncaptured.");
// Keep this private daemon alive after its only owned session is disposed.
TmuxCommandResult result = await server.ExecuteCommandAsync(
["set-option", "-s", "exit-empty", "off"], token);
if (result.ExitCode != 0)
{
throw new InvalidOperationException("Could not keep the empty server alive.");
}
}
Server empty = await server.CaptureSnapshotAsync(SnapshotDepth.Sessions, token);
if (!empty.Sessions.IsCaptured || empty.Sessions.Count != 0)
{
throw new InvalidOperationException("An empty captured relation differs from an uncaptured relation.");
}
Console.WriteLine("Captured sessions can be empty.");
internal sealed class TemporaryDirectory : IDisposable
{
private readonly DirectoryInfo _directory = Directory.CreateTempSubdirectory("libtmux-example-");
public string FullName => _directory.FullName;
public void Dispose() => _directory.Delete(recursive: true);
}

Build the project and run both supported frameworks. Each program checks its result and exits unsuccessfully after an unhandled error:

Terminal window
$ dotnet build Example.csproj --configuration Release && \
dotnet bin/Release/net8.0/Example.dll && \
dotnet bin/Release/net10.0/Example.dll

Expected program output from each framework:

Sessions have not been captured.
The captured active pane has a path and command.
The original handle is still uncaptured.
Captured sessions can be empty.

Recorded calls

source calls
No call edges in the C# model.

The C# reference records declarations, not calls, so this page shows no callers or callees. That is a limit of the extracted data, not a statement that nothing calls LibTmux.Server.CaptureSnapshotAsync.

tmux command

task material

The tmux commands behind CaptureSnapshotAsync.

tmux C API

task material

The C declarations in tmux that implement these commands.

In other ports

task equivalence 19 other ports mapped

Source-verified mappings for Capture the tmux hierarchy. A mapping says these APIs perform the same task. It does not say they share defaults, errors or completion: check each page.

Capture the tmux hierarchy in each port
PortAPIDiffers from C#
C# LibTmux.Server.CaptureSnapshotAsync
Reads the server and answers a handle carrying what it found.
This page
Ruby LibTmux::Server#snapshot
Behavior: Snapshot acquisition.
None recorded in the model
Lua libtmux.Server:snapshot None recorded in the model
PowerShell LibTmux\Get-TmuxSnapshot
Capture a replacement tmux hierarchy.
free function
Acquires a replacement LibTmux.Server to -Depth (panes by default). Acquisition is not atomic; retained handles do not refresh.
Julia LibTmux.snapshot free function2 overloads
Acquires a new Snapshot over an observation interval. Topology checks do not make it a transaction; earlier snapshots do not refresh.
Erlang tmux_server:snapshot/1
Capture a complete immutable graph with default capture limits. Performs I/O. See snapshot/2 for bounds, consistency and conflict errors.
free function
Captures a new graph over an observation interval. Retained snapshots never refresh.
Elixir LibTmux.Server.snapshot/2
Captures a new graph explicitly, with bounded work and read retries.
Captures a new graph over an observation interval. A retained Snapshot never refreshes.
Common Lisp libtmux:fetch-snapshot
Acquire an eager native graph; all later accessors on it are local. TIMEOUT controls one acquisition deadline and OUTPUT-LIMIT caps combined output, including at most two retries after contradictory observations. Synchronous setup and additional cleanup can extend caller wall time. ROW-LIMIT bounds each listing; ENTITY-LIMIT bounds the complete graph; FIELD-LIMIT bounds encoded field bytes. No daemon is started. A live daemon with zero sessions yields empty vectors; connection/listing failures remain errors. Closing CLIENT leaves this snapshot usable. This is an interval observation, not a transactional snapshot.
on client
fetch-snapshot acquires a new interval observation. Retained snapshots never refresh in place.
TypeScript server.Server.snapshot
Acquire an immutable view of the server at this instant.
None recorded in the model
Rust server.Server.hierarchy
Fetch the whole hierarchy in three commands.
async
Fetches the whole hierarchy with list-sessions, list-windows -a and list-panes -a, and stitches the rows by winlink. The three listings are separate commands, so the capture is not an atomic transaction.
Go tmux.Server.Snapshot
Snapshot materializes sessions, winlinks, panes, and clients. Its sequential commands produce an observationally consistent result, not an atomic transaction. Failures remain errors rather than empty snapshots; cancellation may occur after earlier listings completed.
None recorded in the model
Java io.github.libtmux.Server.snapshot
Captures the whole hierarchy in at most four listings, retrying once if the server is replaced.
None recorded in the model
Clojure libtmux.core/snapshot!
Acquires an eager captured graph from an explicit server.
free function
Acquires a captured graph of persistent values. Retained values do not refresh after mutation or close.
Kotlin io.github.libtmux.kotlin.Server.snapshot
Captures the whole hierarchy in at most four listings, retrying once if the server is replaced.
async
Scala io.github.libtmux.scaladsl.Server.snapshot
Captures the whole hierarchy in at most four listings, retrying once if the server is replaced.
None recorded in the model
F# LibTmux.FSharp.Server.capture
Returns a new server handle captured to the requested depth.
free function
Acquires a replacement shared .NET LibTmux.Server to the requested depth. Acquisition is not atomic; retained handles do not refresh.
Swift Server.snapshot()
Reads every object from one daemon incarnation.
async
OCaml Libtmux_unix.Client.capture
Acquire a complete graph over a recorded interval, with a fixed number of listing commands. Races fail validation. Access to returned values performs no I/O, including after closing the client.
free function
Acquires a complete immutable graph over an interval. Earlier observations remain readable without refreshing.
Perl LibTmux::Server::snapshot
snapshot().
Strict acquisition observes an interval, not an atomic transaction; retained handles do not refresh.
Raku LibTmux::Server.snapshot
Acquire a captured graph explicitly. Existing snapshots remain unchanged.
Explicit I/O captures an observation interval. Existing views and relationships remain local.
No Python equivalent.

no equivalent on Server

No Haskell equivalent.

No public Haskell equivalent is implemented; the current modules expose scalar values and version parsing.

No C++ equivalent.

no equivalent on Server: sessions, windows, panes and clients each take a snapshot of one listing, and Snapshot::take needs the internal detail::Backend, so no public call reads the whole hierarchy at once

Esc
Search in

Type to search.

Reference previewOpen reference