refactor: replace WithInput and WithOutput options with Input and Output fields on Program struct
This commit is contained in:
parent
94c70a4ff4
commit
6b6414508e
5 changed files with 71 additions and 46 deletions
|
|
@ -83,7 +83,9 @@ func TestTeaExec(t *testing.T) {
|
|||
var in bytes.Buffer
|
||||
|
||||
m := &testExecModel{cmd: test.cmd}
|
||||
p := NewProgram(m, WithInput(&in), WithOutput(&buf))
|
||||
p := NewProgram(m)
|
||||
p.Input = &in
|
||||
p.output = &buf
|
||||
if _, err := p.Run(t.Context()); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
|
|
|||
20
options.go
20
options.go
|
|
@ -1,7 +1,6 @@
|
|||
package tea
|
||||
|
||||
import (
|
||||
"io"
|
||||
"sync/atomic"
|
||||
|
||||
"github.com/charmbracelet/colorprofile"
|
||||
|
|
@ -16,25 +15,6 @@ import (
|
|||
// p := NewProgram(model, WithInput(someInput), WithOutput(someOutput))
|
||||
type ProgramOption func(*Program)
|
||||
|
||||
// WithOutput sets the output which, by default, is stdout. In most cases you
|
||||
// won't need to use this.
|
||||
func WithOutput(output io.Writer) ProgramOption {
|
||||
return func(p *Program) {
|
||||
p.output = output
|
||||
}
|
||||
}
|
||||
|
||||
// WithInput sets the input which, by default, is stdin. In most cases you
|
||||
// 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.inputType = customInput
|
||||
}
|
||||
}
|
||||
|
||||
// WithInputTTY opens a new TTY for input (or console input device on Windows).
|
||||
func WithInputTTY() ProgramOption {
|
||||
return func(p *Program) {
|
||||
|
|
|
|||
|
|
@ -79,7 +79,7 @@ func TestClearMsg(t *testing.T) {
|
|||
var in bytes.Buffer
|
||||
|
||||
m := &testModel{}
|
||||
p := NewProgram(m, WithInput(&in), WithOutput(&buf),
|
||||
p := NewProgram(m,
|
||||
WithEnvironment([]string{
|
||||
"TERM=xterm-256color", // always use xterm and 256 colors for tests
|
||||
}),
|
||||
|
|
@ -88,6 +88,9 @@ func TestClearMsg(t *testing.T) {
|
|||
// Set the initial window size for the program.
|
||||
WithWindowSize(80, 24))
|
||||
|
||||
p.Input = &in
|
||||
p.Output = &buf
|
||||
|
||||
go p.Send(append(test.cmds, Quit))
|
||||
|
||||
if _, err := p.Run(t.Context()); err != nil {
|
||||
|
|
|
|||
19
tea.go
19
tea.go
|
|
@ -321,6 +321,9 @@ func (h *channelHandlers) shutdown() {
|
|||
|
||||
// Program is a terminal user interface.
|
||||
type Program struct {
|
||||
Input io.Reader
|
||||
Output io.Writer
|
||||
|
||||
initialModel Model
|
||||
|
||||
// handlers is a list of channels that need to be waited on before the
|
||||
|
|
@ -468,11 +471,6 @@ func NewProgram(model Model, opts ...ProgramOption) *Program {
|
|||
opt(p)
|
||||
}
|
||||
|
||||
// if no output was set, set it to stdout
|
||||
if p.output == nil {
|
||||
p.output = os.Stdout
|
||||
}
|
||||
|
||||
// if no environment was set, set it to os.Environ()
|
||||
if p.environ == nil {
|
||||
p.environ = os.Environ()
|
||||
|
|
@ -970,6 +968,15 @@ func (p *Program) Run(ctx context.Context) (returnModel Model, returnErr error)
|
|||
cmds := make(chan Cmd)
|
||||
p.errs = make(chan error, 1)
|
||||
|
||||
if p.Input == nil {
|
||||
p.Input = os.Stdin
|
||||
}
|
||||
if p.Output == nil {
|
||||
p.Output = os.Stdout
|
||||
}
|
||||
|
||||
p.output = p.Output
|
||||
|
||||
p.finished = make(chan struct{})
|
||||
defer func() {
|
||||
close(p.finished)
|
||||
|
|
@ -979,7 +986,7 @@ func (p *Program) Run(ctx context.Context) (returnModel Model, returnErr error)
|
|||
|
||||
switch p.inputType {
|
||||
case defaultInput:
|
||||
p.input = os.Stdin
|
||||
p.input = p.Input
|
||||
|
||||
// The user has not set a custom input, so we need to check whether or
|
||||
// not standard input is a terminal. If it's not, we open a new TTY for
|
||||
|
|
|
|||
69
tea_test.go
69
tea_test.go
|
|
@ -68,7 +68,9 @@ func TestTeaModel(t *testing.T) {
|
|||
ctx, cancel := context.WithTimeout(t.Context(), 3*time.Second)
|
||||
defer cancel()
|
||||
|
||||
p := NewProgram(&testModel{}, WithInput(&in), WithOutput(&buf))
|
||||
p := NewProgram(&testModel{})
|
||||
p.Input = &in
|
||||
p.Output = &buf
|
||||
if _, err := p.Run(ctx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
|
@ -83,7 +85,9 @@ func TestTeaQuit(t *testing.T) {
|
|||
var in bytes.Buffer
|
||||
|
||||
m := &testModel{}
|
||||
p := NewProgram(m, WithInput(&in), WithOutput(&buf))
|
||||
p := NewProgram(m)
|
||||
p.Input = &in
|
||||
p.Output = &buf
|
||||
go func() {
|
||||
for {
|
||||
time.Sleep(time.Millisecond)
|
||||
|
|
@ -108,7 +112,9 @@ func TestTeaWaitQuit(t *testing.T) {
|
|||
errChan := make(chan error, 1)
|
||||
|
||||
m := &testModel{}
|
||||
p := NewProgram(m, WithInput(&in), WithOutput(&buf))
|
||||
p := NewProgram(m)
|
||||
p.Input = &in
|
||||
p.Output = &buf
|
||||
|
||||
go func() {
|
||||
_, err := p.Run(t.Context())
|
||||
|
|
@ -158,7 +164,9 @@ func TestTeaWaitKill(t *testing.T) {
|
|||
errChan := make(chan error, 1)
|
||||
|
||||
m := &testModel{}
|
||||
p := NewProgram(m, WithInput(&in), WithOutput(&buf))
|
||||
p := NewProgram(m)
|
||||
p.Input = &in
|
||||
p.Output = &buf
|
||||
|
||||
go func() {
|
||||
_, err := p.Run(t.Context())
|
||||
|
|
@ -212,8 +220,6 @@ func testTeaWithFilter(t *testing.T, preventCount uint32) {
|
|||
m := &testModel{}
|
||||
shutdowns := uint32(0)
|
||||
p := NewProgram(m,
|
||||
WithInput(&in),
|
||||
WithOutput(&buf),
|
||||
WithFilter(func(_ Model, msg Msg) Msg {
|
||||
if _, ok := msg.(QuitMsg); !ok {
|
||||
return msg
|
||||
|
|
@ -225,6 +231,9 @@ func testTeaWithFilter(t *testing.T, preventCount uint32) {
|
|||
return msg
|
||||
}))
|
||||
|
||||
p.Input = &in
|
||||
p.Output = &buf
|
||||
|
||||
go func() {
|
||||
for atomic.LoadUint32(&shutdowns) <= preventCount {
|
||||
time.Sleep(time.Millisecond)
|
||||
|
|
@ -245,7 +254,9 @@ func TestTeaKill(t *testing.T) {
|
|||
var in bytes.Buffer
|
||||
|
||||
m := &testModel{}
|
||||
p := NewProgram(m, WithInput(&in), WithOutput(&buf))
|
||||
p := NewProgram(m)
|
||||
p.Input = &in
|
||||
p.Output = &buf
|
||||
go func() {
|
||||
for {
|
||||
time.Sleep(time.Millisecond)
|
||||
|
|
@ -275,7 +286,9 @@ func TestTeaContext(t *testing.T) {
|
|||
var in bytes.Buffer
|
||||
|
||||
m := &testModel{}
|
||||
p := NewProgram(m, WithInput(&in), WithOutput(&buf))
|
||||
p := NewProgram(m)
|
||||
p.Input = &in
|
||||
p.Output = &buf
|
||||
go func() {
|
||||
for {
|
||||
time.Sleep(time.Millisecond)
|
||||
|
|
@ -304,7 +317,9 @@ func TestTeaContextImplodeDeadlock(t *testing.T) {
|
|||
var in bytes.Buffer
|
||||
|
||||
m := &testModel{}
|
||||
p := NewProgram(m, WithInput(&in), WithOutput(&buf))
|
||||
p := NewProgram(m)
|
||||
p.Input = &in
|
||||
p.Output = &buf
|
||||
go func() {
|
||||
for {
|
||||
time.Sleep(time.Millisecond)
|
||||
|
|
@ -331,7 +346,9 @@ func TestTeaContextBatchDeadlock(t *testing.T) {
|
|||
}
|
||||
|
||||
m := &testModel{}
|
||||
p := NewProgram(m, WithInput(&in), WithOutput(&buf))
|
||||
p := NewProgram(m)
|
||||
p.Input = &in
|
||||
p.Output = &buf
|
||||
go func() {
|
||||
for {
|
||||
time.Sleep(time.Millisecond)
|
||||
|
|
@ -360,7 +377,9 @@ func TestTeaBatchMsg(t *testing.T) {
|
|||
}
|
||||
|
||||
m := &testModel{}
|
||||
p := NewProgram(m, WithInput(&in), WithOutput(&buf))
|
||||
p := NewProgram(m)
|
||||
p.Input = &in
|
||||
p.Output = &buf
|
||||
go func() {
|
||||
p.Send(BatchMsg{inc, inc})
|
||||
|
||||
|
|
@ -392,7 +411,9 @@ func TestTeaSequenceMsg(t *testing.T) {
|
|||
}
|
||||
|
||||
m := &testModel{}
|
||||
p := NewProgram(m, WithInput(&in), WithOutput(&buf))
|
||||
p := NewProgram(m)
|
||||
p.Input = &in
|
||||
p.Output = &buf
|
||||
go p.Send(sequenceMsg{inc, inc, Quit})
|
||||
|
||||
if _, err := p.Run(t.Context()); err != nil {
|
||||
|
|
@ -416,7 +437,9 @@ func TestTeaSequenceMsgWithBatchMsg(t *testing.T) {
|
|||
}
|
||||
|
||||
m := &testModel{}
|
||||
p := NewProgram(m, WithInput(&in), WithOutput(&buf))
|
||||
p := NewProgram(m)
|
||||
p.Input = &in
|
||||
p.Output = &buf
|
||||
go p.Send(sequenceMsg{batch, inc, Quit})
|
||||
|
||||
if _, err := p.Run(t.Context()); err != nil {
|
||||
|
|
@ -437,7 +460,9 @@ func TestTeaNestedSequenceMsg(t *testing.T) {
|
|||
}
|
||||
|
||||
m := &testModel{}
|
||||
p := NewProgram(m, WithInput(&in), WithOutput(&buf))
|
||||
p := NewProgram(m)
|
||||
p.Input = &in
|
||||
p.Output = &buf
|
||||
go p.Send(sequenceMsg{inc, Sequence(inc, inc, Batch(inc, inc)), Quit})
|
||||
|
||||
if _, err := p.Run(t.Context()); err != nil {
|
||||
|
|
@ -454,7 +479,9 @@ func TestTeaSend(t *testing.T) {
|
|||
var in bytes.Buffer
|
||||
|
||||
m := &testModel{}
|
||||
p := NewProgram(m, WithInput(&in), WithOutput(&buf))
|
||||
p := NewProgram(m)
|
||||
p.Input = &in
|
||||
p.Output = &buf
|
||||
|
||||
// sending before the program is started is a blocking operation
|
||||
go p.Send(Quit())
|
||||
|
|
@ -472,7 +499,9 @@ func TestTeaNoRun(t *testing.T) {
|
|||
var in bytes.Buffer
|
||||
|
||||
m := &testModel{}
|
||||
NewProgram(m, WithInput(&in), WithOutput(&buf))
|
||||
p := NewProgram(m)
|
||||
p.Input = &in
|
||||
p.Output = &buf
|
||||
}
|
||||
|
||||
func TestTeaPanic(t *testing.T) {
|
||||
|
|
@ -480,7 +509,9 @@ func TestTeaPanic(t *testing.T) {
|
|||
var in bytes.Buffer
|
||||
|
||||
m := &testModel{}
|
||||
p := NewProgram(m, WithInput(&in), WithOutput(&buf))
|
||||
p := NewProgram(m)
|
||||
p.Input = &in
|
||||
p.Output = &buf
|
||||
go func() {
|
||||
for {
|
||||
time.Sleep(time.Millisecond)
|
||||
|
|
@ -507,7 +538,9 @@ func TestTeaGoroutinePanic(t *testing.T) {
|
|||
var in bytes.Buffer
|
||||
|
||||
m := &testModel{}
|
||||
p := NewProgram(m, WithInput(&in), WithOutput(&buf))
|
||||
p := NewProgram(m)
|
||||
p.Input = &in
|
||||
p.Output = &buf
|
||||
go func() {
|
||||
for {
|
||||
time.Sleep(time.Millisecond)
|
||||
|
|
|
|||
Loading…
Reference in a new issue