Skip to content
Myr

chore(exec): small API edits (#323)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* feat: add OSExec helper function for running exec.Cmds * chore: for now, un-expose WrapExecCommand * chore: move exec struff into its own file * chore(exec): better name for Exec func that wraps exec.Cmd (thanks, @toby)

authored by Christian Rocha · committed by GitHub · parent 9705056 · Browse files · 3 files+131 added−114 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+131 added−114 removed

  • other:+131 added−114 removed

3 files+131 added−114 removed

modified:examples/exec/main.go+1 added-1 removed
10 unmodified lines
11type editorFinishedMsg struct{ err error }1213func openEditor() tea.Cmd {14 c := exec.Command(os.Getenv("EDITOR")) //nolint:gosec15 return tea.Exec(tea.WrapExecCommand(c), func(err error) tea.Msg {15 return tea.ExecProcess(c, func(err error) tea.Msg {16 return editorFinishedMsg{err}17 })18}19
48 unmodified lines
added:exec.go+129 added
1package tea23import (4 "io"5 "os"6 "os/exec"7)89// execMsg is used internally to run an ExecCommand sent with Exec.10type execMsg struct {11 cmd ExecCommand12 fn ExecCallback13}1415// Exec is used to perform arbitrary I/O in a blocking fashion, effectively16// pausing the Program while execution is runnning and resuming it when17// execution has completed.18//19// Most of the time you'll want to use ExecProcess, which runs an exec.Cmd.20//21// For non-interactive i/o you should use a Cmd (that is, a tea.Cmd).22func Exec(c ExecCommand, fn ExecCallback) Cmd {23 return func() Msg {24 return execMsg{cmd: c, fn: fn}25 }26}2728// ExecProcess runs the given *exec.Cmd in a blocking fashion, effectively29// pausing the Program while the command is running. After the *exec.Cmd exists30// the Program resumes. It's useful for spawning other interactive applications31// such as editors and shells from within a Program.32//33// To produce the command, pass an *exec.Cmd and a function which returns34// a message containing the error which may have occurred when running the35// ExecCommand.36//37// type VimFinishedMsg struct { err error }38//39// c := exec.Command("vim", "file.txt")40//41// cmd := ExecProcess(c, func(err error) Msg {42// return VimFinishedMsg{err: error}43// })44//45// Or, if you don't care about errors, you could simply:46//47// cmd := ExecProcess(exec.Command("vim", "file.txt"), nil)48//49// For non-interactive i/o you should use a Cmd (that is, a tea.Cmd).50func ExecProcess(c *exec.Cmd, fn ExecCallback) Cmd {51 return Exec(wrapExecCommand(c), fn)52}5354// ExecCallback is used when executing an *exec.Command to return a message55// with an error, which may or may not be nil.56type ExecCallback func(error) Msg5758// ExecCommand can be implemented to execute things in a blocking fashion in59// the current terminal.60type ExecCommand interface {61 Run() error62 SetStdin(io.Reader)63 SetStdout(io.Writer)64 SetStderr(io.Writer)65}6667// wrapExecCommand wraps an exec.Cmd so that it satisfies the ExecCommand68// interface so it can be used with Exec.69func wrapExecCommand(c *exec.Cmd) ExecCommand {70 return &osExecCommand{Cmd: c}71}7273// osExecCommand is a layer over an exec.Cmd that satisfies the ExecCommand74// interface.75type osExecCommand struct{ *exec.Cmd }7677// SetStdin sets stdin on underlying exec.Cmd to the given io.Reader.78func (c *osExecCommand) SetStdin(r io.Reader) {79 // If unset, have the command use the same input as the terminal.80 if c.Stdin == nil {81 c.Stdin = r82 }83}8485// SetStdout sets stdout on underlying exec.Cmd to the given io.Writer.86func (c *osExecCommand) SetStdout(w io.Writer) {87 // If unset, have the command use the same output as the terminal.88 if c.Stdout == nil {89 c.Stdout = w90 }91}9293// SetStderr sets stderr on the underlying exec.Cmd to the given io.Writer.94func (c *osExecCommand) SetStderr(w io.Writer) {95 // If unset, use stderr for the command's stderr96 if c.Stderr == nil {97 c.Stderr = w98 }99}100101// exec runs an ExecCommand and delivers the results to the program as a Msg.102func (p *Program) exec(c ExecCommand, fn ExecCallback) {103 if err := p.ReleaseTerminal(); err != nil {104 // If we can't release input, abort.105 if fn != nil {106 go p.Send(fn(err))107 }108 return109 }110111 c.SetStdin(p.input)112 c.SetStdout(p.output)113 c.SetStderr(os.Stderr)114115 // Execute system command.116 if err := c.Run(); err != nil {117 _ = p.RestoreTerminal() // also try to restore the terminal.118 if fn != nil {119 go p.Send(fn(err))120 }121 return122 }123124 // Have the program re-capture input.125 err := p.RestoreTerminal()126 if fn != nil {127 go p.Send(fn(err))128 }129}
modified:tea.go+1 added-113 removed
12 unmodified lines
13 "context"14 "fmt"15 "io"16 "os"17 "os/exec"17 "os/signal"18 "runtime/debug"19 "sync"20 "syscall"
223 unmodified lines
244func HideCursor() Msg {245 return hideCursorMsg{}246}247249// Exec runs the given ExecCommand in a blocking fashion, effectively pausing250// the Program while the command is running. After the *exec.Cmd exists the251// Program resumes. It's useful for spawning other interactive applications252// such as editors and shells from within a Program.253//254// To produce the command, pass an ExecCommand and a function which returns255// a message containing the error which may have occurred when running the256// ExecCommand.257//258// type VimFinishedMsg struct { err error }259//260// c := exec.Command("vim", "file.txt")261//262// cmd := Exec(WrapExecCommand(c), func(err error) Msg {263// return VimFinishedMsg{err: error}264// })265//266// Or, if you don't care about errors you could simply:267//268// cmd := Exec(WrapExecCommand(exec.Command("vim", "file.txt")), nil)269//270// For non-interactive i/o you should use a Cmd (that is, a tea.Cmd).271func Exec(c ExecCommand, fn ExecCallback) Cmd {272 return func() Msg {273 return execMsg{cmd: c, fn: fn}274 }275}276277// ExecCallback is used when executing an *exec.Command to return a message278// with an error, which may or may not be nil.279type ExecCallback func(error) Msg280281// execMsg is used internally to run an ExecCommand sent with Exec.282type execMsg struct {283 cmd ExecCommand284 fn ExecCallback285}286248// hideCursorMsg is an internal command used to hide the cursor. You can send249// this message with HideCursor.250type hideCursorMsg struct{}251
272 unmodified lines
524 case hideCursorMsg:525 hideCursor(p.output)526527 case execMsg:567 // Note: this blocks.528 // NB: this blocks.529 p.exec(msg.cmd, msg.fn)530 }531532 // Process internal messages for the renderer.
173 unmodified lines
706 go p.Send(repaintMsg{})707708 return nil709}749750// ExecCommand can be implemented to execute things in the current751// terminal using the Exec Cmd.752type ExecCommand interface {753 Run() error754 SetStdin(io.Reader)755 SetStdout(io.Writer)756 SetStderr(io.Writer)757}758759// WrapExecCommand wraps an exec.Cmd so that it satisfies the ExecCommand760// interface.761func WrapExecCommand(c *exec.Cmd) ExecCommand {762 return &osExecCommand{Cmd: c}763}764765// osExecCommand is a layer over an exec.Cmd that satisfies the ExecCommand766// interface.767type osExecCommand struct{ *exec.Cmd }768769// SetStdin sets stdin on underlying exec.Cmd to the given io.Reader.770func (c *osExecCommand) SetStdin(r io.Reader) {771 // If unset, have the command use the same input as the terminal.772 if c.Stdin == nil {773 c.Stdin = r774 }775}776777// SetStdout sets stdout on underlying exec.Cmd to the given io.Writer.778func (c *osExecCommand) SetStdout(w io.Writer) {779 // If unset, have the command use the same output as the terminal.780 if c.Stdout == nil {781 c.Stdout = w782 }783}784785// SetStderr sets stderr on the underlying exec.Cmd to the given io.Writer.786func (c *osExecCommand) SetStderr(w io.Writer) {787 // If unset, use stderr for the command's stderr788 if c.Stderr == nil {789 c.Stderr = w790 }791}792793// exec runs an ExecCommand and delivers the results to the program as a Msg.794func (p *Program) exec(c ExecCommand, fn ExecCallback) {795 if err := p.ReleaseTerminal(); err != nil {796 // If we can't release input, abort.797 if fn != nil {798 go p.Send(fn(err))799 }800 return801 }802803 c.SetStdin(p.input)804 c.SetStdout(p.output)805 c.SetStderr(os.Stderr)806807 // Execute system command.808 if err := c.Run(); err != nil {809 _ = p.RestoreTerminal() // also try to restore the terminal.810 if fn != nil {811 go p.Send(fn(err))812 }813 return814 }815816 // Have the program re-capture input.817 err := p.RestoreTerminal()818 if fn != nil {819 go p.Send(fn(err))820 }821}