Capture pane output
Needs libtmux for Scala 0.x Install
This complete Scala program starts a private tmux server, sends a command, and captures the line it prints. It includes imports, setup, and cleanup. You need tmux 3.3a or newer and a Unix environment; no existing session is required.
Read what’s on screen
The leading newline puts the output on a fresh row. Matching the whole line avoids mistaking the echoed command for its output.
import io.github.libtmux.{ServerConfig, ServerEndpoint, SessionSpec}import io.github.libtmux.scaladsl.*import java.nio.file.{Files, Path}import java.time.Durationimport scala.util.Using
object Capture { def main(args: Array[String]): Unit = { val directory = Files.createTempDirectory("libtmux-capture-") val socket = directory.resolve("tmux.sock") val config = ServerConfig.builder() .endpoint(ServerEndpoint.socketPath(socket)) .configFile(Path.of("/dev/null")) .defaultTimeout(Duration.ofSeconds(1)) .build() var failure: Option[Throwable] = None try { Using.resource(Server.open(config)) { server => try { val session = server.newSession(SessionSpec.builder() .named("capture").running("/bin/sh").env("ENV", "/dev/null").build()) val pane = session.windows.head.panes.head pane.sendLine("printf '\\nlibtmux capture ready\\n'") val deadline = System.nanoTime() + Duration.ofSeconds(5).toNanos var captured = false while (!captured && System.nanoTime() < deadline) { captured = pane.capture().contains("libtmux capture ready") if (!captured) Thread.sleep(25) } if (!captured) throw new IllegalStateException("Output did not arrive within five seconds") println("libtmux capture ready") } catch { case error: Throwable => failure = Some(error) throw error } finally { try { if (Files.exists(socket)) server.killServer() Files.deleteIfExists(socket) Files.delete(directory) } catch { case cleanup: Throwable => failure match { case Some(original) => original.addSuppressed(cleanup) case None => throw cleanup } } } } } finally { // Opening a client can fail before the cleanup block is entered. if (!Files.exists(socket)) Files.deleteIfExists(directory) } }}Wait for output or completion
The Scala facade uses blocking calls. The loop checks complete captured lines against a monotonic deadline. Using.resource closes the client; the finally block also stops the tmux server this program created. Cleanup errors stay visible, and a server that cannot be stopped keeps its socket.
Capture reads screen state and scrollback, so output that has scrolled away
may be absent. The program prints libtmux capture ready when its check passes
and exits unsuccessfully if an operation fails.
Setup and run
Use an empty directory and save the files using the displayed names. You need
JDK 25 and Gradle 9. The project files select Scala 3.9.0 and install
libtmux-scala_3 0.1.2 from Maven Central.
Save settings.gradle.kts beside Capture.scala.
rootProject.name = "capture"Save build.gradle.kts beside Capture.scala.
plugins { application scala}
repositories { mavenCentral() }dependencies { implementation("org.scala-lang:scala3-library_3:3.9.0") implementation("io.github.libtmux:libtmux-scala_3:0.1.2")}java { toolchain { languageVersion.set(JavaLanguageVersion.of(25)) } }sourceSets.main { scala.srcDir("."); scala.include("Capture.scala") }application { mainClass.set("Capture") }Save gradle.properties beside Capture.scala.
org.gradle.jvmargs=-Xmx2g -XX:MaxMetaspaceSize=768m -Dfile.encoding=UTF-8kotlin.daemon.jvmargs=-Xmx2gorg.gradle.workers.max=2Build and run the program:
$ gradle --console=plain --no-daemon runWhere this comes from
This program uses libtmux-scala_3 0.1.2 from Maven Central. Its
source
is on GitHub.