2018-08-28 20:13:01 +02:00
|
|
|
package gui
|
|
|
|
|
|
|
|
import (
|
2021-11-02 12:16:00 +02:00
|
|
|
"errors"
|
2018-08-28 20:13:01 +02:00
|
|
|
"fmt"
|
2020-08-20 00:24:35 +02:00
|
|
|
"strings"
|
2018-08-29 13:43:59 +02:00
|
|
|
|
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 {
|
2020-02-14 13:32:31 +02:00
|
|
|
displayString string
|
2020-02-14 13:07:56 +02:00
|
|
|
displayStrings []string
|
|
|
|
onPress func() error
|
2021-11-02 12:16:00 +02:00
|
|
|
// only applies when displayString is used
|
|
|
|
opensMenu bool
|
2020-02-14 13:07:56 +02:00
|
|
|
}
|
|
|
|
|
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, "-")
|
|
|
|
}
|
|
|
|
|
2018-12-06 13:18:17 +02:00
|
|
|
// specific functions
|
|
|
|
|
2020-08-23 02:50:27 +02:00
|
|
|
func (gui *Gui) getMenuOptions() map[string]string {
|
2021-12-29 02:50:20 +02:00
|
|
|
keybindingConfig := gui.UserConfig.Keybinding
|
2020-10-03 06:54:55 +02:00
|
|
|
|
2020-08-23 02:50:27 +02:00
|
|
|
return map[string]string{
|
2020-10-04 02:00:48 +02:00
|
|
|
gui.getKeyDisplay(keybindingConfig.Universal.Return): gui.Tr.LcClose,
|
|
|
|
fmt.Sprintf("%s %s", gui.getKeyDisplay(keybindingConfig.Universal.PrevItem), gui.getKeyDisplay(keybindingConfig.Universal.NextItem)): gui.Tr.LcNavigate,
|
|
|
|
gui.getKeyDisplay(keybindingConfig.Universal.Select): gui.Tr.LcExecute,
|
2018-08-28 20:13:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-02 10:20:40 +02:00
|
|
|
func (gui *Gui) handleMenuClose() error {
|
2022-01-15 04:29:28 +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{
|
2020-10-04 02:00:48 +02:00
|
|
|
displayStrings: []string{gui.Tr.LcCancel},
|
2020-02-14 13:07:56 +02:00
|
|
|
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 {
|
2021-11-02 12:16:00 +02:00
|
|
|
if item.opensMenu && item.displayStrings != nil {
|
|
|
|
return errors.New("Message for the developer of this app: you've set opensMenu with displaystrings on the menu panel. Bad developer!. Apologies, user")
|
|
|
|
}
|
|
|
|
|
2020-02-14 13:32:31 +02:00
|
|
|
if item.displayStrings == nil {
|
2021-11-02 12:16:00 +02:00
|
|
|
styledStr := item.displayString
|
|
|
|
if item.opensMenu {
|
|
|
|
styledStr = opensMenuStyle(styledStr)
|
|
|
|
}
|
|
|
|
stringArrays[i] = []string{styledStr}
|
2020-02-14 13:32:31 +02:00
|
|
|
} 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-04-21 11:25:05 +02:00
|
|
|
menuView.SetOnSelectItem(gui.onSelectItemWrapper(func(selectedLine int) error {
|
|
|
|
return nil
|
|
|
|
}))
|
2021-11-01 00:35:54 +02:00
|
|
|
menuView.SetContent(list)
|
2020-08-20 00:53:10 +02:00
|
|
|
gui.State.Panels.Menu.SelectedLineIdx = 0
|
2020-02-14 13:07:56 +02:00
|
|
|
|
2022-01-15 03:04:00 +02:00
|
|
|
return gui.pushContext(gui.State.Contexts.Menu)
|
2020-02-14 13:07:56 +02:00
|
|
|
}
|
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
|
2021-04-11 05:17:20 +02:00
|
|
|
if err := gui.returnFromContext(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-08-20 00:24:35 +02:00
|
|
|
if err := gui.State.MenuItems[selectedLine].onPress(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-04-11 05:17:20 +02:00
|
|
|
return nil
|
2020-08-20 00:24:35 +02:00
|
|
|
}
|