1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-02-01 13:17:53 +02:00

add getMaxKeyLength

This commit is contained in:
Dawid Dziurla 2018-09-04 15:25:02 +02:00
parent 67d99a24ea
commit f29c81fb5c
No known key found for this signature in database
GPG Key ID: 7B6D8368172E9B0B

View File

@ -5,6 +5,7 @@ import (
"strings"
"github.com/jesseduffield/gocui"
"github.com/jesseduffield/lazygit/pkg/utils"
)
var keys []Binding
@ -60,15 +61,27 @@ func (gui *Gui) GetKey(binding Binding) string {
return key
}
func (gui *Gui) getMaxKeyLength(bindings []Binding) int {
max := 0
for _, binding := range bindings {
keyLength := len(gui.GetKey(binding))
if keyLength > max {
max = keyLength
}
}
return max
}
func (gui *Gui) handleHelp(g *gocui.Gui, v *gocui.View) error {
// clear keys slice, so we don't have ghost elements
keys = keys[:0]
content := ""
bindings := gui.GetKeybindings()
padWidth := gui.getMaxKeyLength(bindings)
for _, binding := range bindings {
if key := gui.GetKey(binding); key != "" && binding.ViewName == v.Name() && binding.Description != "" {
content += fmt.Sprintf(" %s - %s\n", key, binding.Description)
content += fmt.Sprintf("%s %s\n", utils.WithPadding(key, padWidth), binding.Description)
keys = append(keys, binding)
}
}