1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-07-28 16:02:01 +03: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 1fb2317bac
commit ecc5fe24a9
3 changed files with 19 additions and 18 deletions

View File

@ -14,6 +14,7 @@ import (
"time" "time"
"github.com/fatih/color" "github.com/fatih/color"
"github.com/jesseduffield/gocui"
) )
// SplitLines takes a multiline string and splits it on newlines // 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()) { func Safe(f func()) {
panicking := true panicking := true
defer func() { defer func() {
if panicking { if panicking && gocui.Screen != nil {
// TODO: close tcell gocui.Screen.Fini()
// termbox.Close()
} }
}() }()

View File

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

View File

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