Generalize output from an *os.File to an io.Writer

This commit is contained in:
Christian Rocha 2020-12-29 22:21:17 -05:00
parent e84314c622
commit c4aeadd762
1 changed files with 26 additions and 23 deletions

33
tea.go
View File

@ -83,18 +83,10 @@ func WithInput(input io.Reader) ProgramOption {
} }
} }
// WithInputDisables disables input. Use this with caution: if you disable
// input users will not be able to exit your program until it exits itself.
func WithInputDisabled() ProgramOption {
return func(m *Program) {
m.input = nil
}
}
// WithoutCatchPanics disables the panic catching that Bubble Tea does by // WithoutCatchPanics disables the panic catching that Bubble Tea does by
// default. Note that if panic catching is disabled the terminal will be in a // default. If panic catching is disabled the terminal will be in a fairly
// fairly unusable state after a panic because Bubble Tea will not perform its // unusable state after a panic because Bubble Tea will not perform its usual
// usual cleanup on exit. // cleanup on exit.
func WithoutCatchPanics() ProgramOption { func WithoutCatchPanics() ProgramOption {
return func(m *Program) { return func(m *Program) {
m.CatchPanics = false m.CatchPanics = false
@ -107,7 +99,7 @@ type Program struct {
mtx sync.Mutex mtx sync.Mutex
output *os.File // where to send output. this will usually be os.Stdout. output io.Writer // where to send output. this will usually be os.Stdout.
input io.Reader // this will usually be os.Stdin. input io.Reader // this will usually be os.Stdin.
renderer *renderer renderer *renderer
altScreenActive bool altScreenActive bool
@ -177,6 +169,15 @@ func (p *Program) Start() error {
done = make(chan struct{}) done = make(chan struct{})
) )
// Is output a file?
outputAsFile, outputIsFile := p.output.(*os.File)
// Is output a TTY?
var isTTY bool
if outputIsFile {
isTTY = isatty.IsTerminal(outputAsFile.Fd())
}
if p.CatchPanics { if p.CatchPanics {
defer func() { defer func() {
if r := recover(); r != nil { if r := recover(); r != nil {
@ -192,7 +193,7 @@ func (p *Program) Start() error {
// Check if output is a TTY before entering raw mode, hiding the cursor and // Check if output is a TTY before entering raw mode, hiding the cursor and
// so on. // so on.
if isatty.IsTerminal(p.output.Fd()) { if isTTY {
err := initTerminal(p.output) err := initTerminal(p.output)
if err != nil { if err != nil {
return err return err
@ -233,9 +234,10 @@ func (p *Program) Start() error {
}() }()
} }
if isTTY {
// Get initial terminal size // Get initial terminal size
go func() { go func() {
w, h, err := terminal.GetSize(int(p.output.Fd())) w, h, err := terminal.GetSize(int(outputAsFile.Fd()))
if err != nil { if err != nil {
errs <- err errs <- err
} }
@ -243,7 +245,8 @@ func (p *Program) Start() error {
}() }()
// Listen for window resizes // Listen for window resizes
go listenForResize(p.output, msgs, errs) go listenForResize(outputAsFile, msgs, errs)
}
// Process commands // Process commands
go func() { go func() {