2018-09-18 13:07:25 +02:00
|
|
|
package gui
|
|
|
|
|
|
|
|
import (
|
2018-12-08 07:54:54 +02:00
|
|
|
"strings"
|
2018-09-18 13:07:25 +02:00
|
|
|
|
|
|
|
"github.com/jesseduffield/gocui"
|
2021-07-27 15:00:37 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/gui/style"
|
2019-11-16 03:41:04 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/utils"
|
2018-09-18 13:07:25 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func (gui *Gui) getBindings(v *gocui.View) []*Binding {
|
|
|
|
var (
|
|
|
|
bindingsGlobal, bindingsPanel []*Binding
|
|
|
|
)
|
|
|
|
|
2020-09-27 03:04:57 +02:00
|
|
|
bindings := append(gui.GetCustomCommandKeybindings(), gui.GetInitialKeybindings()...)
|
2018-09-18 13:07:25 +02:00
|
|
|
|
|
|
|
for _, binding := range bindings {
|
2020-01-07 19:50:25 +02:00
|
|
|
if GetKeyDisplay(binding.Key) != "" && binding.Description != "" {
|
2018-09-18 13:07:25 +02:00
|
|
|
switch binding.ViewName {
|
|
|
|
case "":
|
|
|
|
bindingsGlobal = append(bindingsGlobal, binding)
|
|
|
|
case v.Name():
|
2019-11-16 03:41:04 +02:00
|
|
|
if len(binding.Contexts) == 0 || utils.IncludesString(binding.Contexts, v.Context) {
|
|
|
|
bindingsPanel = append(bindingsPanel, binding)
|
|
|
|
}
|
2018-09-18 13:07:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// append dummy element to have a separator between
|
|
|
|
// panel and global keybindings
|
|
|
|
bindingsPanel = append(bindingsPanel, &Binding{})
|
|
|
|
return append(bindingsPanel, bindingsGlobal...)
|
|
|
|
}
|
|
|
|
|
2021-02-12 05:51:57 +02:00
|
|
|
func (gui *Gui) displayDescription(binding *Binding) string {
|
|
|
|
if binding.OpensMenu {
|
2021-11-02 12:16:00 +02:00
|
|
|
return opensMenuStyle(binding.Description)
|
2021-02-12 05:51:57 +02:00
|
|
|
}
|
|
|
|
|
2021-07-27 15:00:37 +02:00
|
|
|
return style.FgCyan.Sprint(binding.Description)
|
2021-02-12 05:51:57 +02:00
|
|
|
}
|
|
|
|
|
2021-11-02 12:16:00 +02:00
|
|
|
func opensMenuStyle(str string) string {
|
|
|
|
return style.FgMagenta.Sprintf("%s...", str)
|
|
|
|
}
|
|
|
|
|
2021-04-02 10:20:40 +02:00
|
|
|
func (gui *Gui) handleCreateOptionsMenu() error {
|
|
|
|
view := gui.g.CurrentView()
|
|
|
|
if view == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
bindings := gui.getBindings(view)
|
2018-09-18 13:07:25 +02:00
|
|
|
|
2020-02-14 13:32:31 +02:00
|
|
|
menuItems := make([]*menuItem, len(bindings))
|
|
|
|
|
|
|
|
for i, binding := range bindings {
|
2020-09-26 07:23:28 +02:00
|
|
|
binding := binding // note to self, never close over loop variables
|
2020-02-14 13:32:31 +02:00
|
|
|
menuItems[i] = &menuItem{
|
2021-02-12 05:51:57 +02:00
|
|
|
displayStrings: []string{GetKeyDisplay(binding.Key), gui.displayDescription(binding)},
|
2020-02-14 13:32:31 +02:00
|
|
|
onPress: func() error {
|
2020-09-26 07:23:28 +02:00
|
|
|
if binding.Key == nil {
|
2020-02-14 13:32:31 +02:00
|
|
|
return nil
|
|
|
|
}
|
2021-04-02 10:20:40 +02:00
|
|
|
if err := gui.handleMenuClose(); err != nil {
|
2020-02-14 13:32:31 +02:00
|
|
|
return err
|
|
|
|
}
|
2021-04-02 10:20:40 +02:00
|
|
|
return binding.Handler()
|
2020-02-14 13:32:31 +02:00
|
|
|
},
|
2018-09-18 13:07:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-04 02:00:48 +02:00
|
|
|
return gui.createMenu(strings.Title(gui.Tr.LcMenu), menuItems, createMenuOptions{})
|
2018-09-18 13:07:25 +02:00
|
|
|
}
|