mirror of
https://github.com/taigrr/bubbletea.git
synced 2026-04-02 02:59:09 -07:00
chore: make input options mutually exclusive
This commit is contained in:
32
tea.go
32
tea.go
@@ -60,6 +60,24 @@ type Cmd func() Msg
|
||||
|
||||
type handlers []chan struct{}
|
||||
|
||||
type inputType int
|
||||
|
||||
const (
|
||||
defaultInput inputType = iota
|
||||
ttyInput
|
||||
customInput
|
||||
)
|
||||
|
||||
// String implements the stringer interface for [inputType]. It is inteded to
|
||||
// be used in testing.
|
||||
func (i inputType) String() string {
|
||||
return [...]string{
|
||||
"default input",
|
||||
"tty input",
|
||||
"custom input",
|
||||
}[i]
|
||||
}
|
||||
|
||||
// Options to customize the program during its initialization. These are
|
||||
// generally set with ProgramOptions.
|
||||
//
|
||||
@@ -74,8 +92,6 @@ const (
|
||||
withAltScreen startupOptions = 1 << iota
|
||||
withMouseCellMotion
|
||||
withMouseAllMotion
|
||||
withInputTTY
|
||||
withCustomInput
|
||||
withANSICompressor
|
||||
withoutSignalHandler
|
||||
|
||||
@@ -94,6 +110,8 @@ type Program struct {
|
||||
// treated as bits. These options can be set via various ProgramOptions.
|
||||
startupOptions startupOptions
|
||||
|
||||
inputType inputType
|
||||
|
||||
ctx context.Context
|
||||
cancel context.CancelFunc
|
||||
|
||||
@@ -141,7 +159,6 @@ type QuitMsg struct{}
|
||||
func NewProgram(model Model, opts ...ProgramOption) *Program {
|
||||
p := &Program{
|
||||
initialModel: model,
|
||||
input: os.Stdin,
|
||||
msgs: make(chan Msg),
|
||||
}
|
||||
|
||||
@@ -371,8 +388,11 @@ func (p *Program) Run() (Model, error) {
|
||||
|
||||
defer p.cancel()
|
||||
|
||||
switch {
|
||||
case p.startupOptions.has(withInputTTY):
|
||||
switch p.inputType {
|
||||
case defaultInput:
|
||||
p.input = os.Stdin
|
||||
|
||||
case ttyInput:
|
||||
// Open a new TTY, by request
|
||||
f, err := openInputTTY()
|
||||
if err != nil {
|
||||
@@ -381,7 +401,7 @@ func (p *Program) Run() (Model, error) {
|
||||
defer f.Close() //nolint:errcheck
|
||||
p.input = f
|
||||
|
||||
case !p.startupOptions.has(withCustomInput):
|
||||
case customInput:
|
||||
// If the user hasn't set a custom input, and input's not a terminal,
|
||||
// open a TTY so we can capture input as normal. This will allow things
|
||||
// to "just work" in cases where data was piped or redirected into this
|
||||
|
||||
Reference in New Issue
Block a user