// The P0b title plugin for opencode-native, script side: `/p0btitle` and `/p0blong` set the terminal title, and // `/p0brun` and `/p0btwice` run their input with `sh -c`, once or twice at once. How each program ended becomes the // title, which a terminal's title log keeps where a later toast replaces an earlier one, and a toast. import { define, spawn } from "opencode:plugin" import type { CommandSpec, Host } from "opencode:plugin" export default define((setup) => { const command = (name: string, title: string, slash: string, args?: boolean): CommandSpec => ({ name, title, group: "Sample", slash, palette: true, arguments: args, }) const report = (host: Host, message: string) => { host.setTitle(message) host.toast({ variant: "info", message }) } setup.command(command("p0b.title", "Set the title", "p0btitle"), (host) => { host.setTitle("p0b title") }) setup.command(command("p0b.long", "Set a long title", "p0blong"), (host) => { host.setTitle("p0b long " + "é".repeat(3000)) }) // Each run is a task of its own, so the second of `/p0btwice` asks while the first program runs. const run = (name: string, times: number) => { setup.command(command("p0b." + name, "Run a program", name, true), (host, input) => { for (let i = 0; i < times; i++) { spawn(async () => { try { const ended = await host.foreground({ command: "sh", args: ["-c", input ?? ""] }) report(host, "p0b exit " + JSON.stringify(ended)) } catch (err) { const reason = err instanceof Error ? err.message : typeof err === "string" ? err : "no reason" report(host, "p0b error " + (reason || "no reason")) } }) } }) } run("p0brun", 1) run("p0btwice", 2) })