Skip to content

Commit

Permalink
feat: tea.ExecPty
Browse files Browse the repository at this point in the history
Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>
  • Loading branch information
caarlos0 committed Dec 4, 2023
1 parent a6f07b8 commit 45841a4
Show file tree
Hide file tree
Showing 3 changed files with 381 additions and 11 deletions.
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.
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
10 changes: 7 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/charmbracelet/bubbletea
go 1.17

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
)
Loading

0 comments on commit 45841a4

Please sign in to comment.