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

screen.Clear() sets background color to last used style in v2.5.0 #522

Closed
wedaly opened this issue Apr 5, 2022 · 4 comments
Closed

screen.Clear() sets background color to last used style in v2.5.0 #522

wedaly opened this issue Apr 5, 2022 · 4 comments

Comments

@wedaly
Copy link
Contributor

wedaly commented Apr 5, 2022

In tcell v2.4.0, the following program clears the screen back to the default background color. In tcell v2.5.0, the program clears the screen to the last used style in screen.SetContent()

Here's a program that reproduces the issue:

package main

import (
	"time"
	"github.com/gdamore/tcell/v2"
)

func main() {
	s, err := tcell.NewScreen()
	if err != nil {
		panic(err)
	}

	if err := s.Init(); err != nil {
		panic(err)
	}

	s.Clear()
	s.Show()

	style := tcell.StyleDefault.
		Background(tcell.ColorMaroon).
		Foreground(tcell.ColorWhite).Bold(true)

	s.SetContent(0, 0, 'a', nil, style)
	s.SetContent(1, 0, 'b', nil, style)
	s.SetContent(2, 0, 'c', nil, style)
	s.Show()

	time.Sleep(time.Second)

	// In tcell v2.4.0, this clears the screen back to its default color.
	// In tcell v2.5.0, this clears the screen to maroon.
	s.Clear()
	s.Show()

	time.Sleep(time.Second * 5)

	s.Fini()
}

I was able to reproduce the issue in:

  • Fedora 35, with gnome-terminal (both with and without tmux)
  • macOS, with Terminal.app

Git bisecting shows 62f5502 as the commit that introduced the change in behavior.

Is this a regression? Or am I using SetContent / Clear / Show incorrectly?

(Thanks again for maintaining this project. I've been a happy user for several years now!)

@wedaly
Copy link
Contributor Author

wedaly commented Apr 5, 2022

Stepping through the program with a debugger, I see that when tScreen.clearScreen() calls t.sendFgBg(fg, bg) with fg and bg set to ColorDefault, sendFgBg doesn't actually send anything (all the if statements evaluate false).

Maybe something like this would help?

diff --git a/tscreen.go b/tscreen.go
index 2c161cc..d7df641 100644
--- a/tscreen.go
+++ b/tscreen.go
@@ -859,6 +859,12 @@ func (t *tScreen) Show() {
 
 func (t *tScreen) clearScreen() {
        fg, bg, _ := t.style.Decompose()
+       if fg == ColorDefault {
+               fg = ColorReset
+       }
+       if bg == ColorDefault {
+               bg = ColorReset
+       }
        t.sendFgBg(fg, bg)
        t.TPuts(t.ti.Clear)
        t.clear = false

@wedaly
Copy link
Contributor Author

wedaly commented Apr 5, 2022

This also seems to restore the old behavior:

diff --git a/tscreen.go b/tscreen.go
index 2ed73a2..676084e 100644
--- a/tscreen.go
+++ b/tscreen.go
@@ -859,6 +859,7 @@ func (t *tScreen) Show() {
 
 func (t *tScreen) clearScreen() {
        fg, bg, _ := t.style.Decompose()
+       t.TPuts(t.ti.AttrOff)
        t.sendFgBg(fg, bg)
        t.TPuts(t.ti.Clear)
        t.clear = false

I think ti.AttrOff used to get set by drawCell after ClearScreen filled the cells, but this stopped happening when the dirty flagged started getting set to false in 62f5502

wedaly added a commit to aretext/aretext that referenced this issue Apr 8, 2022
Use the private fork aretext/tcell that contains a fix
for clearing the screen.

Related to gdamore/tcell#522
@tobiassjosten
Copy link
Contributor

Well dug, @wedaly! I'm seeing this exact same problem and have resorted to using Sync() after Clear() to bypass it for now.

@gdamore
Copy link
Owner

gdamore commented Apr 16, 2022

This is fixed in #523

I'll create a patch release today to get this out.

@gdamore gdamore closed this as completed Apr 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants