2018-08-28 20:13:01 +02:00
|
|
|
package gui
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
2018-08-29 13:43:59 +02:00
|
|
|
|
|
|
|
"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
|
|
|
)
|
|
|
|
|
2018-09-05 11:12:11 +02:00
|
|
|
func (gui *Gui) handleMenuSelect(g *gocui.Gui, v *gocui.View) error {
|
2018-08-29 13:43:59 +02:00
|
|
|
// doing nothing for now
|
|
|
|
// but it is needed for switch in newLineFocused
|
2018-08-28 20:13:01 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-09-05 11:12:11 +02:00
|
|
|
func (gui *Gui) renderMenuOptions(g *gocui.Gui) error {
|
2018-08-29 13:43:59 +02:00
|
|
|
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-08-29 13:43:59 +02:00
|
|
|
return gui.renderOptionsMap(g, 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 {
|
2018-09-18 13:07:25 +02:00
|
|
|
if err := g.DeleteKeybinding("menu", gocui.KeySpace, gocui.ModNone); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-09-05 11:12:11 +02:00
|
|
|
err := g.DeleteView("menu")
|
2018-08-29 13:43:59 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return gui.returnFocus(g, v)
|
2018-08-28 20:13:01 +02:00
|
|
|
}
|
|
|
|
|
2018-09-18 13:07:25 +02:00
|
|
|
func (gui *Gui) createMenu(items interface{}, handlePress func(int) error) error {
|
|
|
|
list, err := utils.RenderList(items)
|
2018-09-17 13:02:30 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-09-18 13:07:25 +02:00
|
|
|
x0, y0, x1, y1 := gui.getConfirmationPanelDimensions(gui.g, list)
|
|
|
|
menuView, _ := gui.g.SetView("menu", x0, y0, x1, y1, 0)
|
2018-09-05 11:12:11 +02:00
|
|
|
menuView.Title = strings.Title(gui.Tr.SLocalize("menu"))
|
|
|
|
menuView.FgColor = gocui.ColorWhite
|
2018-09-17 13:02:30 +02:00
|
|
|
menuView.Clear()
|
|
|
|
fmt.Fprint(menuView, list)
|
2018-08-30 13:39:11 +02:00
|
|
|
|
2018-09-18 13:07:25 +02:00
|
|
|
if err := gui.renderMenuOptions(gui.g); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
wrappedHandlePress := func(g *gocui.Gui, v *gocui.View) error {
|
|
|
|
lineNumber := gui.getItemPosition(v)
|
|
|
|
return handlePress(lineNumber)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := gui.g.SetKeybinding("menu", gocui.KeySpace, gocui.ModNone, wrappedHandlePress); err != nil {
|
2018-08-30 13:39:11 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-09-18 13:07:25 +02:00
|
|
|
gui.g.Update(func(g *gocui.Gui) error {
|
|
|
|
if _, err := g.SetViewOnTop("menu"); err != nil {
|
2018-08-29 14:24:04 +02:00
|
|
|
return err
|
|
|
|
}
|
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
|
|
|
}
|