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
Provenance
- Commit
775dbfb- Tree
a3ed00e- Author
Christian Rocha <christian@rocha.is>- Committer
GitHub <noreply@github.com>- Parent
9705056
3 files changed+131 added−114 removed
modified:examples/exec/main.go+1 added-1 removed
10 unmodified lines11 type editorFinishedMsg struct{ err error }12 13 func 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 linesadded:exec.go+129 added
1+package tea2+3+import (4+ "io"5+ "os"6+ "os/exec"7+)8+9+// execMsg is used internally to run an ExecCommand sent with Exec.10+type execMsg struct {11+ cmd ExecCommand12+ fn ExecCallback13+}14+15+// 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).22+func Exec(c ExecCommand, fn ExecCallback) Cmd {23+ return func() Msg {24+ return execMsg{cmd: c, fn: fn}25+ }26+}27+28+// 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).50+func ExecProcess(c *exec.Cmd, fn ExecCallback) Cmd {51+ return Exec(wrapExecCommand(c), fn)52+}53+54+// ExecCallback is used when executing an *exec.Command to return a message55+// with an error, which may or may not be nil.56+type ExecCallback func(error) Msg57+58+// ExecCommand can be implemented to execute things in a blocking fashion in59+// the current terminal.60+type ExecCommand interface {61+ Run() error62+ SetStdin(io.Reader)63+ SetStdout(io.Writer)64+ SetStderr(io.Writer)65+}66+67+// wrapExecCommand wraps an exec.Cmd so that it satisfies the ExecCommand68+// interface so it can be used with Exec.69+func wrapExecCommand(c *exec.Cmd) ExecCommand {70+ return &osExecCommand{Cmd: c}71+}72+73+// osExecCommand is a layer over an exec.Cmd that satisfies the ExecCommand74+// interface.75+type osExecCommand struct{ *exec.Cmd }76+77+// SetStdin sets stdin on underlying exec.Cmd to the given io.Reader.78+func (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+}84+85+// SetStdout sets stdout on underlying exec.Cmd to the given io.Writer.86+func (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+}92+93+// SetStderr sets stderr on the underlying exec.Cmd to the given io.Writer.94+func (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+}100+101+// exec runs an ExecCommand and delivers the results to the program as a Msg.102+func (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+ }110+111+ c.SetStdin(p.input)112+ c.SetStdout(p.output)113+ c.SetStderr(os.Stderr)114+115+ // 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+ }123+124+ // 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 lines13 "context"14 "fmt"15 "io"16 "os"17- "os/exec"17 "os/signal"18 "runtime/debug"19 "sync"20 "syscall"223 unmodified lines244 func HideCursor() Msg {245 return hideCursorMsg{}246 }247 249-// 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).271-func Exec(c ExecCommand, fn ExecCallback) Cmd {272- return func() Msg {273- return execMsg{cmd: c, fn: fn}274- }275-}276-277-// ExecCallback is used when executing an *exec.Command to return a message278-// with an error, which may or may not be nil.279-type ExecCallback func(error) Msg280-281-// execMsg is used internally to run an ExecCommand sent with Exec.282-type execMsg struct {283- cmd ExecCommand284- fn ExecCallback285-}286-248 // hideCursorMsg is an internal command used to hide the cursor. You can send249 // this message with HideCursor.250 type hideCursorMsg struct{}251 272 unmodified lines524 case hideCursorMsg:525 hideCursor(p.output)526 527 case execMsg:567- // Note: this blocks.528+ // NB: this blocks.529 p.exec(msg.cmd, msg.fn)530 }531 532 // Process internal messages for the renderer.173 unmodified lines706 go p.Send(repaintMsg{})707 708 return nil709 }749-750-// ExecCommand can be implemented to execute things in the current751-// terminal using the Exec Cmd.752-type ExecCommand interface {753- Run() error754- SetStdin(io.Reader)755- SetStdout(io.Writer)756- SetStderr(io.Writer)757-}758-759-// WrapExecCommand wraps an exec.Cmd so that it satisfies the ExecCommand760-// interface.761-func WrapExecCommand(c *exec.Cmd) ExecCommand {762- return &osExecCommand{Cmd: c}763-}764-765-// osExecCommand is a layer over an exec.Cmd that satisfies the ExecCommand766-// interface.767-type osExecCommand struct{ *exec.Cmd }768-769-// SetStdin sets stdin on underlying exec.Cmd to the given io.Reader.770-func (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-}776-777-// SetStdout sets stdout on underlying exec.Cmd to the given io.Writer.778-func (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-}784-785-// SetStderr sets stderr on the underlying exec.Cmd to the given io.Writer.786-func (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-}792-793-// exec runs an ExecCommand and delivers the results to the program as a Msg.794-func (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- }802-803- c.SetStdin(p.input)804- c.SetStdout(p.output)805- c.SetStderr(os.Stderr)806-807- // 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- }815-816- // Have the program re-capture input.817- err := p.RestoreTerminal()818- if fn != nil {819- go p.Send(fn(err))820- }821-}