feat: restore WithoutRenderer option

This commit is contained in:
Ayman Bagabas 2025-10-14 17:35:41 -04:00
parent 094aa2eaa9
commit 718c8e750a
No known key found for this signature in database
GPG key ID: C8D51D2157C919AC
4 changed files with 25 additions and 4 deletions

View file

@ -87,6 +87,20 @@ func WithoutSignals() ProgramOption {
}
}
// WithoutRenderer disables the renderer. When this is set output and log
// statements will be plainly sent to stdout (or another output if one is set)
// without any rendering and redrawing logic. In other words, printing and
// logging will behave the same way it would in a non-TUI commandline tool.
// This can be useful if you want to use the Bubble Tea framework for a non-TUI
// application, or to provide an additional non-TUI mode to your Bubble Tea
// programs. For example, your program could behave like a daemon if output is
// not a TTY.
func WithoutRenderer() ProgramOption {
return func(p *Program) {
p.disableRenderer = true
}
}
// WithFilter supplies an event filter that will be invoked before Bubble Tea
// processes a tea.Msg. The event filter can return any tea.Msg which will then
// get handled by Bubble Tea instead of the original event. If the event filter

View file

@ -17,6 +17,13 @@ func TestOptions(t *testing.T) {
}
})
t.Run("renderer", func(t *testing.T) {
p := NewProgram(nil, WithoutRenderer())
if !p.disableRenderer {
t.Errorf("expected renderer to be a nilRenderer, got %v", p.renderer)
}
})
t.Run("without signals", func(t *testing.T) {
p := NewProgram(nil, WithoutSignals())
if atomic.LoadUint32(&p.ignoreSignals) == 0 {

6
tea.go
View file

@ -398,10 +398,10 @@ type Program struct {
// required field when creating a new program.
initialModel Model
// DisableRenderer prevents the program from rendering to the terminal.
// disableRenderer prevents the program from rendering to the terminal.
// This can be useful for running daemon-like programs that don't require a
// UI but still want to take advantage of Bubble Tea's architecture.
DisableRenderer bool
disableRenderer bool
// handlers is a list of channels that need to be waited on before the
// program can exit.
@ -918,7 +918,7 @@ func (p *Program) Run() (returnModel Model, returnErr error) {
resizeMsg := WindowSizeMsg{Width: p.width, Height: p.height}
if p.renderer == nil {
if p.DisableRenderer {
if p.disableRenderer {
p.renderer = &nilRenderer{}
} else {
// If no renderer is set use the cursed one.

2
tty.go
View file

@ -22,7 +22,7 @@ func (p *Program) suspend() {
}
func (p *Program) initTerminal() error {
if p.DisableRenderer {
if p.disableRenderer {
return nil
}
return p.initInput()