diff --git a/choose/command.go b/choose/command.go index 70b8a9f..ece6cd5 100644 --- a/choose/command.go +++ b/choose/command.go @@ -11,6 +11,7 @@ import ( "github.com/charmbracelet/bubbles/help" "github.com/charmbracelet/bubbles/paginator" tea "github.com/charmbracelet/bubbletea" + "github.com/charmbracelet/gum/internal/gumtea" "github.com/charmbracelet/gum/internal/stdin" "github.com/charmbracelet/gum/internal/timeout" "github.com/charmbracelet/gum/internal/tty" @@ -153,7 +154,7 @@ func (o Options) Run() error { defer cancel() // Disable Keybindings since we will control it ourselves. - tm, err := tea.NewProgram( + tm, err := gumtea.NewProgram( m, tea.WithOutput(os.Stderr), tea.WithContext(ctx), diff --git a/confirm/command.go b/confirm/command.go index fc0a02b..687d0b6 100644 --- a/confirm/command.go +++ b/confirm/command.go @@ -8,6 +8,7 @@ import ( "github.com/charmbracelet/bubbles/help" tea "github.com/charmbracelet/bubbletea" "github.com/charmbracelet/gum/internal/exit" + "github.com/charmbracelet/gum/internal/gumtea" "github.com/charmbracelet/gum/internal/stdin" "github.com/charmbracelet/gum/internal/timeout" "github.com/charmbracelet/gum/style" @@ -45,7 +46,7 @@ func (o Options) Run() error { promptStyle: o.PromptStyle.ToLipgloss(), padding: []int{top, right, bottom, left}, } - tm, err := tea.NewProgram( + tm, err := gumtea.NewProgram( m, tea.WithOutput(os.Stderr), tea.WithContext(ctx), diff --git a/file/command.go b/file/command.go index b7cc546..7f70b07 100644 --- a/file/command.go +++ b/file/command.go @@ -9,6 +9,7 @@ import ( "github.com/charmbracelet/bubbles/filepicker" "github.com/charmbracelet/bubbles/help" tea "github.com/charmbracelet/bubbletea" + "github.com/charmbracelet/gum/internal/gumtea" "github.com/charmbracelet/gum/internal/timeout" "github.com/charmbracelet/gum/style" ) @@ -61,7 +62,7 @@ func (o Options) Run() error { ctx, cancel := timeout.Context(o.Timeout) defer cancel() - tm, err := tea.NewProgram( + tm, err := gumtea.NewProgram( &m, tea.WithOutput(os.Stderr), tea.WithContext(ctx), diff --git a/filter/command.go b/filter/command.go index 6c9b2a3..40ffa16 100644 --- a/filter/command.go +++ b/filter/command.go @@ -12,6 +12,7 @@ import ( "github.com/charmbracelet/bubbles/viewport" tea "github.com/charmbracelet/bubbletea" "github.com/charmbracelet/gum/internal/files" + "github.com/charmbracelet/gum/internal/gumtea" "github.com/charmbracelet/gum/internal/stdin" "github.com/charmbracelet/gum/internal/timeout" "github.com/charmbracelet/gum/internal/tty" @@ -143,7 +144,7 @@ func (o Options) Run() error { } } - tm, err := tea.NewProgram(m, options...).Run() + tm, err := gumtea.NewProgram(m, options...).Run() if err != nil { return fmt.Errorf("unable to run filter: %w", err) } diff --git a/input/command.go b/input/command.go index 0900d8d..1fd0d14 100644 --- a/input/command.go +++ b/input/command.go @@ -9,6 +9,7 @@ import ( "github.com/charmbracelet/bubbles/textinput" tea "github.com/charmbracelet/bubbletea" "github.com/charmbracelet/gum/cursor" + "github.com/charmbracelet/gum/internal/gumtea" "github.com/charmbracelet/gum/internal/stdin" "github.com/charmbracelet/gum/internal/timeout" "github.com/charmbracelet/gum/style" @@ -59,7 +60,7 @@ func (o Options) Run() error { ctx, cancel := timeout.Context(o.Timeout) defer cancel() - p := tea.NewProgram( + p := gumtea.NewProgram( m, tea.WithOutput(os.Stderr), tea.WithReportFocus(), diff --git a/internal/gumtea/gumtea.go b/internal/gumtea/gumtea.go new file mode 100644 index 0000000..501888d --- /dev/null +++ b/internal/gumtea/gumtea.go @@ -0,0 +1,35 @@ +// Package gumtea wraps tea.NewProgram to allow custom options. +package gumtea + +import ( + "os" + "strconv" + + tea "github.com/charmbracelet/bubbletea" +) + +// NewProgram wraps tea.NewProgram, injecting options from the environment. +func NewProgram(model tea.Model, baseOpts ...tea.ProgramOption) *tea.Program { + opts := append(baseOpts, loadOptionsFromEnv()...) + return tea.NewProgram(model, opts...) +} + +// loadOptionsFromEnv loads options from environment variables. +// This feature is provisional. It may be altered or removed in a future version of this package. +func loadOptionsFromEnv() []tea.ProgramOption { + var opts []tea.ProgramOption + + if fps := os.Getenv("GUM_FPS"); fps != "" { + if v, err := strconv.Atoi(fps); err == nil && v > 0 { + opts = append(opts, tea.WithFPS(v)) + } + } + + if alt := os.Getenv("GUM_ALTSCREEN"); alt != "" { + if altEnabled, err := strconv.ParseBool(alt); err == nil && altEnabled { + opts = append(opts, tea.WithAltScreen()) + } + } + + return opts +} diff --git a/pager/command.go b/pager/command.go index 414bee7..b90ab3c 100644 --- a/pager/command.go +++ b/pager/command.go @@ -7,6 +7,7 @@ import ( "github.com/charmbracelet/bubbles/help" "github.com/charmbracelet/bubbles/viewport" tea "github.com/charmbracelet/bubbletea" + "github.com/charmbracelet/gum/internal/gumtea" "github.com/charmbracelet/gum/internal/stdin" "github.com/charmbracelet/gum/internal/timeout" ) @@ -47,7 +48,7 @@ func (o Options) Run() error { ctx, cancel := timeout.Context(o.Timeout) defer cancel() - _, err := tea.NewProgram( + _, err := gumtea.NewProgram( m, tea.WithAltScreen(), tea.WithReportFocus(), diff --git a/spin/command.go b/spin/command.go index cff2797..6bbd747 100644 --- a/spin/command.go +++ b/spin/command.go @@ -7,6 +7,7 @@ import ( "github.com/charmbracelet/bubbles/spinner" tea "github.com/charmbracelet/bubbletea" "github.com/charmbracelet/gum/internal/exit" + "github.com/charmbracelet/gum/internal/gumtea" "github.com/charmbracelet/gum/internal/timeout" "github.com/charmbracelet/gum/style" "github.com/charmbracelet/x/term" @@ -37,7 +38,7 @@ func (o Options) Run() error { ctx, cancel := timeout.Context(o.Timeout) defer cancel() - tm, err := tea.NewProgram( + tm, err := gumtea.NewProgram( m, tea.WithOutput(os.Stderr), tea.WithContext(ctx), diff --git a/table/command.go b/table/command.go index 479cd93..62b01f1 100644 --- a/table/command.go +++ b/table/command.go @@ -8,6 +8,7 @@ import ( "github.com/charmbracelet/bubbles/help" "github.com/charmbracelet/bubbles/table" tea "github.com/charmbracelet/bubbletea" + "github.com/charmbracelet/gum/internal/gumtea" "github.com/charmbracelet/gum/internal/stdin" "github.com/charmbracelet/gum/internal/timeout" "github.com/charmbracelet/gum/style" @@ -150,7 +151,7 @@ func (o Options) Run() error { keymap: defaultKeymap(), padding: []int{top, right, bottom, left}, } - tm, err := tea.NewProgram( + tm, err := gumtea.NewProgram( m, tea.WithOutput(os.Stderr), tea.WithContext(ctx), diff --git a/write/command.go b/write/command.go index 6a745fb..fd380db 100644 --- a/write/command.go +++ b/write/command.go @@ -10,6 +10,7 @@ import ( "github.com/charmbracelet/bubbles/textarea" tea "github.com/charmbracelet/bubbletea" "github.com/charmbracelet/gum/cursor" + "github.com/charmbracelet/gum/internal/gumtea" "github.com/charmbracelet/gum/internal/stdin" "github.com/charmbracelet/gum/internal/timeout" "github.com/charmbracelet/gum/style" @@ -68,7 +69,7 @@ func (o Options) Run() error { ctx, cancel := timeout.Context(o.Timeout) defer cancel() - p := tea.NewProgram( + p := gumtea.NewProgram( m, tea.WithOutput(os.Stderr), tea.WithReportFocus(),