Skip to content

Commit

Permalink
chore: make input options mutually exclusive
Browse files Browse the repository at this point in the history
  • Loading branch information
meowgorithm committed May 5, 2023
1 parent 25022e9 commit fcc805f
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 16 deletions.
8 changes: 5 additions & 3 deletions options.go
Expand Up @@ -37,18 +37,20 @@ func WithOutput(output io.Writer) ProgramOption {
}

// WithInput sets the input which, by default, is stdin. In most cases you
// won't need to use this.
// won't need to use this. To disable input entirely pass nil.
//
// p := NewProgram(model, WithInput(nil))
func WithInput(input io.Reader) ProgramOption {
return func(p *Program) {
p.input = input
p.startupOptions |= withCustomInput
p.inputType = customInput
}
}

// WithInputTTY opens a new TTY for input (or console input device on Windows).
func WithInputTTY() ProgramOption {
return func(p *Program) {
p.startupOptions |= withInputTTY
p.inputType = ttyInput
}
}

Expand Down
32 changes: 25 additions & 7 deletions options_test.go
Expand Up @@ -14,13 +14,13 @@ func TestOptions(t *testing.T) {
}
})

t.Run("input", func(t *testing.T) {
t.Run("custom input", func(t *testing.T) {
var b bytes.Buffer
p := NewProgram(nil, WithInput(&b))
if p.input != &b {
t.Errorf("expected input to custom, got %v", p.input)
}
if p.startupOptions&withCustomInput == 0 {
if p.inputType != customInput {
t.Errorf("expected startup options to have custom input set, got %v", p.input)
}
})
Expand Down Expand Up @@ -49,6 +49,25 @@ func TestOptions(t *testing.T) {
}
})

t.Run("input options", func(t *testing.T) {
exercise := func(t *testing.T, opt ProgramOption, expect inputType) {
p := NewProgram(nil, opt)
if p.inputType != expect {
t.Errorf("expected input type %s, got %s", expect, p.inputType)
}
}

t.Run("tty input", func(t *testing.T) {
exercise(t, WithInputTTY(), ttyInput)
})

t.Run("custom input", func(t *testing.T) {
var b bytes.Buffer
exercise(t, WithInput(&b), customInput)
})

})

t.Run("startup options", func(t *testing.T) {
exercise := func(t *testing.T, opt ProgramOption, expect startupOptions) {
p := NewProgram(nil, opt)
Expand All @@ -57,10 +76,6 @@ func TestOptions(t *testing.T) {
}
}

t.Run("input tty", func(t *testing.T) {
exercise(t, WithInputTTY(), withInputTTY)
})

t.Run("alt screen", func(t *testing.T) {
exercise(t, WithAltScreen(), withAltScreen)
})
Expand Down Expand Up @@ -100,10 +115,13 @@ func TestOptions(t *testing.T) {

t.Run("multiple", func(t *testing.T) {
p := NewProgram(nil, WithMouseAllMotion(), WithAltScreen(), WithInputTTY())
for _, opt := range []startupOptions{withMouseAllMotion, withAltScreen, withInputTTY} {
for _, opt := range []startupOptions{withMouseAllMotion, withAltScreen} {
if !p.startupOptions.has(opt) {
t.Errorf("expected startup options have %v, got %v", opt, p.startupOptions)
}
if p.inputType != ttyInput {
t.Errorf("expected input to be %v, got %v", opt, p.startupOptions)
}
}
})
}
32 changes: 26 additions & 6 deletions tea.go
Expand Up @@ -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.
//
Expand All @@ -74,8 +92,6 @@ const (
withAltScreen startupOptions = 1 << iota
withMouseCellMotion
withMouseAllMotion
withInputTTY
withCustomInput
withANSICompressor
withoutSignalHandler

Expand All @@ -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

Expand Down Expand Up @@ -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),
}

Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand Down

0 comments on commit fcc805f

Please sign in to comment.