Core Library0.xExamples

Choose documentation 1

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
Edit this page on GitHub

Examples

Start with Capture pane output for a standalone program with imports, a project file, and private-server cleanup.

Query, send and wait

This complete program creates an isolated tmux server, filters its live sessions, sends a command and waits for its output, then reads another command’s exit status. The owned server closes when the task completes. Follow the package quickstart to create an F# project and install LibTmux.FSharp, then use this as Program.fs.

Program.fsComplete program
open System
open System.IO
open System.Threading
open LibTmux
open LibTmux.FSharp
let runAsync () =
task {
use deadline = new CancellationTokenSource(TimeSpan.FromSeconds 30.)
let token = deadline.Token
// A server of its own on a private socket, without user configuration.
let socketRoot = Directory.CreateTempSubdirectory "libtmux-example-"
use _cleanup =
{ new IDisposable with
member _.Dispose() = socketRoot.Delete true
}
let options =
ServerConnectionOptions(
SocketPath = Path.Combine(socketRoot.FullName, "tmux.sock"),
ConfigurationFile = "/dev/null"
)
use! owned = options |> Server.createOwned token
// One session whose window runs a plain shell, whatever the user's login shell is.
let! session =
owned.Value |> Server.newSession token (SessionSpec.running "build" "/bin/sh")
let! pane = session |> Session.activePane token
// Type a command and wait for what it prints, not for its echo.
let! started =
pane
|> Pane.sendAndWait token (TimeSpan.FromSeconds 10.) "echo build started" "build started"
printfn "wait found: %b" started.Found
// Run a command to its exit status and read what it printed.
let! result =
pane |> Pane.run token (TimeSpan.FromSeconds 10.) "printf 'ok\\n'; exit 3"
match result with
| PaneRun.Exited status -> printfn "run: exit %d, output %A" status (List.ofSeq result.Output)
| PaneRun.Ended -> printfn "run: the shell exited first"
| PaneRun.NotStarted -> printfn "run: the shell was not at a prompt"
| PaneRun.TimedOut -> printfn "run: still running"
// List and filter: tmux narrows the listing, then every row is rechecked.
let! found =
owned.Value
|> Server.sessions
|> Query.where (SessionFields.name |> Filter.startsWith "bu")
|> Query.list token
printfn "sessions: %s" (String.Join(", ", [ for listed in found -> listed.Name ]))
}
runAsync().GetAwaiter().GetResult()
Esc
Search in

Type to search.

Reference previewOpen reference