forked from Mirrors/bubbletea
(feat): Add option to set max FPS (#578)
* (feat): add option for setting FPS on renderer Co-authored-by: Christian Rocha <christian@rocha.is>
This commit is contained in:
parent
44f17fa1c0
commit
f3e1b67605
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
@ -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{},
|
||||
}
|
||||
|
|
6
tea.go
6
tea.go
|
@ -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.
|
||||
|
@ -445,7 +449,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
|
||||
|
|
Loading…
Reference in New Issue