Skip to content

v2: Consider erroring or warning when trying to print on the scrollback buffer while AltScreen is enabled #1384

Open
@andreynering

Description

@andreynering

Take this snippet as an example:

package main

import (
	tea "github.com/charmbracelet/bubbletea/v2"
)

type model struct{}

func (m model) Init() tea.Cmd {
	return nil
}

func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
	switch msg := msg.(type) {
	case tea.KeyMsg:
		switch msg.String() {
		case "q":
			return m, tea.Quit
		default:
			return m, tea.Printf("Key pressed: %s", msg.String())
		}
	}
	return m, nil
}

func (m model) View() string {
	return `Logging stuff. Press "q" to exit.`
}

func main() {
	p := tea.NewProgram(model{}, tea.WithAltScreen())
	if _, err := p.Run(); err != nil {
		panic(err)
	}
}

Because we're using tea.WithAltScreen(), the tea.Printf instructions are no-op and are not been print at all. Because these are used for debugging, the user may believe that some other feature is not working while it's actually just the debugging logs not being print due to AltScreen.

Before releasing v2 for real, we may consider adding some errors or warning somehow if tea.Printf is used together with tea.WithAltScreen(). Even a panic can be considered, but perhaps that's a bit too drastic?

Below, I'll paste the code paths until a return is reached, preventing the print:

bubbletea/tea.go

Lines 739 to 740 in 7858a14

case printLineMessage:
p.renderer.insertAbove(msg.messageBody)

s.scr.InsertAbove(lines)

https://github.com/charmbracelet/x/blob/92fd130481998d7760473ae5b14c1a9bc90b9df3/cellbuf/screen.go#L1449-L1451

/cc @aymanbagabas

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions