1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-07-05 00:59:19 +02:00

more efficient gocui

This commit is contained in:
Jesse Duffield
2021-04-02 21:14:09 +11:00
parent 69e9f6d29d
commit 5289d49f75
5 changed files with 33 additions and 20 deletions

View File

@ -533,10 +533,6 @@ func (g *Gui) SetManagerFunc(manager func(*Gui) error) {
// MainLoop runs the main loop until an error is returned. A successful
// finish should return ErrQuit.
func (g *Gui) MainLoop() error {
if err := g.flush(); err != nil {
return err
}
go func() {
for {
select {
@ -552,9 +548,6 @@ func (g *Gui) MainLoop() error {
Screen.EnableMouse()
}
if err := g.flush(); err != nil {
return err
}
for {
select {
case ev := <-g.gEvents:
@ -619,7 +612,8 @@ func (g *Gui) handleEvent(ev *GocuiEvent) error {
// flush updates the gui, re-drawing frames and buffers.
func (g *Gui) flush() error {
g.clear(g.FgColor, g.BgColor)
// pretty sure we don't need this, but keeping it here in case we get weird visual artifacts
// g.clear(g.FgColor, g.BgColor)
maxX, maxY := Screen.Size()
// if GUI's size has changed, we need to redraw all views
@ -690,7 +684,7 @@ func (g *Gui) flush() error {
}
func (g *Gui) clear(fg, bg Attribute) (int, int) {
st := getTcellStyle(fg, bg, g.outputMode)
st := getTcellStyle(oldStyle{fg: fg, bg: bg, outputMode: g.outputMode})
w, h := Screen.Size()
for row := 0; row < h; row++ {
for col := 0; col < w; col++ {

View File

@ -11,6 +11,17 @@ import (
// We probably don't want this being a global variable for YOLO for now
var Screen tcell.Screen
// oldStyle is a representation of how a cell would be styled when we were using termbox
type oldStyle struct {
fg Attribute
bg Attribute
outputMode OutputMode
}
// we're using this cache to speed up rendering, because obtaining the tcell style
// from the old style is deterministic
var cellStyleCache map[oldStyle]tcell.Style = map[oldStyle]tcell.Style{}
// tcellInit initializes tcell screen for use.
func tcellInit() error {
if s, e := tcell.NewScreen(); e != nil {
@ -25,25 +36,31 @@ func tcellInit() error {
// tcellSetCell sets the character cell at a given location to the given
// content (rune) and attributes using provided OutputMode
func tcellSetCell(x, y int, ch rune, fg, bg Attribute, omode OutputMode) {
st := getTcellStyle(fg, bg, omode)
func tcellSetCell(x, y int, ch rune, fg, bg Attribute, outputMode OutputMode) {
st := getTcellStyle(oldStyle{fg: fg, bg: bg, outputMode: outputMode})
Screen.SetContent(x, y, ch, nil, st)
}
// getTcellStyle creates tcell.Style from Attributes
func getTcellStyle(fg, bg Attribute, omode OutputMode) tcell.Style {
func getTcellStyle(input oldStyle) tcell.Style {
if cachedResult, ok := cellStyleCache[input]; ok {
return cachedResult
}
st := tcell.StyleDefault
// extract colors and attributes
if fg != ColorDefault {
st = st.Foreground(getTcellColor(fg, omode))
st = setTcellFontEffectStyle(st, fg)
if input.fg != ColorDefault {
st = st.Foreground(getTcellColor(input.fg, input.outputMode))
st = setTcellFontEffectStyle(st, input.fg)
}
if bg != ColorDefault {
st = st.Background(getTcellColor(bg, omode))
st = setTcellFontEffectStyle(st, bg)
if input.bg != ColorDefault {
st = st.Background(getTcellColor(input.bg, input.outputMode))
st = setTcellFontEffectStyle(st, input.bg)
}
cellStyleCache[input] = st
return st
}