2022-12-30 23:24:24 +11:00
|
|
|
package gui
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2024-01-02 12:19:31 +11:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/gui/context"
|
2023-03-23 12:35:07 +11:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/gui/controllers/helpers"
|
2022-12-30 23:24:24 +11:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/gui/keybindings"
|
2024-01-02 12:19:31 +11:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/gui/style"
|
2022-12-30 23:24:24 +11:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/gui/types"
|
2024-01-02 12:19:31 +11:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/theme"
|
|
|
|
"github.com/jesseduffield/lazygit/pkg/utils"
|
2023-03-21 20:57:52 +11:00
|
|
|
"github.com/samber/lo"
|
2022-12-30 23:24:24 +11:00
|
|
|
)
|
|
|
|
|
|
|
|
type OptionsMapMgr struct {
|
2023-03-23 12:35:07 +11:00
|
|
|
c *helpers.HelperCommon
|
2022-12-30 23:24:24 +11:00
|
|
|
}
|
|
|
|
|
2024-01-02 12:19:31 +11:00
|
|
|
func (gui *Gui) renderContextOptionsMap() {
|
2023-07-31 18:32:38 +10:00
|
|
|
// In demos, we render our own content to this view
|
|
|
|
if gui.integrationTest != nil && gui.integrationTest.IsDemo() {
|
|
|
|
return
|
|
|
|
}
|
2022-12-30 23:24:24 +11:00
|
|
|
mgr := OptionsMapMgr{c: gui.c}
|
2024-01-02 12:19:31 +11:00
|
|
|
mgr.renderContextOptionsMap()
|
2022-12-30 23:24:24 +11:00
|
|
|
}
|
|
|
|
|
2024-01-02 12:19:31 +11:00
|
|
|
// Render the options available for the current context at the bottom of the screen
|
|
|
|
// STYLE GUIDE: we use the default options fg color for most keybindings. We can
|
|
|
|
// only use a different color if we're in a specific mode where the user is likely
|
|
|
|
// to want to press that key. For example, when in cherry-picking mode, we
|
|
|
|
// want to prominently show the keybinding for pasting commits.
|
|
|
|
func (self *OptionsMapMgr) renderContextOptionsMap() {
|
|
|
|
currentContext := self.c.CurrentContext()
|
|
|
|
|
|
|
|
currentContextBindings := currentContext.GetKeybindings(self.c.KeybindingsOpts())
|
|
|
|
globalBindings := self.c.Contexts().Global.GetKeybindings(self.c.KeybindingsOpts())
|
|
|
|
|
|
|
|
allBindings := append(currentContextBindings, globalBindings...)
|
|
|
|
|
|
|
|
bindingsToDisplay := lo.Filter(allBindings, func(binding *types.Binding, _ int) bool {
|
|
|
|
return binding.DisplayOnScreen && !binding.IsDisabled()
|
|
|
|
})
|
|
|
|
|
|
|
|
optionsMap := lo.Map(bindingsToDisplay, func(binding *types.Binding, _ int) bindingInfo {
|
|
|
|
displayStyle := theme.OptionsFgColor
|
|
|
|
if binding.DisplayStyle != nil {
|
|
|
|
displayStyle = *binding.DisplayStyle
|
|
|
|
}
|
|
|
|
|
|
|
|
description := binding.Description
|
|
|
|
if binding.ShortDescription != "" {
|
|
|
|
description = binding.ShortDescription
|
|
|
|
}
|
|
|
|
|
|
|
|
return bindingInfo{
|
|
|
|
key: keybindings.LabelFromKey(binding.Key),
|
|
|
|
description: description,
|
|
|
|
style: displayStyle,
|
|
|
|
}
|
2023-03-21 20:57:52 +11:00
|
|
|
})
|
|
|
|
|
2024-01-02 12:19:31 +11:00
|
|
|
// Mode-specific local keybindings
|
|
|
|
if currentContext.GetKey() == context.LOCAL_COMMITS_CONTEXT_KEY {
|
|
|
|
if self.c.Modes().CherryPicking.Active() {
|
|
|
|
optionsMap = utils.Prepend(optionsMap, bindingInfo{
|
|
|
|
key: keybindings.Label(self.c.KeybindingsOpts().Config.Commits.PasteCommits),
|
|
|
|
description: self.c.Tr.PasteCommits,
|
|
|
|
style: style.FgCyan,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.c.Model().BisectInfo.Started() {
|
|
|
|
optionsMap = utils.Prepend(optionsMap, bindingInfo{
|
|
|
|
key: keybindings.Label(self.c.KeybindingsOpts().Config.Commits.ViewBisectOptions),
|
|
|
|
description: self.c.Tr.ViewBisectOptions,
|
|
|
|
style: style.FgGreen,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mode-specific global keybindings
|
|
|
|
if self.c.Model().WorkingTreeStateAtLastCommitRefresh.IsRebasing() {
|
|
|
|
optionsMap = utils.Prepend(optionsMap, bindingInfo{
|
|
|
|
key: keybindings.Label(self.c.KeybindingsOpts().Config.Universal.CreateRebaseOptionsMenu),
|
|
|
|
description: self.c.Tr.ViewRebaseOptions,
|
|
|
|
style: style.FgYellow,
|
|
|
|
})
|
|
|
|
} else if self.c.Model().WorkingTreeStateAtLastCommitRefresh.IsMerging() {
|
|
|
|
optionsMap = utils.Prepend(optionsMap, bindingInfo{
|
|
|
|
key: keybindings.Label(self.c.KeybindingsOpts().Config.Universal.CreateRebaseOptionsMenu),
|
|
|
|
description: self.c.Tr.ViewMergeOptions,
|
|
|
|
style: style.FgYellow,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.c.Git().Patch.PatchBuilder.Active() {
|
|
|
|
optionsMap = utils.Prepend(optionsMap, bindingInfo{
|
|
|
|
key: keybindings.Label(self.c.KeybindingsOpts().Config.Universal.CreatePatchOptionsMenu),
|
|
|
|
description: self.c.Tr.ViewPatchOptions,
|
|
|
|
style: style.FgYellow,
|
2023-03-21 20:57:52 +11:00
|
|
|
})
|
2022-12-30 23:24:24 +11:00
|
|
|
}
|
|
|
|
|
2023-03-21 20:57:52 +11:00
|
|
|
self.renderOptions(self.formatBindingInfos(optionsMap))
|
2022-12-30 23:24:24 +11:00
|
|
|
}
|
|
|
|
|
2023-03-21 20:57:52 +11:00
|
|
|
func (self *OptionsMapMgr) formatBindingInfos(bindingInfos []bindingInfo) string {
|
2024-01-02 12:19:31 +11:00
|
|
|
width := self.c.Views().Options.Width() - 4 // -4 for the padding
|
|
|
|
var builder strings.Builder
|
|
|
|
ellipsis := "…"
|
|
|
|
separator := " | "
|
|
|
|
|
|
|
|
length := 0
|
|
|
|
|
|
|
|
for i, info := range bindingInfos {
|
|
|
|
plainText := fmt.Sprintf("%s: %s", info.description, info.key)
|
|
|
|
|
|
|
|
// Check if adding the next formatted string exceeds the available width
|
|
|
|
if i > 0 && length+len(separator)+len(plainText) > width {
|
|
|
|
builder.WriteString(theme.OptionsFgColor.Sprint(separator + ellipsis))
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
formatted := info.style.Sprintf(plainText)
|
|
|
|
|
|
|
|
if i > 0 {
|
|
|
|
builder.WriteString(theme.OptionsFgColor.Sprint(separator))
|
|
|
|
length += len(separator)
|
|
|
|
}
|
|
|
|
builder.WriteString(formatted)
|
|
|
|
length += len(plainText)
|
|
|
|
}
|
|
|
|
|
|
|
|
return builder.String()
|
2022-12-30 23:24:24 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
func (self *OptionsMapMgr) renderOptions(options string) {
|
|
|
|
self.c.SetViewContent(self.c.Views().Options, options)
|
|
|
|
}
|
|
|
|
|
2023-03-21 20:57:52 +11:00
|
|
|
type bindingInfo struct {
|
|
|
|
key string
|
|
|
|
description string
|
2024-01-02 12:19:31 +11:00
|
|
|
style style.TextStyle
|
2023-03-21 20:57:52 +11:00
|
|
|
}
|