Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: tea.ExecPty #879

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ jobs:
build:
strategy:
matrix:
go-version: [~1.17, ^1]
go-version: [~1.18, ^1]
os: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{ matrix.os }}
env:
Expand Down
10 changes: 7 additions & 3 deletions examples/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ require (
github.com/alecthomas/chroma v0.10.0 // indirect
github.com/atotto/clipboard v0.1.4 // indirect
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/aymanbagabas/go-pty v0.2.1 // indirect
github.com/aymanbagabas/go-udiff v0.1.0 // indirect
github.com/aymerick/douceur v0.2.0 // indirect
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 // indirect
github.com/creack/pty v1.1.15 // indirect
github.com/dlclark/regexp2 v1.4.0 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/gorilla/css v1.0.0 // indirect
Expand All @@ -34,13 +36,15 @@ require (
github.com/olekukonko/tablewriter v0.0.5 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/sahilm/fuzzy v0.1.1-0.20230530133925-c48e322e2a8f // indirect
github.com/u-root/u-root v0.11.0 // indirect
github.com/yuin/goldmark v1.5.2 // indirect
github.com/yuin/goldmark-emoji v1.0.1 // indirect
golang.org/x/crypto v0.16.0 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/term v0.13.0 // indirect
golang.org/x/text v0.13.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/term v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
)

replace github.com/charmbracelet/bubbletea => ../
340 changes: 337 additions & 3 deletions examples/go.sum

Large diffs are not rendered by default.

23 changes: 20 additions & 3 deletions exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package tea

import (
"io"
"os"
"os/exec"

"github.com/aymanbagabas/go-pty"
)

// execMsg is used internally to run an ExecCommand sent with Exec.
Expand Down Expand Up @@ -51,6 +52,11 @@ func ExecProcess(c *exec.Cmd, fn ExecCallback) Cmd {
return Exec(wrapExecCommand(c), fn)
}

// ExecPty does the same as `ExecProcess`, but for a *pty.Cmd.
func ExecPty(c *pty.Cmd, fn ExecCallback) Cmd {
return Exec(wrapPtyCommand(c), fn)
}

// ExecCallback is used when executing an *exec.Command to return a message
// with an error, which may or may not be nil.
type ExecCallback func(error) Msg
Expand All @@ -64,8 +70,19 @@ type ExecCommand interface {
SetStderr(io.Writer)
}

func wrapPtyCommand(c *pty.Cmd) ExecCommand {
return &ptyCommand{c}
}

// ptyCommand wraps a pty.Cmd so that it satisfied the ExecCommand interface.
caarlos0 marked this conversation as resolved.
Show resolved Hide resolved
type ptyCommand struct{ *pty.Cmd }

func (*ptyCommand) SetStderr(io.Writer) {}
func (*ptyCommand) SetStdin(io.Reader) {}
func (*ptyCommand) SetStdout(io.Writer) {}

// wrapExecCommand wraps an exec.Cmd so that it satisfies the ExecCommand
// interface so it can be used with Exec.
// interface.
func wrapExecCommand(c *exec.Cmd) ExecCommand {
return &osExecCommand{Cmd: c}
}
Expand Down Expand Up @@ -110,7 +127,7 @@ func (p *Program) exec(c ExecCommand, fn ExecCallback) {

c.SetStdin(p.input)
c.SetStdout(p.output.TTY())
c.SetStderr(os.Stderr)
c.SetStderr(p.output.TTY())

// Execute system command.
if err := c.Run(); err != nil {
Expand Down
12 changes: 8 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
module github.com/charmbracelet/bubbletea

go 1.17
go 1.18

require (
github.com/aymanbagabas/go-pty v0.2.1
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81
github.com/mattn/go-isatty v0.0.18
github.com/mattn/go-localereader v0.0.1
Expand All @@ -11,14 +12,17 @@ require (
github.com/muesli/reflow v0.3.0
github.com/muesli/termenv v0.15.2
golang.org/x/sync v0.1.0
golang.org/x/term v0.6.0
golang.org/x/term v0.15.0
)

require (
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/creack/pty v1.1.15 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-runewidth v0.0.14 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
golang.org/x/sys v0.7.0 // indirect
golang.org/x/text v0.3.8 // indirect
github.com/u-root/u-root v0.11.0 // indirect
golang.org/x/crypto v0.16.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
)
359 changes: 354 additions & 5 deletions go.sum

Large diffs are not rendered by default.

10 changes: 7 additions & 3 deletions tutorials/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ require github.com/charmbracelet/bubbletea v0.23.2

require (
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/aymanbagabas/go-pty v0.2.1 // indirect
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 // indirect
github.com/creack/pty v1.1.15 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-isatty v0.0.18 // indirect
github.com/mattn/go-localereader v0.0.1 // indirect
Expand All @@ -16,10 +18,12 @@ require (
github.com/muesli/reflow v0.3.0 // indirect
github.com/muesli/termenv v0.15.2 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/u-root/u-root v0.11.0 // indirect
golang.org/x/crypto v0.16.0 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.7.0 // indirect
golang.org/x/term v0.6.0 // indirect
golang.org/x/text v0.3.8 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/term v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
)

replace github.com/charmbracelet/bubbletea => ../
Loading
Loading