bubbletea/tty.go

52 lines
733 B
Go
Raw Normal View History

2020-10-13 12:52:30 -04:00
package tea
import (
"errors"
2020-10-13 12:52:30 -04:00
)
var errInputIsNotAFile = errors.New("input is not a file")
2020-10-13 12:52:30 -04:00
func (p *Program) initTerminal() error {
err := p.initInput()
if err != nil {
return err
2021-01-11 16:33:14 -05:00
}
if p.inputIsTTY {
if p.console == nil {
return errors.New("no console")
}
err = p.console.SetRaw()
2021-01-11 16:33:14 -05:00
if err != nil {
return err
}
}
if p.outputIsTTY {
enableAnsiColors(p.output)
hideCursor(p.output)
2020-10-13 12:52:30 -04:00
}
return nil
}
2021-01-11 16:33:14 -05:00
func (p Program) restoreTerminal() error {
if p.outputIsTTY {
showCursor(p.output)
}
if err := p.restoreInput(); err != nil {
return err
}
// Console will only be set if input is a TTY.
if p.console != nil {
err := p.console.Reset()
if err != nil {
return err
}
2021-01-11 16:33:14 -05:00
}
return nil
2020-10-13 12:52:30 -04:00
}