Skip to content

Commit

Permalink
(feat): Add option to set max FPS
Browse files Browse the repository at this point in the history
Added the `WithMaxFPS` option to bubbletea program struct, if it is set
with a non 0 value it will set the maximum interval at which the view is
updated, if it is not set or set to 0, the interval will be the
`defaultFramerate` constant.
  • Loading branch information
tomfeigin committed Oct 26, 2022
1 parent 280bc6f commit 7bf9be4
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
9 changes: 9 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package tea
import (
"context"
"io"
"time"

"github.com/muesli/termenv"
)
Expand Down Expand Up @@ -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)
}
}
7 changes: 5 additions & 2 deletions standard_renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{},
}
Expand Down
7 changes: 6 additions & 1 deletion tea.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"runtime/debug"
"sync"
"syscall"
"time"

"github.com/containerd/console"
isatty "github.com/mattn/go-isatty"
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 7bf9be4

Please sign in to comment.