mirror of
https://github.com/psviderski/uncloud.git
synced 2026-08-26 11:03:34 +00:00
cli: do not use interactive TUI elements when control terminal (tty) is not available (fixes #386)
This commit is contained in:
@@ -25,7 +25,9 @@ func Confirm(title string) (bool, error) {
|
||||
Value(&confirmed),
|
||||
),
|
||||
).WithTheme(ThemeConfirm()).
|
||||
WithAccessible(true)
|
||||
WithAccessible(true).
|
||||
// Render to stderr so stdout stays clean for command output.
|
||||
WithOutput(os.Stderr)
|
||||
if err := form.Run(); err != nil {
|
||||
return false, err
|
||||
}
|
||||
@@ -51,6 +53,14 @@ func ThemeConfirmDanger() huh.Theme {
|
||||
})
|
||||
}
|
||||
|
||||
// IsTerminalAvailable reports whether the control terminal (TTY) is available so an interactive TUI can run.
|
||||
func IsTerminalAvailable() bool {
|
||||
// Bubbletea interactive programs read keyboard input from stdin and render to stdout (default) or stderr (uncloud),
|
||||
// so both must be terminals. In particular, bubbletea falls back to opening /dev/tty when stdin is not a terminal,
|
||||
// which fails when there is no controlling terminal. See https://github.com/psviderski/uncloud/issues/386
|
||||
return IsStdinTerminal() && IsStderrTerminal()
|
||||
}
|
||||
|
||||
// IsStdinTerminal checks if the standard input is a terminal (TTY).
|
||||
func IsStdinTerminal() bool {
|
||||
return term.IsTerminal(int(os.Stdin.Fd()))
|
||||
@@ -61,6 +71,11 @@ func IsStdoutTerminal() bool {
|
||||
return term.IsTerminal(int(os.Stdout.Fd()))
|
||||
}
|
||||
|
||||
// IsStderrTerminal checks if the standard error is a terminal (TTY).
|
||||
func IsStderrTerminal() bool {
|
||||
return term.IsTerminal(int(os.Stderr.Fd()))
|
||||
}
|
||||
|
||||
// TerminalWidth returns the width of the terminal.
|
||||
// Returns 0 if stdout is not a terminal or the width cannot be determined.
|
||||
func TerminalWidth() int {
|
||||
|
||||
Reference in New Issue
Block a user