1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-12-12 11:15:00 +02:00
lazygit/pkg/gui/menu_panel.go

103 lines
2.7 KiB
Go
Raw Normal View History

2018-08-28 20:13:01 +02:00
package gui
import (
"fmt"
"github.com/jesseduffield/gocui"
2018-09-04 15:25:02 +02:00
"github.com/jesseduffield/lazygit/pkg/utils"
2018-08-28 20:13:01 +02:00
)
// list panel functions
2018-09-05 11:12:11 +02:00
func (gui *Gui) handleMenuSelect(g *gocui.Gui, v *gocui.View) error {
return gui.focusPoint(0, gui.State.Panels.Menu.SelectedLine, gui.State.MenuItemCount, v)
}
func (gui *Gui) handleMenuNextLine(g *gocui.Gui, v *gocui.View) error {
panelState := gui.State.Panels.Menu
gui.changeSelectedLine(&panelState.SelectedLine, v.LinesHeight(), false)
return gui.handleMenuSelect(g, v)
}
func (gui *Gui) handleMenuPrevLine(g *gocui.Gui, v *gocui.View) error {
panelState := gui.State.Panels.Menu
gui.changeSelectedLine(&panelState.SelectedLine, v.LinesHeight(), true)
return gui.handleMenuSelect(g, v)
2018-08-28 20:13:01 +02:00
}
// specific functions
2018-12-07 09:52:31 +02:00
func (gui *Gui) renderMenuOptions() error {
optionsMap := map[string]string{
"esc/q": gui.Tr.SLocalize("close"),
"↑ ↓": gui.Tr.SLocalize("navigate"),
"space": gui.Tr.SLocalize("execute"),
2018-08-28 20:13:01 +02:00
}
2018-12-07 09:52:31 +02:00
return gui.renderOptionsMap(optionsMap)
2018-08-28 20:13:01 +02:00
}
2018-09-05 11:12:11 +02:00
func (gui *Gui) handleMenuClose(g *gocui.Gui, v *gocui.View) error {
for _, key := range []gocui.Key{gocui.KeySpace, gocui.KeyEnter} {
if err := g.DeleteKeybinding("menu", key, gocui.ModNone); err != nil {
return err
}
2018-09-18 13:07:25 +02:00
}
2018-09-05 11:12:11 +02:00
err := g.DeleteView("menu")
if err != nil {
return err
}
return gui.returnFocus(g, v)
2018-08-28 20:13:01 +02:00
}
func (gui *Gui) createMenu(title string, items interface{}, itemCount int, handlePress func(int) error) error {
isFocused := gui.g.CurrentView().Name() == "menu"
gui.State.MenuItemCount = itemCount
list, err := utils.RenderList(items, isFocused)
if err != nil {
return err
}
x0, y0, x1, y1 := gui.getConfirmationPanelDimensions(gui.g, false, list)
2018-09-18 13:07:25 +02:00
menuView, _ := gui.g.SetView("menu", x0, y0, x1, y1, 0)
2018-12-08 07:54:54 +02:00
menuView.Title = title
2018-09-05 11:12:11 +02:00
menuView.FgColor = gocui.ColorWhite
menuView.Clear()
fmt.Fprint(menuView, list)
gui.State.Panels.Menu.SelectedLine = 0
2018-09-18 13:07:25 +02:00
wrappedHandlePress := func(g *gocui.Gui, v *gocui.View) error {
selectedLine := gui.State.Panels.Menu.SelectedLine
2018-12-08 07:54:54 +02:00
if err := handlePress(selectedLine); err != nil {
2019-06-08 12:29:25 +02:00
return err
2018-12-08 07:54:54 +02:00
}
if _, err := gui.g.View("menu"); err == nil {
if _, err := gui.g.SetViewOnBottom("menu"); err != nil {
return err
}
2018-12-08 07:54:54 +02:00
}
2018-12-08 07:54:54 +02:00
return gui.returnFocus(gui.g, menuView)
2018-09-18 13:07:25 +02:00
}
for _, key := range []gocui.Key{gocui.KeySpace, gocui.KeyEnter} {
_ = gui.g.DeleteKeybinding("menu", key, gocui.ModNone)
if err := gui.g.SetKeybinding("menu", key, gocui.ModNone, wrappedHandlePress); err != nil {
return err
}
}
2018-09-18 13:07:25 +02:00
gui.g.Update(func(g *gocui.Gui) error {
if _, err := gui.g.View("menu"); err == nil {
if _, err := g.SetViewOnTop("menu"); err != nil {
return err
}
2018-08-29 14:24:04 +02:00
}
2018-09-18 13:07:25 +02:00
currentView := gui.g.CurrentView()
return gui.switchFocus(gui.g, currentView, menuView)
2018-08-28 20:13:01 +02:00
})
return nil
2018-08-29 11:56:28 +02:00
}