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

79 lines
2.2 KiB
Go
Raw Normal View History

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"
2022-01-29 10:09:20 +02:00
"github.com/jesseduffield/lazygit/pkg/gui/types"
"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
)
func (gui *Gui) getMenuOptions() map[string]string {
keybindingConfig := gui.c.UserConfig.Keybinding
2020-10-03 06:54:55 +02:00
return map[string]string{
gui.getKeyDisplay(keybindingConfig.Universal.Return): gui.c.Tr.LcClose,
fmt.Sprintf("%s %s", gui.getKeyDisplay(keybindingConfig.Universal.PrevItem), gui.getKeyDisplay(keybindingConfig.Universal.NextItem)): gui.c.Tr.LcNavigate,
gui.getKeyDisplay(keybindingConfig.Universal.Select): gui.c.Tr.LcExecute,
2018-08-28 20:13:01 +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
}
2022-01-28 11:44:36 +02:00
// note: items option is mutated by this function
2022-01-29 10:09:20 +02:00
func (gui *Gui) createMenu(opts types.CreateMenuOptions) error {
2022-01-28 11:44:36 +02:00
if !opts.HideCancel {
2020-02-14 13:07:56 +02:00
// this is mutative but I'm okay with that for now
2022-01-29 10:09:20 +02:00
opts.Items = append(opts.Items, &types.MenuItem{
DisplayStrings: []string{gui.c.Tr.LcCancel},
2022-01-28 11:44:36 +02:00
OnPress: func() error {
2020-02-14 13:07:56 +02:00
return nil
},
})
}
2022-01-28 11:44:36 +02:00
gui.State.MenuItems = opts.Items
2020-02-14 13:07:56 +02:00
2022-01-28 11:44:36 +02:00
stringArrays := make([][]string, len(opts.Items))
for i, item := range opts.Items {
if item.OpensMenu && item.DisplayStrings != nil {
2021-11-02 12:16:00 +02:00
return errors.New("Message for the developer of this app: you've set opensMenu with displaystrings on the menu panel. Bad developer!. Apologies, user")
}
2022-01-28 11:44:36 +02:00
if item.DisplayStrings == nil {
styledStr := item.DisplayString
if item.OpensMenu {
2021-11-02 12:16:00 +02:00
styledStr = opensMenuStyle(styledStr)
}
stringArrays[i] = []string{styledStr}
2020-02-14 13:32:31 +02:00
} else {
2022-01-28 11:44:36 +02:00
stringArrays[i] = item.DisplayStrings
2020-02-14 13:32:31 +02:00
}
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)
2022-01-28 11:44:36 +02:00
menuView.Title = opts.Title
2020-02-14 13:07:56 +02:00
menuView.FgColor = theme.GocuiDefaultTextColor
2020-04-21 11:25:05 +02:00
menuView.SetOnSelectItem(gui.onSelectItemWrapper(func(selectedLine int) error {
return nil
}))
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
return gui.c.PushContext(gui.State.Contexts.Menu)
2020-02-14 13:07:56 +02:00
}
2020-08-20 00:24:35 +02:00
2022-01-29 10:09:20 +02:00
func (gui *Gui) getSelectedMenuItem() *types.MenuItem {
if len(gui.State.MenuItems) == 0 {
return nil
2020-08-20 00:24:35 +02:00
}
return gui.State.MenuItems[gui.State.Panels.Menu.SelectedLineIdx]
2020-08-20 00:24:35 +02:00
}