Pane.WaitForTextAsync
Waits until the pane shows literal text.
Source and package
- Qualified name
-
LibTmux .Pane. Wait For Text Async - Namespace
- LibTmux
- Declared in
Pane- Package
- LibTmux
- Source
- src/LibTmux/Pane.Waiting.cs
- Task<Pane
Wait > WaitForTextAsync( string text, TimeResult Span timeout, CancellationToken cancellationToken = default )Task<PaneWait > WaitForTextAsync( PaneResult Wait request, CancellationRequest Token cancellationToken = default ) -
Description
Text already on screen ends the wait at once as
PaneWaitOutcome.PresentAtEntry. Matching is line by line. A line the pane wrapped onto several rows is matched as one line, so text that crosses a wrap is found, including the spaces a wrapped row held; a real line break ends a line. A line may start with any number of spaces, a full pane width included, or hold rows of only spaces. Each read takes two captures and pairs their lines by the characters other than spaces. If the pane changed between them and they disagree about one line, that line alone is matched row by row; every other line is still joined.- Parameters
-
-
text(string) – The text to wait for, matched within a line; a line the pane wrapped is one line. forWaitForTextAsync(text, timeout, cancellationToken) -
timeout(TimeSpan ) – How long to wait. forWaitForTextAsync(text, timeout, cancellationToken) -
cancellationToken(CancellationToken ) – Stops the wait.Default
default -
request(PaneWait ) – The patterns and time allowed. forRequest WaitForTextAsync(request, cancellationToken)
-
- Returns
-
How the wait ended.
- Errors
-
-
Argument
Exception – The text is empty or contains a line break. forWaitForTextAsync(text, timeout, cancellationToken) -
Argument
Out – The timeout is negative or longer than 49 days.Of Range Exception -
Tmux
Transport – Control observation failed and polling was not enabled.Exception -
Tmux
Object – tmux no longer has the pane when the wait begins.Not Found Exception -
Regex
Match – A pattern exceeded its own match timeout. forTimeout Exception WaitForTextAsync(request, cancellationToken)
-
Examples
Send literal text and a named Enter key, wait for the line the command prints, then require it in the captured pane. All scopes are disposed after errors or cancellation. 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 ofLibTmuxon NuGet: View sourceExample.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 usingdisposes 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 sourceProgram.csComplete program using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Text.RegularExpressions;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);await using OwnedSessionScope session = await owned.Value.CreateOwnedSessionAsync(new NewSessionRequest{Name = "demo",Command = "env PS1='ready> ' /bin/sh",},token);Server server = await Server.ConnectAsync(options, token);Pane pane = (await server.GetPanesAsync(token)).Single();// The shell has to be reading before anything is typed, or its prompt lands in front of the output.// PS1 rides on the command because a non-interactive bash default-shell drops it from the environment.PaneWaitResult prompt = await pane.WaitForTextAsync(PaneWaitRequest.FromTextPatterns(["ready>"], simpleMatch: true)with{ Timeout = TimeSpan.FromSeconds(5) },token);if (prompt.Outcome is not (PaneWaitOutcome.Matched or PaneWaitOutcome.PresentAtEntry)){throw new InvalidOperationException($"The shell never showed its prompt: {prompt.Outcome}.");}const string marker = "api capture ready";await pane.SendKeysAsync(new SendKeysRequest { Text = $"printf '%s\\n' '{marker}'", Literal = true, Enter = false }, token);await pane.SendKeysAsync(new SendKeysRequest { Text = "Enter", Enter = false }, token);// The echoed command contains the marker, so only a whole line counts as the output.PaneWaitResult printed = await pane.WaitForTextAsync(new PaneWaitRequest{Patterns = [new Regex($"^{Regex.Escape(marker)}$", RegexOptions.CultureInvariant)],Timeout = TimeSpan.FromSeconds(5),},token);if (printed.Outcome is not (PaneWaitOutcome.Matched or PaneWaitOutcome.PresentAtEntry)){throw new InvalidOperationException($"The pane never printed the marker: {printed.Outcome}.");}IReadOnlyList<string> lines = await pane.CaptureAsync(new CapturePaneRequest { JoinWrappedLines = true }, token);// Joining wrapped lines preserves terminal padding after the output.if (!lines.Any(line => string.Equals(line.TrimEnd(), marker, StringComparison.Ordinal))){throw new InvalidOperationException("Capture did not contain the complete output line.");}Console.WriteLine(marker);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.dllExpected program output from each framework:
api capture ready
Recorded calls
source callsThe 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.Pane.WaitForTextAsync.