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
Provenance
- Commit
fcc805f- Tree
adef6eb- Author
Christian Rocha <christian@rocha.is>- Committer
Christian Rocha <christian@rocha.is>- Parent
25022e9
3 files changed+56 added−16 removed
modified:options_test.go+25 added-7 removed
12 unmodified lines13 t.Errorf("expected output to custom, got %v", p.output.TTY().Fd())14 }15 })16 17- 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 lines48 t.Errorf("expected filter to be set")49 }50 })51 52+ 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+ }59+60+ t.Run("tty input", func(t *testing.T) {61+ exercise(t, WithInputTTY(), ttyInput)62+ })63+64+ t.Run("custom input", func(t *testing.T) {65+ var b bytes.Buffer66+ exercise(t, WithInput(&b), customInput)67+ })68+69+ })70+71 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 }78 60- t.Run("input tty", func(t *testing.T) {61- exercise(t, WithInputTTY(), withInputTTY)62- })63-79 t.Run("alt screen", func(t *testing.T) {80 exercise(t, WithAltScreen(), withAltScreen)81 })82 31 unmodified lines114 })115 116 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 lines36 }37 }38 39 // 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))43 func WithInput(input io.Reader) ProgramOption {44 return func(p *Program) {45 p.input = input44- p.startupOptions |= withCustomInput46+ p.inputType = customInput47 }48 }49 50 // WithInputTTY opens a new TTY for input (or console input device on Windows).51 func WithInputTTY() ProgramOption {52 return func(p *Program) {51- p.startupOptions |= withInputTTY53+ p.inputType = ttyInput54 }55 }56 57 // WithoutSignalHandler disables the signal handler that Bubble Tea sets up for145 unmodified linesmodified:tea.go+26 added-6 removed
58 unmodified lines59 type Cmd func() Msg60 61 type handlers []chan struct{}62 63+type inputType int64+65+const (66+ defaultInput inputType = iota67+ ttyInput68+ customInput69+)70+71+// String implements the stringer interface for [inputType]. It is inteded to72+// be used in testing.73+func (i inputType) String() string {74+ return [...]string{75+ "default input",76+ "tty input",77+ "custom input",78+ }[i]79+}80+81 // 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 lines91 const (92 withAltScreen startupOptions = 1 << iota93 withMouseCellMotion94 withMouseAllMotion77- withInputTTY78- withCustomInput95 withANSICompressor96 withoutSignalHandler97 98 // Catching panics is incredibly useful for restoring the terminal to a10 unmodified lines109 // Configuration options that will set as the program is initializing,110 // treated as bits. These options can be set via various ProgramOptions.111 startupOptions startupOptions112 113+ inputType inputType114+115 ctx context.Context116 cancel context.CancelFunc117 118 msgs chan Msg39 unmodified lines158 // NewProgram creates a new Program.159 func NewProgram(model Model, opts ...ProgramOption) *Program {160 p := &Program{161 initialModel: model,144- input: os.Stdin,162 msgs: make(chan Msg),163 }164 165 // Apply all options to the program.221 unmodified lines387 p.finished = make(chan struct{}, 1)388 389 defer p.cancel()390 374- switch {375- case p.startupOptions.has(withInputTTY):391+ switch p.inputType {392+ case defaultInput:393+ p.input = os.Stdin394+395+ 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 = f403 384- 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