1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-02-09 13:47:11 +02:00

get tcell to cleanup the terminal if we panic

This commit is contained in:
Jesse Duffield 2021-02-09 22:19:49 +11:00
parent 6ad1409c33
commit ae6adac95c
3 changed files with 19 additions and 18 deletions

View File

@ -14,6 +14,7 @@ import (
"time"
"github.com/fatih/color"
"github.com/jesseduffield/gocui"
)
// SplitLines takes a multiline string and splits it on newlines
@ -366,9 +367,8 @@ func ResolveTemplate(templateStr string, object interface{}) (string, error) {
func Safe(f func()) {
panicking := true
defer func() {
if panicking {
// TODO: close tcell
// termbox.Close()
if panicking && gocui.Screen != nil {
gocui.Screen.Fini()
}
}()

View File

@ -158,7 +158,7 @@ func NewGui(mode OutputMode, supportOverlaps bool, recordEvents bool) (*Gui, err
return nil, err
}
} else {
g.maxX, g.maxY = screen.Size()
g.maxX, g.maxY = Screen.Size()
}
g.BgColor, g.FgColor, g.FrameColor = ColorDefault, ColorDefault, ColorDefault
@ -184,7 +184,7 @@ func (g *Gui) Close() {
go func() {
g.stop <- struct{}{}
}()
screen.Fini()
Screen.Fini()
}
// Size returns the terminal's size.
@ -210,7 +210,7 @@ func (g *Gui) Rune(x, y int) (rune, error) {
if x < 0 || y < 0 || x >= g.maxX || y >= g.maxY {
return ' ', errors.New("invalid point")
}
c, _, _, _ := screen.GetContent(x, y)
c, _, _, _ := Screen.GetContent(x, y)
return c, nil
}
@ -536,7 +536,7 @@ func (g *Gui) MainLoop() error {
}()
if g.Mouse {
screen.EnableMouse()
Screen.EnableMouse()
}
if err := g.flush(); err != nil {
@ -608,7 +608,7 @@ func (g *Gui) handleEvent(ev *GocuiEvent) error {
func (g *Gui) flush() error {
g.clear(g.FgColor, g.BgColor)
maxX, maxY := screen.Size()
maxX, maxY := Screen.Size()
// if GUI's size has changed, we need to redraw all views
if maxX != g.maxX || maxY != g.maxY {
for _, v := range g.views {
@ -672,16 +672,16 @@ func (g *Gui) flush() error {
return err
}
}
screen.Show()
Screen.Show()
return nil
}
func (g *Gui) clear(fg, bg Attribute) (int, int) {
st := getTcellStyle(fg, bg, g.outputMode)
w, h := screen.Size()
w, h := Screen.Size()
for row := 0; row < h; row++ {
for col := 0; col < w; col++ {
screen.SetContent(col, row, ' ', nil, st)
Screen.SetContent(col, row, ' ', nil, st)
}
}
return w, h
@ -969,13 +969,13 @@ func (g *Gui) draw(v *View) error {
// tcell is hiding cursor by setting coordinates outside of screen.
// Keeping it here for now, as I'm not 100% sure :)
if cx >= 0 && cx < gMaxX && cy >= 0 && cy < gMaxY {
screen.ShowCursor(cx, cy)
Screen.ShowCursor(cx, cy)
} else {
screen.HideCursor()
Screen.HideCursor()
}
}
} else {
screen.HideCursor()
Screen.HideCursor()
}
v.clearRunes()

View File

@ -8,7 +8,8 @@ import (
"github.com/gdamore/tcell/v2"
)
var screen tcell.Screen
// We probably don't want this being a global variable for YOLO for now
var Screen tcell.Screen
// tcellInit initializes tcell screen for use.
func tcellInit() error {
@ -17,7 +18,7 @@ func tcellInit() error {
} else if e = s.Init(); e != nil {
return e
} else {
screen = s
Screen = s
return nil
}
}
@ -26,7 +27,7 @@ func tcellInit() error {
// content (rune) and attributes using provided OutputMode
func tcellSetCell(x, y int, ch rune, fg, bg Attribute, omode OutputMode) {
st := getTcellStyle(fg, bg, omode)
screen.SetContent(x, y, ch, nil, st)
Screen.SetContent(x, y, ch, nil, st)
}
// getTcellStyle creates tcell.Style from Attributes
@ -120,7 +121,7 @@ var (
// pollEvent get tcell.Event and transform it into gocuiEvent
func pollEvent() GocuiEvent {
tev := screen.PollEvent()
tev := Screen.PollEvent()
switch tev := tev.(type) {
case *tcell.EventInterrupt:
return GocuiEvent{Type: eventInterrupt}