Skip to content
Myr

chore: make input options mutually exclusive

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Christian Rocha · committed by Christian Rocha · parent 25022e9 · Browse files · 3 files+56 added−16 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+56 added−16 removed

  • other:+56 added−16 removed

3 files+56 added−16 removed

modified:options_test.go+25 added-7 removed
12 unmodified lines
13 t.Errorf("expected output to custom, got %v", p.output.TTY().Fd())14 }15 })1617 t.Run("input", func(t *testing.T) {17 t.Run("custom input", func(t *testing.T) {18 var b bytes.Buffer19 p := NewProgram(nil, WithInput(&b))20 if p.input != &b {21 t.Errorf("expected input to custom, got %v", p.input)22 }23 if p.startupOptions&withCustomInput == 0 {23 if p.inputType != customInput {24 t.Errorf("expected startup options to have custom input set, got %v", p.input)25 }26 })27
20 unmodified lines
48 t.Errorf("expected filter to be set")49 }50 })5152 t.Run("input options", func(t *testing.T) {53 exercise := func(t *testing.T, opt ProgramOption, expect inputType) {54 p := NewProgram(nil, opt)55 if p.inputType != expect {56 t.Errorf("expected input type %s, got %s", expect, p.inputType)57 }58 }5960 t.Run("tty input", func(t *testing.T) {61 exercise(t, WithInputTTY(), ttyInput)62 })6364 t.Run("custom input", func(t *testing.T) {65 var b bytes.Buffer66 exercise(t, WithInput(&b), customInput)67 })6869 })7071 t.Run("startup options", func(t *testing.T) {72 exercise := func(t *testing.T, opt ProgramOption, expect startupOptions) {73 p := NewProgram(nil, opt)74 if !p.startupOptions.has(expect) {75 t.Errorf("expected startup options have %v, got %v", expect, p.startupOptions)76 }77 }7860 t.Run("input tty", func(t *testing.T) {61 exercise(t, WithInputTTY(), withInputTTY)62 })6379 t.Run("alt screen", func(t *testing.T) {80 exercise(t, WithAltScreen(), withAltScreen)81 })82
31 unmodified lines
114 })115116 t.Run("multiple", func(t *testing.T) {117 p := NewProgram(nil, WithMouseAllMotion(), WithAltScreen(), WithInputTTY())103 for _, opt := range []startupOptions{withMouseAllMotion, withAltScreen, withInputTTY} {118 for _, opt := range []startupOptions{withMouseAllMotion, withAltScreen} {119 if !p.startupOptions.has(opt) {120 t.Errorf("expected startup options have %v, got %v", opt, p.startupOptions)121 }122 if p.inputType != ttyInput {123 t.Errorf("expected input to be %v, got %v", opt, p.startupOptions)124 }125 }126 })127}
modified:options.go+5 added-3 removed
35 unmodified lines
36 }37}3839// WithInput sets the input which, by default, is stdin. In most cases you40// won't need to use this.40// won't need to use this. To disable input entirely pass nil.41//42// p := NewProgram(model, WithInput(nil))43func WithInput(input io.Reader) ProgramOption {44 return func(p *Program) {45 p.input = input44 p.startupOptions |= withCustomInput46 p.inputType = customInput47 }48}4950// WithInputTTY opens a new TTY for input (or console input device on Windows).51func WithInputTTY() ProgramOption {52 return func(p *Program) {51 p.startupOptions |= withInputTTY53 p.inputType = ttyInput54 }55}5657// WithoutSignalHandler disables the signal handler that Bubble Tea sets up for
145 unmodified lines
modified:tea.go+26 added-6 removed
58 unmodified lines
59type Cmd func() Msg6061type handlers []chan struct{}6263type inputType int6465const (66 defaultInput inputType = iota67 ttyInput68 customInput69)7071// String implements the stringer interface for [inputType]. It is inteded to72// be used in testing.73func (i inputType) String() string {74 return [...]string{75 "default input",76 "tty input",77 "custom input",78 }[i]79}8081// Options to customize the program during its initialization. These are82// generally set with ProgramOptions.83//84// The options here are treated as bits.
6 unmodified lines
91const (92 withAltScreen startupOptions = 1 << iota93 withMouseCellMotion94 withMouseAllMotion77 withInputTTY78 withCustomInput95 withANSICompressor96 withoutSignalHandler9798 // Catching panics is incredibly useful for restoring the terminal to a
10 unmodified lines
109 // Configuration options that will set as the program is initializing,110 // treated as bits. These options can be set via various ProgramOptions.111 startupOptions startupOptions112113 inputType inputType114115 ctx context.Context116 cancel context.CancelFunc117118 msgs chan Msg
39 unmodified lines
158// NewProgram creates a new Program.159func NewProgram(model Model, opts ...ProgramOption) *Program {160 p := &Program{161 initialModel: model,144 input: os.Stdin,162 msgs: make(chan Msg),163 }164165 // Apply all options to the program.
221 unmodified lines
387 p.finished = make(chan struct{}, 1)388389 defer p.cancel()390374 switch {375 case p.startupOptions.has(withInputTTY):391 switch p.inputType {392 case defaultInput:393 p.input = os.Stdin394395 case ttyInput:396 // Open a new TTY, by request397 f, err := openInputTTY()398 if err != nil {399 return p.initialModel, err400 }401 defer f.Close() //nolint:errcheck402 p.input = f403384 case !p.startupOptions.has(withCustomInput):404 case customInput:405 // If the user hasn't set a custom input, and input's not a terminal,406 // open a TTY so we can capture input as normal. This will allow things407 // to "just work" in cases where data was piped or redirected into this408 // application.
283 unmodified lines