main zmx / examples / run_and_wait.zig
Eric Bower  ·  2026-10-20
 1//! Example: run a command in a zmx session, stream its output, then read the
 2//! scrollback and clean up.
 3const std = @import("std");
 4const zmx = @import("zmx");
 5
 6pub fn main(init: std.process.Init) !void {
 7    const gpa = init.gpa;
 8    const io = init.io;
 9
10    // Run `echo "hello from libzmx"` in a task-mode session.
11    // With detached = false, output is streamed to stdout while it runs.
12    const exit_code = try zmx.run(
13        gpa,
14        io,
15        "example-run",
16        &.{ "echo", "hello from libzmx" },
17        .{},
18    );
19
20    // Read the full scrollback of the session.
21    const scrollback = try zmx.history(gpa, io, "example-run", .plain);
22    defer gpa.free(scrollback);
23
24    var buf: [4096]u8 = undefined;
25    var w = std.Io.File.stdout().writer(io, &buf);
26    try w.interface.print("exit code: {d}\n--- scrollback ---\n", .{exit_code});
27    try w.interface.writeAll(scrollback);
28    try w.interface.flush();
29
30    // Clean up the session.
31    _ = try zmx.kill(gpa, io, "example-run", .{});
32}