1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-03-19 21:28:28 +02:00
lazygit/pkg/gui/menu_panel.go

132 lines
3.4 KiB
Go
Raw Normal View History

2018-08-28 20:13:01 +02:00
package gui
import (
"fmt"
"strings"
"github.com/jesseduffield/gocui"
2018-09-04 15:25:02 +02:00
"github.com/jesseduffield/lazygit/pkg/utils"
2018-08-28 20:13:01 +02:00
)
2018-09-05 11:12:11 +02:00
func (gui *Gui) handleMenuPress(g *gocui.Gui, v *gocui.View) error {
lineNumber := gui.getItemPosition(v)
2018-09-05 13:01:21 +02:00
if gui.State.Keys[lineNumber].Key == nil {
return nil
}
2018-09-04 15:29:43 +02:00
if len(gui.State.Keys) > lineNumber {
2018-09-05 11:12:11 +02:00
err := gui.handleMenuClose(g, v)
if err != nil {
return err
}
2018-09-04 15:29:43 +02:00
return gui.State.Keys[lineNumber].Handler(g, v)
2018-08-28 20:13:01 +02:00
}
return nil
2018-08-28 20:13:01 +02:00
}
2018-09-05 11:12:11 +02:00
func (gui *Gui) handleMenuSelect(g *gocui.Gui, v *gocui.View) error {
// doing nothing for now
// but it is needed for switch in newLineFocused
2018-08-28 20:13:01 +02:00
return nil
}
2018-09-05 11:12:11 +02:00
func (gui *Gui) renderMenuOptions(g *gocui.Gui) error {
optionsMap := map[string]string{
"esc/q": gui.Tr.SLocalize("close"),
"↑ ↓": gui.Tr.SLocalize("navigate"),
"space": gui.Tr.SLocalize("execute"),
2018-08-28 20:13:01 +02:00
}
return gui.renderOptionsMap(g, 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 {
// better to delete because for example after closing update confirmation panel,
// the focus isn't set back to any of panels and one is unable to even quit
//_, err := g.SetViewOnBottom(v.Name())
2018-09-05 11:12:11 +02:00
err := g.DeleteView("menu")
if err != nil {
return err
}
return gui.returnFocus(g, v)
2018-08-28 20:13:01 +02:00
}
func (gui *Gui) GetKey(binding Binding) string {
r, ok := binding.Key.(rune)
key := ""
if ok {
key = string(r)
} else if binding.KeyReadable != "" {
key = binding.KeyReadable
}
return key
}
2018-09-05 13:01:21 +02:00
func (gui *Gui) GetMaxKeyLength(bindings []Binding) int {
2018-09-04 15:25:02 +02:00
max := 0
for _, binding := range bindings {
keyLength := len(gui.GetKey(binding))
if keyLength > max {
max = keyLength
}
}
return max
}
2018-09-05 11:12:11 +02:00
func (gui *Gui) handleMenu(g *gocui.Gui, v *gocui.View) error {
var (
contentGlobal, contentPanel []string
bindingsGlobal, bindingsPanel []Binding
)
// clear keys slice, so we don't have ghost elements
2018-09-04 15:29:43 +02:00
gui.State.Keys = gui.State.Keys[:0]
bindings := gui.GetKeybindings()
2018-09-05 13:01:21 +02:00
padWidth := gui.GetMaxKeyLength(bindings)
2018-08-28 20:13:01 +02:00
2018-08-29 11:56:28 +02:00
for _, binding := range bindings {
key := gui.GetKey(binding)
if key != "" && binding.Description != "" {
content := fmt.Sprintf("%s %s", utils.WithPadding(key, padWidth), binding.Description)
switch binding.ViewName {
case "":
contentGlobal = append(contentGlobal, content)
bindingsGlobal = append(bindingsGlobal, binding)
case v.Name():
contentPanel = append(contentPanel, content)
bindingsPanel = append(bindingsPanel, binding)
2018-09-05 13:01:21 +02:00
}
2018-08-28 20:13:01 +02:00
}
}
// append dummy element to have a separator between
// panel and global keybindings
contentPanel = append(contentPanel, "")
bindingsPanel = append(bindingsPanel, Binding{})
content := append(contentPanel, contentGlobal...)
gui.State.Keys = append(bindingsPanel, bindingsGlobal...)
// append newline at the end so the last line would be selectable
contentJoined := strings.Join(content, "\n") + "\n"
// y1-1 so there will not be an extra space at the end of panel
x0, y0, x1, y1 := gui.getConfirmationPanelDimensions(g, contentJoined)
2018-09-05 11:12:11 +02:00
menuView, _ := g.SetView("menu", x0, y0, x1, y1-1, 0)
menuView.Title = strings.Title(gui.Tr.SLocalize("menu"))
menuView.FgColor = gocui.ColorWhite
2018-09-05 11:12:11 +02:00
if err := gui.renderMenuOptions(g); err != nil {
return err
}
fmt.Fprint(menuView, contentJoined)
2018-08-28 20:13:01 +02:00
g.Update(func(g *gocui.Gui) error {
2018-09-05 11:12:11 +02:00
_, err := g.SetViewOnTop("menu")
2018-08-29 14:24:04 +02:00
if err != nil {
return err
}
2018-09-05 11:12:11 +02:00
return gui.switchFocus(g, v, menuView)
2018-08-28 20:13:01 +02:00
})
return nil
2018-08-29 11:56:28 +02:00
}