Skip to content
Myr

exec.go

This file

Path

exec.go

129 lines · 3.5 KiB · Go

Last changed by

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

History of this file

Take it

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 running 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: err}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(false); 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}