Server.GetWindowAsync
Reads one window by identifier, throwing when it is absent.
Source and package
-
Description#
A handle that has not found a live server yet discovers one first.
Examples#
Use typed required lookups, nullable absence and explicit errors. Cancellation, invalid input and a stopped server remain exceptions. Use Linux or macOS with Git, tmux 3.2a or newer, .NET SDK 10.0.303, and the .NET 8 and 10 runtimes. In an empty directory, fetch the documented library revision:
Terminal window $ git clone https://github.com/libtmux/libtmux-dotnet.git libtmux-source && \git -C libtmux-source checkout --detach cf34b255c896d19a97e7d7fabea58609c916012dSave this complete consumer setup as global.json: View source
{"sdk": {"version": "10.0.303","rollForward": "disable","allowPrerelease": false},"test": {"runner": "Microsoft.Testing.Platform"}}Save this complete consumer setup as NuGet.config: View source
<?xml version="1.0" encoding="utf-8"?><configuration><packageSources><clear /><add key="libtmux-source" value="libtmux-source/artifacts/api-example-packages" /><add key="nuget.org" value="https://api.nuget.org/v3/index.json" /></packageSources><packageSourceMapping><clear /><packageSource key="libtmux-source"><package pattern="LibTmux" /><package pattern="LibTmux.*" /></packageSource><packageSource key="nuget.org"><package pattern="*" /></packageSource></packageSourceMapping></configuration>Save this complete consumer setup as Example.csproj: View source
<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.0.0-alpha.20]" /></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
using System;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 binary = Environment.GetEnvironmentVariable("LIBTMUX_TMUX") ?? "tmux";string socketName = "csharp-entitylookups-" + Guid.NewGuid().ToString("N");ServerConnectionOptions options = new(){SocketName = socketName,ConfigurationFile = "/dev/null",TmuxBinaryPath = binary,};Server stoppedServer;await using (OwnedServerScope owned = await Server.CreateOwnedAsync(options, token)){await using OwnedSessionScope session = await owned.Value.CreateOwnedSessionAsync(new NewSessionRequest { Name = "demo", Command = "/bin/cat" }, token);Server server = await Server.ConnectAsync(options, token);Session found = await server.GetSessionAsync(session.Value.Id, token);Window listedWindow = (await found.GetWindowsAsync(token)).Single();Window window = await server.GetWindowAsync(listedWindow.Id, token);Pane listedPane = (await window.GetPanesAsync(token)).Single();Pane pane = await server.GetPaneAsync(listedPane.Id, token);if (found.Id != session.Value.Id || pane.Id != listedPane.Id){throw new InvalidOperationException("Typed lookup returned an unexpected entity.");}SessionId missingId = new(int.MaxValue);Session? missing = await server.FindSessionAsync(missingId, token);if (missing is not null){throw new InvalidOperationException("The absent session unexpectedly exists.");}Console.WriteLine("A successful lookup can return null.");try{_ = await server.GetSessionAsync(missingId, token);throw new InvalidOperationException("GetSessionAsync must throw for an absent session.");}catch (TmuxObjectNotFoundException){Console.WriteLine("Required lookup reports TmuxObjectNotFoundException.");}using CancellationTokenSource cancelled = new();cancelled.Cancel();try{_ = await server.FindSessionAsync(session.Value.Id, cancelled.Token);throw new InvalidOperationException("Cancelled lookup must throw.");}catch (OperationCanceledException){Console.WriteLine("Cancellation remains OperationCanceledException.");}try{_ = await window.FindPaneAsync(" ", token);throw new InvalidOperationException("A blank pane target must be rejected.");}catch (ArgumentException){Console.WriteLine("A blank pane target remains ArgumentException.");}stoppedServer = server;}try{_ = await stoppedServer.FindSessionAsync(new SessionId(int.MaxValue), token);throw new InvalidOperationException("A failed read must not become null.");}catch (LibTmuxException){Console.WriteLine("A stopped server remains a LibTmuxException.");}Build the exact source package and the saved consumer, then run both supported frameworks. The local package feed prevents an unrelated published package from satisfying the restore. Each program checks its result and exits unsuccessfully after an unhandled error:
Terminal window $ export NUGET_PACKAGES="$PWD/packages" && \dotnet restore libtmux-source/src/LibTmux/LibTmux.csproj --locked-mode && \dotnet pack libtmux-source/src/LibTmux/LibTmux.csproj \--configuration Release --no-restore -p:ContinuousIntegrationBuild=true \--output "$PWD/libtmux-source/artifacts/api-example-packages" && \dotnet restore Example.csproj --configfile NuGet.config && \dotnet build Example.csproj --configuration Release --no-restore --warnaserror && \export LIBTMUX_TMUX="$(command -v tmux)" && \export TMUX_TMPDIR=/tmp/libtmux-dotnet-dev && \mkdir -p "$TMUX_TMPDIR" && \unset TMUX TMUX_PANE && \dotnet bin/Release/net8.0/Example.dll && \dotnet bin/Release/net10.0/Example.dllExpected program output from each framework:
A successful lookup can return null.Required lookup reports TmuxObjectNotFoundException.Cancellation remains OperationCanceledException.A blank pane target remains ArgumentException.A stopped server remains a LibTmuxException.- Parameters
-
-
id ( Window
Id ) – The window identifier. -
cancellationToken ( Cancellation
Token ) – Cancels the tmux command.
-
- Returns
-
A materialized window carrying captured scalar state.
- Raises
-
-
Tmux
Object – The window does not exist.Not Found Exception -
Lib
Tmux – The lookup failed, including an absent daemon.Exception
-
In other ports Look one window up by target
- Python no equivalent on
Server - Ruby
LibTmux::Server#window - Lua No source-verified Lua equivalent is recorded for this operation.
- TypeScript no equivalent on
Server - Rust
server.Server.window_by_id - Go
tmux.Server.Window - Java no equivalent on
Server - C++
libtmux::Server::window - Swift no equivalent on
Server - Kotlin No source-verified Kotlin equivalent is recorded for this operation.
- Scala No source-verified Scala equivalent is recorded for this operation.
- F# No source-verified F# equivalent is recorded for this operation.