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

105 lines
2.5 KiB
Go
Raw Normal View History

2018-08-28 20:13:01 +02:00
package gui
import (
"fmt"
2020-08-20 00:24:35 +02:00
"strings"
"github.com/jesseduffield/gocui"
"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 {
2020-02-14 13:32:31 +02:00
displayString string
2020-02-14 13:07:56 +02:00
displayStrings []string
onPress func() error
}
2020-08-20 00:24:35 +02:00
// every item in a list context needs an ID
func (i *menuItem) ID() string {
if i.displayString != "" {
return i.displayString
}
return strings.Join(i.displayStrings, "-")
}
// list panel functions
2020-08-15 08:53:12 +02:00
func (gui *Gui) handleMenuSelect() error {
2020-03-09 02:34:10 +02:00
return nil
}
// specific functions
func (gui *Gui) getMenuOptions() map[string]string {
return map[string]string{
gui.getKeyDisplay("universal.return"): gui.Tr.SLocalize("close"),
2020-01-07 23:26:29 +02:00
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-09-05 11:12:11 +02:00
func (gui *Gui) handleMenuClose(g *gocui.Gui, v *gocui.View) error {
err := g.DeleteView("menu")
if err != nil {
return err
}
2020-08-16 05:58:29 +02:00
return gui.returnFromContext()
2018-08-28 20:13:01 +02:00
}
2020-02-14 13:07:56 +02:00
type createMenuOptions struct {
showCancel bool
}
2020-02-14 14:39:02 +02:00
func (gui *Gui) createMenu(title string, items []*menuItem, createMenuOptions createMenuOptions) error {
2020-02-14 13:07:56 +02:00
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
},
})
}
2020-08-20 00:24:35 +02:00
gui.State.MenuItems = items
2020-02-14 13:07:56 +02:00
stringArrays := make([][]string, len(items))
for i, item := range items {
2020-02-14 13:32:31 +02:00
if item.displayStrings == nil {
stringArrays[i] = []string{item.displayString}
} else {
stringArrays[i] = item.displayStrings
}
2020-02-14 13:07:56 +02:00
}
list := utils.RenderDisplayStrings(stringArrays)
2020-08-15 09:23:16 +02:00
x0, y0, x1, y1 := gui.getConfirmationPanelDimensions(false, list)
2020-02-14 13:07:56 +02:00
menuView, _ := gui.g.SetView("menu", x0, y0, x1, y1, 0)
menuView.Title = title
menuView.FgColor = theme.GocuiDefaultTextColor
2020-02-24 22:17:49 +02:00
menuView.ContainsList = true
2020-02-14 13:07:56 +02:00
menuView.Clear()
2020-04-21 11:25:05 +02:00
menuView.SetOnSelectItem(gui.onSelectItemWrapper(func(selectedLine int) error {
return nil
}))
2020-02-14 13:07:56 +02:00
fmt.Fprint(menuView, list)
2020-08-20 00:53:10 +02:00
gui.State.Panels.Menu.SelectedLineIdx = 0
2020-02-14 13:07:56 +02:00
gui.g.Update(func(g *gocui.Gui) error {
2020-08-16 05:58:29 +02:00
return gui.switchContext(gui.Contexts.Menu.Context)
2020-02-14 13:07:56 +02:00
})
return nil
}
2020-08-20 00:24:35 +02:00
func (gui *Gui) onMenuPress() error {
2020-08-20 00:53:10 +02:00
selectedLine := gui.State.Panels.Menu.SelectedLineIdx
2020-08-20 00:24:35 +02:00
if err := gui.State.MenuItems[selectedLine].onPress(); err != nil {
return err
}
return gui.returnFromContext()
}