-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #320 from overmindtech/improve-fatal-errors
Improve fatal errors; improve display of messages around commands
- Loading branch information
Showing
10 changed files
with
212 additions
and
149 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"os/exec" | ||
|
||
tea "github.com/charmbracelet/bubbletea" | ||
) | ||
|
||
type ExecCommandFunc func(cmd *exec.Cmd) tea.ExecCommand | ||
|
||
// NewExecCommand returns a new ExecCommand that will print the last view from | ||
// the parent cmdModel after bubbletea has released the terminal, but before the | ||
// command is run. | ||
func (m *cmdModel) NewExecCommand(c *exec.Cmd) tea.ExecCommand { | ||
return NewExecCommand(m, c) | ||
} | ||
|
||
func NewExecCommand(parent *cmdModel, c *exec.Cmd) *cliExecCommandModel { | ||
return &cliExecCommandModel{ | ||
parent: parent, | ||
Cmd: c, | ||
} | ||
} | ||
|
||
// osExecCommand is a layer over an exec.Cmd that satisfies the ExecCommand | ||
// interface. It prints the last view from | ||
// the parent cmdModel after bubbletea has released the terminal, but before the | ||
// command is run. | ||
type cliExecCommandModel struct { | ||
parent *cmdModel | ||
*exec.Cmd | ||
} | ||
|
||
func (c cliExecCommandModel) Run() error { | ||
_, err := c.Stdout.Write([]byte(c.parent.frozenView)) | ||
if err != nil { | ||
return fmt.Errorf("failed to write view to stdout: %w", err) | ||
} | ||
return c.Cmd.Run() | ||
} | ||
|
||
// SetStdin sets stdin on underlying exec.Cmd to the given io.Reader. | ||
func (c *cliExecCommandModel) SetStdin(r io.Reader) { | ||
// If unset, have the command use the same input as the terminal. | ||
if c.Stdin == nil { | ||
c.Stdin = r | ||
} | ||
} | ||
|
||
// SetStdout sets stdout on underlying exec.Cmd to the given io.Writer. | ||
func (c *cliExecCommandModel) SetStdout(w io.Writer) { | ||
// If unset, have the command use the same output as the terminal. | ||
if c.Stdout == nil { | ||
c.Stdout = w | ||
} | ||
} | ||
|
||
// SetStderr sets stderr on the underlying exec.Cmd to the given io.Writer. | ||
func (c *cliExecCommandModel) SetStderr(w io.Writer) { | ||
// If unset, use stderr for the command's stderr | ||
if c.Stderr == nil { | ||
c.Stderr = w | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.