mirror of
https://github.com/psviderski/uncloud.git
synced 2026-08-28 03:53:33 +00:00
cli: do not use interactive TUI elements when control terminal (tty) is not available (fixes #386)
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
package tui
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"charm.land/huh/v2/spinner"
|
||||
"charm.land/lipgloss/v2"
|
||||
)
|
||||
|
||||
// RunSpinner shows an animated spinner that prints title while running action.
|
||||
// When the terminal (TTY) is not available, it prints the title as plain text.
|
||||
// It renders to stderr so stdout stays clean for command output.
|
||||
func RunSpinner(ctx context.Context, title string, action func(ctx context.Context) error) error {
|
||||
// Fall back to plain text when a terminal is not available to avoid the bubbletea /dev/tty error and to keep
|
||||
// escape codes out of redirected output.
|
||||
if !IsTerminalAvailable() {
|
||||
fmt.Fprintln(os.Stderr, title)
|
||||
return action(ctx)
|
||||
}
|
||||
|
||||
return spinner.New().
|
||||
// Leading space offsets the title from the spinner glyph.
|
||||
Title(" " + title).
|
||||
Type(spinner.MiniDot).
|
||||
WithTheme(spinner.ThemeFunc(func(isDark bool) *spinner.Styles {
|
||||
return &spinner.Styles{
|
||||
Spinner: lipgloss.NewStyle().Foreground(lipgloss.Yellow),
|
||||
Title: lipgloss.NewStyle(),
|
||||
}
|
||||
})).
|
||||
WithOutput(os.Stderr).
|
||||
Context(ctx).
|
||||
ActionWithErr(action).
|
||||
Run()
|
||||
}
|
||||
Reference in New Issue
Block a user