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

style disappears #509

Closed
slukits opened this issue Jan 10, 2022 · 2 comments
Closed

style disappears #509

slukits opened this issue Jan 10, 2022 · 2 comments

Comments

@slukits
Copy link

slukits commented Jan 10, 2022

Thank you for your Work!

In the following simplified example I try to draw a colored frame at the edges of the screen area.
Clearing the screen and drawing the frame before entering the event-loop as the tutorial suggests creates the frame (the characters are displayed) but the top line and the first cell in the second line loos their style. Moving the clearing and frame drawing inside the resize event shows initially the expected behavior. But if I resize the terminal-window (gnome-terminal, vim-terminal) by making the window smaller the problem seems to reoccur for every second line the window gets smaller. Making the window larger shows always the expected behavior.

package main

import (
	"log"
	"os"

	"github.com/gdamore/tcell/v2"
)

func main() {

	defStyle := tcell.StyleDefault.Background(
		tcell.ColorBlack).Foreground(tcell.ColorWhite)
	frmStyle := tcell.StyleDefault.Background(
		tcell.ColorWhite).Foreground(tcell.ColorBlack)

	scr, err := tcell.NewScreen()
	if err != nil {
		log.Fatalf("%+v", err)
	}
	if err := scr.Init(); err != nil {
		log.Fatalf("%+v", err)
	}
	scr.SetStyle(defStyle)

	quit := func() {
		scr.Fini()
		os.Exit(0)
	}

	frame := func() {
		w, h := scr.Size()
		for x, y := 0, 0; x < w; x++ {
			scr.SetContent(x, y, 't', nil, frmStyle)
		}
		for x, y := 0, h-1; x < w; x++ {
			scr.SetContent(x, y, 'b', nil, frmStyle)
		}
		for x, y := 0, 1; y < h-1; y++ {
			scr.SetContent(x, y, 'l', nil, frmStyle)
		}
		for x, y := w-1, 1; y < h-1; y++ {
			scr.SetContent(x, y, 'r', nil, frmStyle)
		}
	}

	// BUG the first line and the first cell of the second line loose
	// their style; comment the following clearing and frame drawing out
	// and uncomment it in the resize event to see the expected behavior
	scr.Clear()
	frame()

	for {
		// Update screen
		scr.Show()

		// Poll event
		ev := scr.PollEvent()

		// Process event
		switch ev.(type) {
		case *tcell.EventResize:
			// BUG uncomment the following clearing and frame drawing
			// while the above clearing and frame drawing is comment out,
			// to see the expected initial behavior.  But resizing the
                        // terminal window by making it smaller still shows this problem.
			// scr.Clear()
			// frame()
			scr.Sync()
		case *tcell.EventKey:
			quit()
		}
	}
}
@gdamore
Copy link
Owner

gdamore commented Apr 3, 2022

So this does appear to be a subtle bug, but I'm still trying to figure it out.

A simple workaround exists however:

In your event handler, do the following:

scr.Clear()
scr.Sync()
scr.frame()

Something weird happens if you swap the scr.Sync() and scr.Clear() -- the colors of the frame are lost and it goes to the default.

scr.Clear() resets the backing colors, but those should be changed by the call to frame() above. Something doesn't make sense to me. I think it's probably the automargin code that is messing things up.

@gdamore
Copy link
Owner

gdamore commented Apr 3, 2022

Figured it out. We need to reset the style when redrawing the screen, because we can't make assumptions about the current style.

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

2 participants