refactor: remove WithInputTTY option and related code

This commit is contained in:
Ayman Bagabas 2025-09-30 14:15:50 -04:00
parent 6b6414508e
commit 608155cf09
No known key found for this signature in database
GPG key ID: C8D51D2157C919AC
4 changed files with 1 additions and 83 deletions

View file

@ -15,13 +15,6 @@ import (
// p := NewProgram(model, WithInput(someInput), WithOutput(someOutput))
type ProgramOption func(*Program)
// WithInputTTY opens a new TTY for input (or console input device on Windows).
func WithInputTTY() ProgramOption {
return func(p *Program) {
p.inputType = ttyInput
}
}
// WithEnvironment sets the environment variables that the program will use.
// This useful when the program is running in a remote session (e.g. SSH) and
// you want to pass the environment variables from the remote session to the

59
tea.go
View file

@ -238,24 +238,6 @@ type CursorModel interface {
// update function.
type Cmd func() Msg
type inputType int
const (
defaultInput inputType = iota
ttyInput
customInput
)
// String implements the stringer interface for [inputType]. It is intended 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.
//
@ -338,8 +320,6 @@ type Program struct {
// program starts.
startupTitle string
inputType inputType
// ctx is the programs's internal context for signalling internal teardown.
// It is built and derived from the externalCtx in NewProgram().
ctx context.Context
@ -975,6 +955,7 @@ func (p *Program) Run(ctx context.Context) (returnModel Model, returnErr error)
p.Output = os.Stdout
}
p.input = p.Input
p.output = p.Output
p.finished = make(chan struct{})
@ -984,44 +965,6 @@ func (p *Program) Run(ctx context.Context) (returnModel Model, returnErr error)
defer p.cancel()
switch p.inputType {
case defaultInput:
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
// input. This will allow things to "just work" in cases where data was
// piped in or redirected to the application.
//
// To disable input entirely pass nil to the [WithInput] program option.
f, isFile := p.input.(term.File)
if !isFile {
break
}
if term.IsTerminal(f.Fd()) {
break
}
f, err := openInputTTY()
if err != nil {
return p.initialModel, err
}
defer f.Close() //nolint:errcheck
p.input = f
case ttyInput:
// Open a new TTY, by request
f, err := openInputTTY()
if err != nil {
return p.initialModel, err
}
defer f.Close() //nolint:errcheck
p.input = f
case customInput:
// (There is nothing extra to do.)
}
// Handle signals.
if !p.startupOptions.has(withoutSignalHandler) {
p.handlers.add(p.handleSignals())

View file

@ -34,14 +34,6 @@ func (p *Program) initInput() (err error) {
return nil
}
func openInputTTY() (*os.File, error) {
f, err := os.Open("/dev/tty")
if err != nil {
return nil, fmt.Errorf("could not open a new TTY: %w", err)
}
return f, nil
}
const suspendSupported = true
// Send SIGTSTP to the entire process group.

View file

@ -5,7 +5,6 @@ package tea
import (
"fmt"
"os"
"github.com/charmbracelet/x/term"
"golang.org/x/sys/windows"
@ -60,15 +59,6 @@ func (p *Program) initInput() (err error) {
return //nolint:nakedret
}
// Open the Windows equivalent of a TTY.
func openInputTTY() (*os.File, error) {
f, err := os.OpenFile("CONIN$", os.O_RDWR, 0o644) //nolint:mnd,gosec
if err != nil {
return nil, fmt.Errorf("error opening CONIN$: %w", err)
}
return f, nil
}
const suspendSupported = false
func suspendProcess() {}