bubbletea icon indicating copy to clipboard operation
bubbletea copied to clipboard

Exiting fullscreen glitch when also removing a line in output

Open applejag opened this issue 9 months ago • 2 comments

Describe the bug When conditionally rendering a line if you're full screen, then when you exit full screen it clears too many lines and removes terminal output that isn't part of

Setup Please complete the following information along with version numbers, if applicable.

  • OS: Linux, NixOS
  • Shell: tested and getting the same glitch in bash, zsh, fish, and pwsh
  • Terminal Emulator: only tested alacritty, kitty, and iterm
  • Terminal Multiplexer: tested both with and without tmux

To Reproduce Steps to reproduce the behavior:

  1. Have some history in your terminal
  2. go run .
  3. click f to enter fullscreen and then again to exit fullscreen (doesn't have to be double clicked quickly. The glitch is not time sensitive at all actually)
  4. repeat step 3 and see how it clears 1 line at a time from your terminal history

Source Code

package main

import (
	"fmt"
	"os"

	tea "github.com/charmbracelet/bubbletea"
)

func main() {
	p := tea.NewProgram(model{})
	if _, err := p.Run(); err != nil {
		fmt.Printf("Alas, there's been an error: %v", err)
		os.Exit(1)
	}
}

type model struct {
	isFullscreen bool
}

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() {

		// These keys should exit the program.
		case "ctrl+c", "q":
			return m, tea.Quit

		// Toggle fullscreen
		case "f":
			m.isFullscreen = !m.isFullscreen
			if m.isFullscreen {
                return m, tea.EnterAltScreen
            } else {
                return m, tea.ExitAltScreen
			}
		}
	}
	return m, nil
}

func (m model) View() string {
	s := "Hello\n"

    if m.isFullscreen {
        s += "yay we're fullscreen\n"
    }

	s += "\nPress q to quit.\n"

	return s
}

Expected behavior When exiting full screen mode, tea renders the view first before trying to figure out how many lines to clear.

Screenshots When first just running the program:

image

After clicking f to enter fullscreen:

image

Then clicking f again to exit:

image

Doing that again, clicking f twice, results in another line removed:

image

Double clicking again:

image

And again:

image

Additional context As a more real-life example, in my real program I have all the normal output, but then conditionally a status bar at the end.

When toggling fullscreen it will add a status line to my program saying "forcing fullscreen". Then if there are no other status messages, when I get out of full screen then the status line should also disappear, leading to the bug/glitch explained in this issue.

applejag avatar May 10 '24 16:05 applejag

Additionally, reversing the condition leaves more lines in the terminal after toggling the full screen. For example:

if !m.isFullscreen {
        s += "yay we're fullscreen\n"
}

keeps leftover lines in the terminal

semihbkgr avatar May 15 '24 20:05 semihbkgr

Oof, wow, this is definitely a bug. Appreciate the report—we'll look into this.

meowgorithm avatar May 15 '24 20:05 meowgorithm