Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ sessions.

## Run the example

Use the .NET 10 SDK and tmux 3.3a or newer on Linux, macOS, or WSL. The library
Use the .NET 10 SDK and tmux 3.4 or newer on Linux, macOS, or WSL. The library
and tool use the same [published source revision](https://github.com/libtmux/libtmux-dotnet/tree/472c2bcfbda6735139849324cc109ad70300001e).
`run_shell_command` stages its command in a named tmux buffer, so this example
needs a server at version 3.4 or later; on 3.3a the tool refuses the request.
`run_shell_command` stages its command in a named tmux buffer, so the tool
refuses the request on an earlier server, such as 3.3a.

Create an empty directory:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ covers interactive attachment and detaching.

Save the complete program as `connect.exs`. The environment variable
`LIBTMUX_SOCKET_PATH` selects the existing server. The launcher below sets it
to a private socket for trying the example. The first line installs the
released 0.1 series from Hex.
to a private socket for trying the example. The first line installs libtmux
from Hex. Its requirement, `~> 0.1`, accepts any release from 0.1.0 up to, but
not including, 1.0.0.

```elixir title="connect.exs"
Mix.install([{:libtmux, "~> 0.1"}])
Expand Down
22 changes: 11 additions & 11 deletions site/src/data/home-examples.json
Original file line number Diff line number Diff line change
Expand Up @@ -188,37 +188,37 @@
"go": {
"lang": "go",
"sourceRepository": "libtmux/libtmux-go",
"sourceRevision": "6f3df55f8a41999e41225eefb3940545b834e6a2",
"sourceTree": "96e6d6d4f688bf26503e46a541a88d6fce71663c",
"sourceRevision": "c21afc69867e989410e15b3616a8c0f0b1073c33",
"sourceTree": "827524d332b79227dd4bd82b0d99e5fc12a1156e",
"program": "quickstart.go",
"excerpt": {
"startLine": 1,
"endLine": 116,
"endLine": 56,
"dedent": 0,
"importsEndLine": 0
},
"files": [
{
"name": "quickstart.go",
"lang": "go",
"code": "package main\n\nimport (\n\t\"bufio\"\n\t\"context\"\n\t\"errors\"\n\t\"fmt\"\n\t\"io\"\n\t\"log\"\n\t\"os\"\n\t\"path/filepath\"\n\t\"time\"\n\n\t\"github.com/libtmux/libtmux-go/tmux\"\n)\n\nfunc main() {\n\tif err := start(); err != nil {\n\t\tlog.Fatal(err)\n\t}\n}\n\nfunc start() (err error) {\n\troot := \"/tmp/libtmux-go-test\"\n\tif err := os.MkdirAll(root, 0700); err != nil {\n\t\treturn err\n\t}\n\tdirectory, err := os.MkdirTemp(root, \"home-\")\n\tif err != nil {\n\t\treturn err\n\t}\n\n\tserver, err := tmux.NewServer(tmux.ServerOptions{\n\t\tBinary: os.Getenv(\"TMUX_BIN\"),\n\t\tSocketPath: filepath.Join(directory, \"tmux.sock\"),\n\t\tConfigFile: \"/dev/null\",\n\t})\n\tif err != nil {\n\t\treturn errors.Join(err, os.RemoveAll(directory))\n\t}\n\n\tdefer func() {\n\t\tcleanup, cancel := context.WithTimeout(\n\t\t\tcontext.Background(),\n\t\t\t3*time.Second,\n\t\t)\n\t\tdefer cancel()\n\n\t\tif stopErr := server.Kill(cleanup); stopErr != nil {\n\t\t\terr = errors.Join(err, stopErr)\n\t\t\treturn // Keep the directory if cleanup fails.\n\t\t}\n\t\terr = errors.Join(err, os.RemoveAll(directory))\n\t}()\n\n\tctx, cancel := context.WithTimeout(\n\t\tcontext.Background(),\n\t\t10*time.Second,\n\t)\n\tdefer cancel()\n\n\treturn run(ctx, server)\n}\n\nfunc run(ctx context.Context, server tmux.Server) (err error) {\n\tsession, err := server.NewSession(ctx, tmux.NewSessionRequest{\n\t\tName: \"libtmux-go-quickstart\",\n\t\tWindowName: \"start\",\n\t})\n\tif err != nil {\n\t\treturn fmt.Errorf(\"create session: %w\", err)\n\t}\n\n\twindow, err := session.NewWindow(ctx, tmux.NewWindowRequest{\n\t\tName: new(\"work\"),\n\t})\n\tif err != nil {\n\t\treturn fmt.Errorf(\"create window: %w\", err)\n\t}\n\n\tpane, err := window.SplitPane(ctx, tmux.SplitPaneRequest{\n\t\tDirection: tmux.PaneDirectionRight,\n\t\tCommand: \"sh\",\n\t})\n\tif err != nil {\n\t\treturn fmt.Errorf(\"split window: %w\", err)\n\t}\n\n\toutput, err := pane.OpenObservation(ctx)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"watch pane: %w\", err)\n\t}\n\tdefer func() {\n\t\terr = errors.Join(err, output.Close())\n\t}()\n\n\t_, err = fmt.Fprintln(\n\t\tpane.Writer(ctx),\n\t\t\"printf 'libtmux ready\\\\n'\",\n\t)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"send command: %w\", err)\n\t}\n\tscanner := bufio.NewScanner(output.Reader(ctx))\n\tfor scanner.Scan() {\n\t\tif scanner.Text() == \"libtmux ready\" {\n\t\t\tfmt.Println(\"libtmux ready\")\n\t\t\treturn nil\n\t\t}\n\t}\n\n\tif err := scanner.Err(); err != nil {\n\t\treturn fmt.Errorf(\"read pane: %w\", err)\n\t}\n\treturn io.ErrUnexpectedEOF\n}\n"
"code": "// Command quickstart runs a command in tmux and prints what it showed.\npackage main\n\nimport (\n\t\"context\"\n\t\"errors\"\n\t\"fmt\"\n\t\"log\"\n\t\"os\"\n\n\t\"github.com/libtmux/libtmux-go/tmux\"\n)\n\nfunc main() {\n\tif err := run(context.Background()); err != nil {\n\t\tlog.Fatal(err)\n\t}\n}\n\nfunc run(ctx context.Context) (err error) {\n\t// A private server: a socket and an empty config of its own, so this\n\t// neither touches nor is changed by a tmux you already use.\n\t// The process ID in the socket name keeps one run apart from the next.\n\tserver, err := tmux.NewServer(tmux.ServerOptions{\n\t\tSocketName: fmt.Sprintf(\"libtmux-quickstart-%d\", os.Getpid()),\n\t\tConfigFile: os.DevNull,\n\t})\n\tif err != nil {\n\t\treturn fmt.Errorf(\"configure tmux: %w\", err)\n\t}\n\tsession, err := server.NewSession(ctx, tmux.NewSessionRequest{\n\t\tName: \"demo\",\n\t})\n\tif err != nil {\n\t\treturn fmt.Errorf(\"create session: %w\", err)\n\t}\n\t// Cleanup runs even when ctx is the reason the program is leaving.\n\tdefer func() {\n\t\terr = errors.Join(err, server.Kill(context.WithoutCancel(ctx)))\n\t}()\n\n\t// The command runs in a window of its own, with a terminal.\n\tresult, err := session.Run(\n\t\tctx,\n\t\t\"echo hello from tmux\",\n\t\ttmux.RunOptions{},\n\t)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"run command: %w\", err)\n\t}\n\tfor _, line := range result.Lines {\n\t\tfmt.Println(line)\n\t}\n\tfmt.Println(\"exit status:\", result.Status)\n\treturn nil\n}\n"
},
{
"name": "go.mod",
"lang": "text",
"code": "module example.com/libtmux-home\n\ngo 1.26.0\n\nrequire github.com/libtmux/libtmux-go v0.0.0\n\nreplace github.com/libtmux/libtmux-go => ./source\n"
"code": "module example.com/libtmux-home\n\ngo 1.26.0\n\nrequire github.com/libtmux/libtmux-go v0.1.0\n"
}
],
"prerequisites": "POSIX shell, Git, tmux 3.3a or newer and Go 1.26.5.",
"prerequisites": "POSIX shell, tmux 3.3a or newer and Go 1.26.5.",
"commands": [
"git init -q source\ngit -C source fetch -q --depth=1 \\\n https://github.com/libtmux/libtmux-go.git \\\n 6f3df55f8a41999e41225eefb3940545b834e6a2\ngit -C source checkout -q --detach FETCH_HEAD\ngo build -p 2 -o quickstart quickstart.go",
"go mod tidy\ngo build -p 2 -o quickstart quickstart.go",
"unset TMUX TMUX_PANE\nexport ENV=/dev/null BASH_ENV=/dev/null SHELL=/bin/sh\n./quickstart"
],
"expectedOutput": "libtmux ready\n",
"expectedOutput": "hello from tmux\nexit status: 0\n",
"variants": {
"concise": "package main\n\nimport (\n\t\"bufio\"\n\t\"context\"\n\t\"errors\"\n\t\"fmt\"\n\t\"io\"\n\t\"log\"\n\t\"os\"\n\t\"path/filepath\"\n\t\"time\"\n\n\t\"github.com/libtmux/libtmux-go/tmux\"\n)\n\nfunc main() {\n\tif err := start(); err != nil {\n\t\tlog.Fatal(err)\n\t}\n}\n\nfunc start() (err error) {\n\troot := \"/tmp/libtmux-go-test\"\n\tif err := os.MkdirAll(root, 0700); err != nil {\n\t\treturn err\n\t}\n\tdirectory, err := os.MkdirTemp(root, \"home-\")\n\tif err != nil {\n\t\treturn err\n\t}\n\n\tserver, err := tmux.NewServer(tmux.ServerOptions{\n\t\tBinary: os.Getenv(\"TMUX_BIN\"),\n\t\tSocketPath: filepath.Join(directory, \"tmux.sock\"),\n\t\tConfigFile: \"/dev/null\",\n\t})\n\tif err != nil {\n\t\treturn err\n\t}\n\n\tctx, cancel := context.WithTimeout(\n\t\tcontext.Background(),\n\t\t10*time.Second,\n\t)\n\tdefer cancel()\n\n\treturn run(ctx, server)\n}\n\nfunc run(ctx context.Context, server tmux.Server) (err error) {\n\tsession, err := server.NewSession(ctx, tmux.NewSessionRequest{\n\t\tName: \"libtmux-go-quickstart\",\n\t\tWindowName: \"start\",\n\t})\n\tif err != nil {\n\t\treturn err\n\t}\n\n\twindow, err := session.NewWindow(ctx, tmux.NewWindowRequest{\n\t\tName: new(\"work\"),\n\t})\n\tif err != nil {\n\t\treturn err\n\t}\n\n\tpane, err := window.SplitPane(ctx, tmux.SplitPaneRequest{\n\t\tDirection: tmux.PaneDirectionRight,\n\t\tCommand: \"sh\",\n\t})\n\tif err != nil {\n\t\treturn err\n\t}\n\n\toutput, err := pane.OpenObservation(ctx)\n\tif err != nil {\n\t\treturn err\n\t}\n\tdefer func() {\n\t\terr = errors.Join(err, output.Close())\n\t}()\n\n\t_, err = fmt.Fprintln(\n\t\tpane.Writer(ctx),\n\t\t\"printf 'libtmux ready\\\\n'\",\n\t)\n\tif err != nil {\n\t\treturn err\n\t}\n\tscanner := bufio.NewScanner(output.Reader(ctx))\n\tfor scanner.Scan() {\n\t\tif scanner.Text() == \"libtmux ready\" {\n\t\t\tfmt.Println(\"libtmux ready\")\n\t\t\treturn nil\n\t\t}\n\t}\n\n\tif err := scanner.Err(); err != nil {\n\t\treturn err\n\t}\n\treturn io.ErrUnexpectedEOF\n}\n",
"errors": "package main\n\nimport (\n\t\"bufio\"\n\t\"context\"\n\t\"errors\"\n\t\"fmt\"\n\t\"io\"\n\t\"log\"\n\t\"os\"\n\t\"path/filepath\"\n\t\"time\"\n\n\t\"github.com/libtmux/libtmux-go/tmux\"\n)\n\nfunc main() {\n\tif err := start(); err != nil {\n\t\tlog.Fatal(err)\n\t}\n}\n\nfunc start() (err error) {\n\troot := \"/tmp/libtmux-go-test\"\n\tif err := os.MkdirAll(root, 0700); err != nil {\n\t\treturn err\n\t}\n\tdirectory, err := os.MkdirTemp(root, \"home-\")\n\tif err != nil {\n\t\treturn err\n\t}\n\n\tserver, err := tmux.NewServer(tmux.ServerOptions{\n\t\tBinary: os.Getenv(\"TMUX_BIN\"),\n\t\tSocketPath: filepath.Join(directory, \"tmux.sock\"),\n\t\tConfigFile: \"/dev/null\",\n\t})\n\tif err != nil {\n\t\treturn err\n\t}\n\n\tctx, cancel := context.WithTimeout(\n\t\tcontext.Background(),\n\t\t10*time.Second,\n\t)\n\tdefer cancel()\n\n\treturn run(ctx, server)\n}\n\nfunc run(ctx context.Context, server tmux.Server) (err error) {\n\tsession, err := server.NewSession(ctx, tmux.NewSessionRequest{\n\t\tName: \"libtmux-go-quickstart\",\n\t\tWindowName: \"start\",\n\t})\n\tif err != nil {\n\t\treturn fmt.Errorf(\"create session: %w\", err)\n\t}\n\n\twindow, err := session.NewWindow(ctx, tmux.NewWindowRequest{\n\t\tName: new(\"work\"),\n\t})\n\tif err != nil {\n\t\treturn fmt.Errorf(\"create window: %w\", err)\n\t}\n\n\tpane, err := window.SplitPane(ctx, tmux.SplitPaneRequest{\n\t\tDirection: tmux.PaneDirectionRight,\n\t\tCommand: \"sh\",\n\t})\n\tif err != nil {\n\t\treturn fmt.Errorf(\"split window: %w\", err)\n\t}\n\n\toutput, err := pane.OpenObservation(ctx)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"watch pane: %w\", err)\n\t}\n\tdefer func() {\n\t\terr = errors.Join(err, output.Close())\n\t}()\n\n\t_, err = fmt.Fprintln(\n\t\tpane.Writer(ctx),\n\t\t\"printf 'libtmux ready\\\\n'\",\n\t)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"send command: %w\", err)\n\t}\n\tscanner := bufio.NewScanner(output.Reader(ctx))\n\tfor scanner.Scan() {\n\t\tif scanner.Text() == \"libtmux ready\" {\n\t\t\tfmt.Println(\"libtmux ready\")\n\t\t\treturn nil\n\t\t}\n\t}\n\n\tif err := scanner.Err(); err != nil {\n\t\treturn fmt.Errorf(\"read pane: %w\", err)\n\t}\n\treturn io.ErrUnexpectedEOF\n}\n",
"cleanup": "package main\n\nimport (\n\t\"bufio\"\n\t\"context\"\n\t\"errors\"\n\t\"fmt\"\n\t\"io\"\n\t\"log\"\n\t\"os\"\n\t\"path/filepath\"\n\t\"time\"\n\n\t\"github.com/libtmux/libtmux-go/tmux\"\n)\n\nfunc main() {\n\tif err := start(); err != nil {\n\t\tlog.Fatal(err)\n\t}\n}\n\nfunc start() (err error) {\n\troot := \"/tmp/libtmux-go-test\"\n\tif err := os.MkdirAll(root, 0700); err != nil {\n\t\treturn err\n\t}\n\tdirectory, err := os.MkdirTemp(root, \"home-\")\n\tif err != nil {\n\t\treturn err\n\t}\n\n\tserver, err := tmux.NewServer(tmux.ServerOptions{\n\t\tBinary: os.Getenv(\"TMUX_BIN\"),\n\t\tSocketPath: filepath.Join(directory, \"tmux.sock\"),\n\t\tConfigFile: \"/dev/null\",\n\t})\n\tif err != nil {\n\t\treturn errors.Join(err, os.RemoveAll(directory))\n\t}\n\n\tdefer func() {\n\t\tcleanup, cancel := context.WithTimeout(\n\t\t\tcontext.Background(),\n\t\t\t3*time.Second,\n\t\t)\n\t\tdefer cancel()\n\n\t\tif stopErr := server.Kill(cleanup); stopErr != nil {\n\t\t\terr = errors.Join(err, stopErr)\n\t\t\treturn // Keep the directory if cleanup fails.\n\t\t}\n\t\terr = errors.Join(err, os.RemoveAll(directory))\n\t}()\n\n\tctx, cancel := context.WithTimeout(\n\t\tcontext.Background(),\n\t\t10*time.Second,\n\t)\n\tdefer cancel()\n\n\treturn run(ctx, server)\n}\n\nfunc run(ctx context.Context, server tmux.Server) (err error) {\n\tsession, err := server.NewSession(ctx, tmux.NewSessionRequest{\n\t\tName: \"libtmux-go-quickstart\",\n\t\tWindowName: \"start\",\n\t})\n\tif err != nil {\n\t\treturn err\n\t}\n\n\twindow, err := session.NewWindow(ctx, tmux.NewWindowRequest{\n\t\tName: new(\"work\"),\n\t})\n\tif err != nil {\n\t\treturn err\n\t}\n\n\tpane, err := window.SplitPane(ctx, tmux.SplitPaneRequest{\n\t\tDirection: tmux.PaneDirectionRight,\n\t\tCommand: \"sh\",\n\t})\n\tif err != nil {\n\t\treturn err\n\t}\n\n\toutput, err := pane.OpenObservation(ctx)\n\tif err != nil {\n\t\treturn err\n\t}\n\tdefer func() {\n\t\terr = errors.Join(err, output.Close())\n\t}()\n\n\t_, err = fmt.Fprintln(\n\t\tpane.Writer(ctx),\n\t\t\"printf 'libtmux ready\\\\n'\",\n\t)\n\tif err != nil {\n\t\treturn err\n\t}\n\tscanner := bufio.NewScanner(output.Reader(ctx))\n\tfor scanner.Scan() {\n\t\tif scanner.Text() == \"libtmux ready\" {\n\t\t\tfmt.Println(\"libtmux ready\")\n\t\t\treturn nil\n\t\t}\n\t}\n\n\tif err := scanner.Err(); err != nil {\n\t\treturn err\n\t}\n\treturn io.ErrUnexpectedEOF\n}\n"
"concise": "// Command quickstart runs a command in tmux and prints what it showed.\npackage main\n\nimport (\n\t\"context\"\n\t\"fmt\"\n\t\"log\"\n\t\"os\"\n\n\t\"github.com/libtmux/libtmux-go/tmux\"\n)\n\nfunc main() {\n\tif err := run(context.Background()); err != nil {\n\t\tlog.Fatal(err)\n\t}\n}\n\nfunc run(ctx context.Context) error {\n\t// A private server: a socket and an empty config of its own, so this\n\t// neither touches nor is changed by a tmux you already use.\n\t// The process ID in the socket name keeps one run apart from the next.\n\tserver, err := tmux.NewServer(tmux.ServerOptions{\n\t\tSocketName: fmt.Sprintf(\"libtmux-quickstart-%d\", os.Getpid()),\n\t\tConfigFile: os.DevNull,\n\t})\n\tif err != nil {\n\t\treturn err\n\t}\n\tsession, err := server.NewSession(ctx, tmux.NewSessionRequest{\n\t\tName: \"demo\",\n\t})\n\tif err != nil {\n\t\treturn err\n\t}\n\n\t// The command runs in a window of its own, with a terminal.\n\tresult, err := session.Run(\n\t\tctx,\n\t\t\"echo hello from tmux\",\n\t\ttmux.RunOptions{},\n\t)\n\tif err != nil {\n\t\treturn err\n\t}\n\tfor _, line := range result.Lines {\n\t\tfmt.Println(line)\n\t}\n\tfmt.Println(\"exit status:\", result.Status)\n\treturn nil\n}\n",
"errors": "// Command quickstart runs a command in tmux and prints what it showed.\npackage main\n\nimport (\n\t\"context\"\n\t\"fmt\"\n\t\"log\"\n\t\"os\"\n\n\t\"github.com/libtmux/libtmux-go/tmux\"\n)\n\nfunc main() {\n\tif err := run(context.Background()); err != nil {\n\t\tlog.Fatal(err)\n\t}\n}\n\nfunc run(ctx context.Context) error {\n\t// A private server: a socket and an empty config of its own, so this\n\t// neither touches nor is changed by a tmux you already use.\n\t// The process ID in the socket name keeps one run apart from the next.\n\tserver, err := tmux.NewServer(tmux.ServerOptions{\n\t\tSocketName: fmt.Sprintf(\"libtmux-quickstart-%d\", os.Getpid()),\n\t\tConfigFile: os.DevNull,\n\t})\n\tif err != nil {\n\t\treturn fmt.Errorf(\"configure tmux: %w\", err)\n\t}\n\tsession, err := server.NewSession(ctx, tmux.NewSessionRequest{\n\t\tName: \"demo\",\n\t})\n\tif err != nil {\n\t\treturn fmt.Errorf(\"create session: %w\", err)\n\t}\n\n\t// The command runs in a window of its own, with a terminal.\n\tresult, err := session.Run(\n\t\tctx,\n\t\t\"echo hello from tmux\",\n\t\ttmux.RunOptions{},\n\t)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"run command: %w\", err)\n\t}\n\tfor _, line := range result.Lines {\n\t\tfmt.Println(line)\n\t}\n\tfmt.Println(\"exit status:\", result.Status)\n\treturn nil\n}\n",
"cleanup": "// Command quickstart runs a command in tmux and prints what it showed.\npackage main\n\nimport (\n\t\"context\"\n\t\"errors\"\n\t\"fmt\"\n\t\"log\"\n\t\"os\"\n\n\t\"github.com/libtmux/libtmux-go/tmux\"\n)\n\nfunc main() {\n\tif err := run(context.Background()); err != nil {\n\t\tlog.Fatal(err)\n\t}\n}\n\nfunc run(ctx context.Context) (err error) {\n\t// A private server: a socket and an empty config of its own, so this\n\t// neither touches nor is changed by a tmux you already use.\n\t// The process ID in the socket name keeps one run apart from the next.\n\tserver, err := tmux.NewServer(tmux.ServerOptions{\n\t\tSocketName: fmt.Sprintf(\"libtmux-quickstart-%d\", os.Getpid()),\n\t\tConfigFile: os.DevNull,\n\t})\n\tif err != nil {\n\t\treturn err\n\t}\n\tsession, err := server.NewSession(ctx, tmux.NewSessionRequest{\n\t\tName: \"demo\",\n\t})\n\tif err != nil {\n\t\treturn err\n\t}\n\t// Cleanup runs even when ctx is the reason the program is leaving.\n\tdefer func() {\n\t\terr = errors.Join(err, server.Kill(context.WithoutCancel(ctx)))\n\t}()\n\n\t// The command runs in a window of its own, with a terminal.\n\tresult, err := session.Run(\n\t\tctx,\n\t\t\"echo hello from tmux\",\n\t\ttmux.RunOptions{},\n\t)\n\tif err != nil {\n\t\treturn err\n\t}\n\tfor _, line := range result.Lines {\n\t\tfmt.Println(line)\n\t}\n\tfmt.Println(\"exit status:\", result.Status)\n\treturn nil\n}\n"
}
},
"java": {
Expand Down
Loading
Loading