2018-08-28 20:13:01 +02:00
|
|
|
package gui
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-08-29 13:43:59 +02:00
|
|
|
|
|
|
|
"github.com/jesseduffield/gocui"
|
2019-10-18 09:48:37 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/theme"
|
2018-09-04 15:25:02 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/utils"
|
2018-08-28 20:13:01 +02:00
|
|
|
)
|
|
|
|
|
2020-02-14 13:07:56 +02:00
|
|
|
type menuItem struct {
|
|
|
|
displayStrings []string
|
|
|
|
onPress func() error
|
|
|
|
}
|
|
|
|
|
2018-12-06 13:18:17 +02:00
|
|
|
// list panel functions
|
|
|
|
|
2018-09-05 11:12:11 +02:00
|
|
|
func (gui *Gui) handleMenuSelect(g *gocui.Gui, v *gocui.View) error {
|
2019-03-23 02:42:19 +02:00
|
|
|
return gui.focusPoint(0, gui.State.Panels.Menu.SelectedLine, gui.State.MenuItemCount, v)
|
2018-12-06 13:18:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// specific functions
|
|
|
|
|
2018-12-07 09:52:31 +02:00
|
|
|
func (gui *Gui) renderMenuOptions() error {
|
2018-08-29 13:43:59 +02:00
|
|
|
optionsMap := map[string]string{
|
2020-01-07 23:26:29 +02:00
|
|
|
fmt.Sprintf("%s/%s", gui.getKeyDisplay("universal.return"), gui.getKeyDisplay("universal.quit")): gui.Tr.SLocalize("close"),
|
|
|
|
fmt.Sprintf("%s %s", gui.getKeyDisplay("universal.prevItem"), gui.getKeyDisplay("universal.nextItem")): gui.Tr.SLocalize("navigate"),
|
|
|
|
gui.getKeyDisplay("universal.select"): 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 {
|
2019-03-03 14:18:28 +02:00
|
|
|
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")
|
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
|
|
|
}
|
|
|
|
|
2020-02-14 13:07:56 +02:00
|
|
|
type createMenuOptions struct {
|
|
|
|
showCancel bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (gui *Gui) createMenuNew(title string, items []*menuItem, createMenuOptions createMenuOptions) error {
|
|
|
|
if createMenuOptions.showCancel {
|
|
|
|
// this is mutative but I'm okay with that for now
|
|
|
|
items = append(items, &menuItem{
|
|
|
|
displayStrings: []string{gui.Tr.SLocalize("cancel")},
|
|
|
|
onPress: func() error {
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
gui.State.MenuItemCount = len(items)
|
|
|
|
|
|
|
|
stringArrays := make([][]string, len(items))
|
|
|
|
for i, item := range items {
|
|
|
|
stringArrays[i] = item.displayStrings
|
|
|
|
}
|
|
|
|
|
|
|
|
list := utils.RenderDisplayStrings(stringArrays)
|
|
|
|
|
|
|
|
x0, y0, x1, y1 := gui.getConfirmationPanelDimensions(gui.g, false, list)
|
|
|
|
menuView, _ := gui.g.SetView("menu", x0, y0, x1, y1, 0)
|
|
|
|
menuView.Title = title
|
|
|
|
menuView.FgColor = theme.GocuiDefaultTextColor
|
|
|
|
menuView.Clear()
|
|
|
|
fmt.Fprint(menuView, list)
|
|
|
|
gui.State.Panels.Menu.SelectedLine = 0
|
|
|
|
|
|
|
|
wrappedHandlePress := func(g *gocui.Gui, v *gocui.View) error {
|
|
|
|
selectedLine := gui.State.Panels.Menu.SelectedLine
|
|
|
|
if err := items[selectedLine].onPress(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := gui.g.View("menu"); err == nil {
|
|
|
|
if _, err := gui.g.SetViewOnBottom("menu"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return gui.returnFocus(gui.g, menuView)
|
|
|
|
}
|
|
|
|
|
|
|
|
gui.State.Panels.Menu.OnPress = wrappedHandlePress
|
|
|
|
|
|
|
|
for _, key := range []gocui.Key{gocui.KeySpace, gocui.KeyEnter, 'y'} {
|
|
|
|
_ = gui.g.DeleteKeybinding("menu", key, gocui.ModNone)
|
|
|
|
|
|
|
|
if err := gui.g.SetKeybinding("menu", nil, key, gocui.ModNone, wrappedHandlePress); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
currentView := gui.g.CurrentView()
|
|
|
|
return gui.switchFocus(gui.g, currentView, menuView)
|
|
|
|
})
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-03-23 02:42:19 +02:00
|
|
|
func (gui *Gui) createMenu(title string, items interface{}, itemCount int, handlePress func(int) error) error {
|
2019-02-16 06:17:44 +02:00
|
|
|
isFocused := gui.g.CurrentView().Name() == "menu"
|
2019-03-23 02:42:19 +02:00
|
|
|
gui.State.MenuItemCount = itemCount
|
2019-02-16 06:17:44 +02:00
|
|
|
list, err := utils.RenderList(items, isFocused)
|
2018-09-17 13:02:30 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-01-15 10:33:42 +02:00
|
|
|
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
|
2019-10-18 09:48:37 +02:00
|
|
|
menuView.FgColor = theme.GocuiDefaultTextColor
|
2018-09-17 13:02:30 +02:00
|
|
|
menuView.Clear()
|
|
|
|
fmt.Fprint(menuView, list)
|
2018-12-06 13:18:17 +02:00
|
|
|
gui.State.Panels.Menu.SelectedLine = 0
|
2018-08-30 13:39:11 +02:00
|
|
|
|
2018-09-18 13:07:25 +02:00
|
|
|
wrappedHandlePress := func(g *gocui.Gui, v *gocui.View) error {
|
2018-12-06 13:18:17 +02:00
|
|
|
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
|
|
|
}
|
2019-02-16 03:07:27 +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
|
|
|
}
|
2019-02-16 03:07:27 +02:00
|
|
|
|
2018-12-08 07:54:54 +02:00
|
|
|
return gui.returnFocus(gui.g, menuView)
|
2018-09-18 13:07:25 +02:00
|
|
|
}
|
|
|
|
|
2019-11-10 07:20:35 +02:00
|
|
|
gui.State.Panels.Menu.OnPress = wrappedHandlePress
|
|
|
|
|
|
|
|
for _, key := range []gocui.Key{gocui.KeySpace, gocui.KeyEnter, 'y'} {
|
2019-03-23 04:15:29 +02:00
|
|
|
_ = gui.g.DeleteKeybinding("menu", key, gocui.ModNone)
|
|
|
|
|
2019-11-16 03:41:04 +02:00
|
|
|
if err := gui.g.SetKeybinding("menu", nil, key, gocui.ModNone, wrappedHandlePress); err != nil {
|
2019-03-03 14:18:28 +02:00
|
|
|
return err
|
|
|
|
}
|
2018-08-30 13:39:11 +02:00
|
|
|
}
|
|
|
|
|
2018-09-18 13:07:25 +02:00
|
|
|
gui.g.Update(func(g *gocui.Gui) error {
|
2019-02-16 03:07:27 +02:00
|
|
|
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
|
|
|
}
|