1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-11-28 09:08:41 +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

2
go.mod
View File

@ -20,7 +20,7 @@ require (
github.com/imdario/mergo v0.3.11
github.com/integrii/flaggy v1.4.0
github.com/jesseduffield/go-git/v5 v5.1.2-0.20201006095850-341962be15a4
github.com/jesseduffield/gocui v0.3.1-0.20210402040718-77a1b9631715
github.com/jesseduffield/gocui v0.3.1-0.20210402113210-6fd7ef27ce76
github.com/jesseduffield/termbox-go v0.0.0-20200823212418-a2289ed6aafe // indirect
github.com/jesseduffield/yaml v2.1.0+incompatible
github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0

2
go.sum
View File

@ -94,6 +94,8 @@ github.com/jesseduffield/gocui v0.3.1-0.20210402033412-1238f910f001 h1:1WH+lTSK5
github.com/jesseduffield/gocui v0.3.1-0.20210402033412-1238f910f001/go.mod h1:QWq79xplEoyhQO+dgpk3sojjTVRKjQklyTlzm5nC5Kg=
github.com/jesseduffield/gocui v0.3.1-0.20210402040718-77a1b9631715 h1:nELTdFJiZk3vv7j8nWoHvl7H2IqTr26EHKl6LaorRA8=
github.com/jesseduffield/gocui v0.3.1-0.20210402040718-77a1b9631715/go.mod h1:QWq79xplEoyhQO+dgpk3sojjTVRKjQklyTlzm5nC5Kg=
github.com/jesseduffield/gocui v0.3.1-0.20210402113210-6fd7ef27ce76 h1:miXVlortFNTlOX+KiKW3cVxOR6+Uhl4pnQRei2X26Y4=
github.com/jesseduffield/gocui v0.3.1-0.20210402113210-6fd7ef27ce76/go.mod h1:QWq79xplEoyhQO+dgpk3sojjTVRKjQklyTlzm5nC5Kg=
github.com/jesseduffield/termbox-go v0.0.0-20200823212418-a2289ed6aafe h1:qsVhCf2RFyyKIUe/+gJblbCpXMUki9rZrHuEctg6M/E=
github.com/jesseduffield/termbox-go v0.0.0-20200823212418-a2289ed6aafe/go.mod h1:anMibpZtqNxjDbxrcDEAwSdaJ37vyUeM1f/M4uekib4=
github.com/jesseduffield/yaml v2.1.0+incompatible h1:HWQJ1gIv2zHKbDYNp0Jwjlj24K8aqpFHnMCynY1EpmE=

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
}

2
vendor/modules.txt vendored
View File

@ -149,7 +149,7 @@ github.com/jesseduffield/go-git/v5/utils/merkletrie/filesystem
github.com/jesseduffield/go-git/v5/utils/merkletrie/index
github.com/jesseduffield/go-git/v5/utils/merkletrie/internal/frame
github.com/jesseduffield/go-git/v5/utils/merkletrie/noder
# github.com/jesseduffield/gocui v0.3.1-0.20210402040718-77a1b9631715
# github.com/jesseduffield/gocui v0.3.1-0.20210402113210-6fd7ef27ce76
## explicit
github.com/jesseduffield/gocui
# github.com/jesseduffield/termbox-go v0.0.0-20200823212418-a2289ed6aafe