Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(feat): Add option to set max FPS #578

Merged
merged 2 commits into from
Jun 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,3 +200,12 @@ func WithFilter(filter func(Model, Msg) Msg) ProgramOption {
p.filter = filter
}
}

// WithMaxFPS sets a custom maximum FPS at which the renderer should run. If
// less than 1, the default value of 60 will be used. If over 120, the FPS
// will be capped at 120.
func WithFPS(fps int) ProgramOption {
return func(p *Program) {
p.fps = fps
}
}
12 changes: 9 additions & 3 deletions standard_renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import (
const (
// defaultFramerate specifies the maximum interval at which we should
// update the view.
defaultFramerate = time.Second / 60
defaultFPS = 60
maxFPS = 120
)

// standardRenderer is a framerate-based terminal renderer, updating the view
Expand Down Expand Up @@ -54,12 +55,17 @@ 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, fps int) renderer {
if fps < 1 {
fps = defaultFPS
} else if fps > maxFPS {
fps = maxFPS
}
r := &standardRenderer{
out: out,
mtx: &sync.Mutex{},
done: make(chan struct{}),
framerate: defaultFramerate,
framerate: time.Second / time.Duration(fps),
useANSICompressor: useANSICompressor,
queuedMessageLines: []string{},
}
Expand Down
6 changes: 5 additions & 1 deletion tea.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ type Program struct {
windowsStdin *os.File //nolint:golint,structcheck,unused

filter func(Model, Msg) Msg

// fps is the frames per second we should set on the renderer, if
// applicable,
fps int
}

// Quit is a special command that tells the Bubble Tea program to exit.
Expand Down Expand Up @@ -441,7 +445,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.fps)
}

// Check if output is a TTY before entering raw mode, hiding the cursor and
Expand Down