diff --git a/options.go b/options.go index d9ce42c7c2..4719abbbbd 100644 --- a/options.go +++ b/options.go @@ -3,6 +3,7 @@ package tea import ( "context" "io" + "time" "github.com/muesli/termenv" ) @@ -151,3 +152,11 @@ func WithANSICompressor() ProgramOption { p.startupOptions |= withANSICompressor } } + +// WithMaxFPS sets a custom maximum FPS at which we should +// update the view. +func WithMaxFPS(fps uint) ProgramOption { + return func(p *Program) { + p.frameRate = time.Second / time.Duration(fps) + } +} diff --git a/standard_renderer.go b/standard_renderer.go index 17b44e2301..f5481e6939 100644 --- a/standard_renderer.go +++ b/standard_renderer.go @@ -54,11 +54,14 @@ type standardRenderer struct { // newRenderer creates a new renderer. Normally you'll want to initialize it // with os.Stdout as the first argument. -func newRenderer(out *termenv.Output, useANSICompressor bool) renderer { +func newRenderer(out *termenv.Output, useANSICompressor bool, frameRate time.Duration) renderer { + if frameRate == 0 { + frameRate = defaultFramerate + } r := &standardRenderer{ out: out, mtx: &sync.Mutex{}, - framerate: defaultFramerate, + framerate: frameRate, useANSICompressor: useANSICompressor, queuedMessageLines: []string{}, } diff --git a/tea.go b/tea.go index 6736d6a877..4577630498 100644 --- a/tea.go +++ b/tea.go @@ -19,6 +19,7 @@ import ( "runtime/debug" "sync" "syscall" + "time" "github.com/containerd/console" isatty "github.com/mattn/go-isatty" @@ -122,6 +123,10 @@ type Program struct { // as this value only comes into play on Windows, hence the ignore comment // below. windowsStdin *os.File //nolint:golint,structcheck,unused + + // frameRate specifies a custom maximum interval at which we should + // update the view. If it is 0, the default value is used. + frameRate time.Duration } // Quit is a special command that tells the Bubble Tea program to exit. @@ -391,7 +396,7 @@ func (p *Program) Run() (Model, error) { // If no renderer is set use the standard one. if p.renderer == nil { - p.renderer = newRenderer(p.output, p.startupOptions.has(withANSICompressor)) + p.renderer = newRenderer(p.output, p.startupOptions.has(withANSICompressor), p.frameRate) } // Check if output is a TTY before entering raw mode, hiding the cursor and