Core LibraryAPI Reference

Choose documentation 1

latest

Current version

latest
English

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

method libtmux-kotlin latest

Pane.awaitText

Package io.github.libtmux.kotlin.Pane.awaitText io.github.libtmux.kotlin

Waits until text shows in this pane.

View as Markdown

Source and package
Module
io.github.libtmux.kotlin
Declared in
Pane
Package
io.github.libtmux:libtmux-kotlin
Source
libtmux-kotlin/src/main/kotlin/io/github/libtmux/kotlin/Pane.kt
public Kotlin suspending functions Allows a function to call suspending operations and pause without blocking a thread. It does not start a coroutine by itself. Language documentation fun awaitText( text: String, timeout: Duration, every: Duration = 50.milliseconds, dispatcher: CoroutineDispatcher = Dispatchers.IO, ): TextOutcome
methodasync [source]

Description#

Cancelling the coroutine interrupts the wait. That is not a timeout, and it does not stop a program already running in the pane.

Examples#

Send input and wait for output before capturing the pane. Use JDK 25, Git, and tmux 3.2a through 3.7c on Linux, 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-java.git libtmux-source && \
git -C libtmux-source checkout f56392b5d9bc7f1f9c1631842333fb90f3d82d40

Save this consumer setup as settings.gradle.kts: View source

rootProject.name = "api-example"
includeBuild("libtmux-source")

Save this consumer setup as build.gradle.kts: View source

plugins {
kotlin("jvm") version "2.4.10"
application
}
repositories { mavenCentral() }
dependencies {
implementation("io.github.libtmux:libtmux-kotlin:0.0.1-alpha.17-SNAPSHOT")
}
java { toolchain { languageVersion.set(JavaLanguageVersion.of(25)) } }
kotlin { jvmToolchain(25) }
application { mainClass.set(providers.gradleProperty("exampleMain")) }

Save this consumer setup as gradle.properties: View source

org.gradle.jvmargs=-Xmx2g -XX:MaxMetaspaceSize=768m -Dfile.encoding=UTF-8
kotlin.daemon.jvmargs=-Xmx2g
org.gradle.workers.max=2

Save this launcher as run.sh. It creates and stops a private tmux server, including after program failure. A shutdown failure retains its socket directory and exits unsuccessfully. View source

#!/bin/sh
set -eu
if [ "$#" -eq 0 ]; then
printf 'Usage: sh run.sh program [arguments...]\n' >&2
exit 2
fi
binary=$(command -v "${TMUX_BIN:-tmux}")
mkdir -p /tmp/libtmux-java-dev
directory=$(mktemp -d /tmp/libtmux-java-dev/api.XXXXXX)
socket="$directory/tmux.sock"
config="$directory/tmux.conf"
alive() {
"$binary" -S "$socket" display-message -p '#{pid}' >/dev/null 2>&1
}
cleanup() {
status=$?
trap - 0 HUP INT TERM
if alive; then
if ! "$binary" -S "$socket" kill-server; then
printf 'Cannot stop tmux; kept %s\n' "$directory" >&2
exit 1
fi
attempts=0
while alive && [ "$attempts" -lt 100 ]; do
sleep 0.05
attempts=$((attempts + 1))
done
if alive; then
printf 'tmux is still responding; kept %s\n' "$directory" >&2
exit 1
fi
fi
rm -rf "$directory" || exit 1
exit "$status"
}
trap cleanup 0
trap 'exit 1' HUP INT TERM
unset TMUX TMUX_PANE
printf 'set-option -g default-shell /bin/sh\n' > "$config"
"$binary" -S "$socket" -f "$config" new-session -d -s work-one -n editor /bin/cat
"$binary" -S "$socket" split-window -h -t '=work-one:editor' /bin/cat
"$binary" -S "$socket" new-window -d -t '=work-one' -n logs /bin/cat
"$binary" -S "$socket" new-session -d -s work-two -n editor /bin/cat
"$@" "$binary" "$socket" "$config"
"$binary" -S "$socket" has-session -t '=work-one'
"$binary" -S "$socket" has-session -t '=work-two'

Save this complete program as src/main/kotlin/Capture.kt. It opens an ordinary client for the launcher's server; closing the client releases its transport. View source

package io.github.libtmux.examples.api.kotlin
import io.github.libtmux.ServerConfig
import io.github.libtmux.ServerEndpoint
import io.github.libtmux.SessionSpec
import io.github.libtmux.TextOutcome
import io.github.libtmux.kotlin.*
import java.nio.file.Path
import kotlin.system.exitProcess
import kotlinx.coroutines.runBlocking
import kotlin.time.Duration.Companion.seconds
/** Send input and wait for output before capturing the pane. */
fun main(args: Array<String>) {
try {
require(args.size == 3) { "expected: tmux-binary socket-path config-file" }
val config = ServerConfig.builder()
.binary(args[0])
.endpoint(ServerEndpoint.socketPath(Path.of(args[1])))
.configFile(Path.of(args[2]))
.build()
runBlocking {
withServer(config) { server ->
val session = server.newSession(
SessionSpec.builder().named("api-capture").running("/bin/sh").build()
)
val pane = session.windows.first().panes.first()
pane.sendKeys(listOf("printf 'api-%s\\n' keys", "Enter"))
check(pane.awaitText("api-keys", timeout = 5.seconds) != TextOutcome.TIMED_OUT) {
"timed out waiting for keys output"
}
pane.sendLine("printf 'api-%s\\n' line")
check(pane.awaitText("api-line", timeout = 5.seconds) != TextOutcome.TIMED_OUT) {
"timed out waiting for line output"
}
val lines = pane.capture()
for (expected in listOf("api-keys", "api-line")) {
check(lines.any { it.trim() == expected }) { "capture is missing $expected" }
println(expected)
}
}
}
} catch (error: Exception) {
if (error is InterruptedException) Thread.currentThread().interrupt()
System.err.println("Example failed: ${error.message}")
exitProcess(1)
}
}

Build the minimal consumer and run the saved program with the private fixture. An operation or cleanup error makes the command exit unsuccessfully:

Terminal window
$ ./libtmux-source/gradlew --project-dir . --console=plain --quiet \
installDist -PexampleMain=io.github.libtmux.examples.api.kotlin.CaptureKt && \
sh run.sh build/install/api-example/bin/api-example

Expected output:

api-keys
api-line
Parameters
  • text ( String )

  • timeout ( Duration ) – how long to keep looking, finite and not negative

  • every ( Duration ) – how long to leave between looks, at least 10 ms

  • dispatcher ( CoroutineDispatcher ) – where the blocking wait runs

Returns

why the wait ended

Esc

Type to search.