2022-02-05 17:04:10 +11:00
|
|
|
package context
|
|
|
|
|
|
|
|
import (
|
2022-03-27 17:15:17 +11:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/gui/keybindings"
|
2022-03-27 17:22:31 +11:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/gui/style"
|
2022-02-05 17:04:10 +11:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/gui/types"
|
2023-05-21 12:06:22 +10:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/utils"
|
2023-05-21 11:02:36 +10:00
|
|
|
"github.com/samber/lo"
|
2022-02-05 17:04:10 +11:00
|
|
|
)
|
|
|
|
|
|
|
|
type MenuContext struct {
|
2023-05-21 11:02:36 +10:00
|
|
|
c *ContextCommon
|
|
|
|
|
2022-02-05 17:04:10 +11:00
|
|
|
*MenuViewModel
|
|
|
|
*ListContextTrait
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ types.IListContext = (*MenuContext)(nil)
|
|
|
|
|
|
|
|
func NewMenuContext(
|
2023-03-23 12:35:07 +11:00
|
|
|
c *ContextCommon,
|
2022-02-05 17:04:10 +11:00
|
|
|
) *MenuContext {
|
2023-05-21 11:02:36 +10:00
|
|
|
viewModel := NewMenuViewModel(c)
|
2022-02-05 17:04:10 +11:00
|
|
|
|
|
|
|
return &MenuContext{
|
2023-05-21 11:02:36 +10:00
|
|
|
c: c,
|
2022-02-05 17:04:10 +11:00
|
|
|
MenuViewModel: viewModel,
|
|
|
|
ListContextTrait: &ListContextTrait{
|
|
|
|
Context: NewSimpleContext(NewBaseContext(NewBaseContextOpts{
|
2023-03-21 20:57:52 +11:00
|
|
|
View: c.Views().Menu,
|
2022-06-13 11:01:26 +10:00
|
|
|
WindowName: "menu",
|
|
|
|
Key: "menu",
|
|
|
|
Kind: types.TEMPORARY_POPUP,
|
|
|
|
Focusable: true,
|
|
|
|
HasUncontrolledBounds: true,
|
2023-03-21 21:01:58 +11:00
|
|
|
})),
|
2023-08-17 20:57:20 +02:00
|
|
|
ListRenderer: ListRenderer{
|
|
|
|
list: viewModel,
|
|
|
|
getDisplayStrings: viewModel.GetDisplayStrings,
|
|
|
|
getColumnAlignments: func() []utils.Alignment { return viewModel.columnAlignment },
|
2023-08-08 13:26:26 +02:00
|
|
|
getNonModelItems: viewModel.GetNonModelItems,
|
2023-08-17 20:57:20 +02:00
|
|
|
},
|
|
|
|
c: c,
|
2022-02-05 17:04:10 +11:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: remove this thing.
|
|
|
|
func (self *MenuContext) GetSelectedItemId() string {
|
|
|
|
item := self.GetSelected()
|
|
|
|
if item == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2022-05-08 14:23:32 +10:00
|
|
|
return item.Label
|
2022-02-05 17:04:10 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
type MenuViewModel struct {
|
2023-07-20 21:23:46 +10:00
|
|
|
c *ContextCommon
|
|
|
|
menuItems []*types.MenuItem
|
|
|
|
columnAlignment []utils.Alignment
|
2023-05-27 14:14:43 +10:00
|
|
|
*FilteredListViewModel[*types.MenuItem]
|
2022-02-05 17:04:10 +11:00
|
|
|
}
|
|
|
|
|
2023-05-21 11:02:36 +10:00
|
|
|
func NewMenuViewModel(c *ContextCommon) *MenuViewModel {
|
2022-02-05 17:04:10 +11:00
|
|
|
self := &MenuViewModel{
|
|
|
|
menuItems: nil,
|
2023-05-21 11:02:36 +10:00
|
|
|
c: c,
|
2022-02-05 17:04:10 +11:00
|
|
|
}
|
|
|
|
|
2023-05-27 14:14:43 +10:00
|
|
|
self.FilteredListViewModel = NewFilteredListViewModel(
|
|
|
|
func() []*types.MenuItem { return self.menuItems },
|
|
|
|
func(item *types.MenuItem) []string { return item.LabelColumns },
|
|
|
|
)
|
2022-02-05 17:04:10 +11:00
|
|
|
|
|
|
|
return self
|
|
|
|
}
|
|
|
|
|
2023-07-20 21:23:46 +10:00
|
|
|
func (self *MenuViewModel) SetMenuItems(items []*types.MenuItem, columnAlignment []utils.Alignment) {
|
2022-02-05 17:04:10 +11:00
|
|
|
self.menuItems = items
|
2023-07-20 21:23:46 +10:00
|
|
|
self.columnAlignment = columnAlignment
|
2022-02-05 17:04:10 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: move into presentation package
|
2023-08-17 21:52:29 +02:00
|
|
|
func (self *MenuViewModel) GetDisplayStrings(_ int, _ int) [][]string {
|
2023-05-27 14:14:43 +10:00
|
|
|
menuItems := self.FilteredListViewModel.GetItems()
|
2023-07-24 13:06:42 +10:00
|
|
|
showKeys := lo.SomeBy(menuItems, func(item *types.MenuItem) bool {
|
2022-03-27 17:15:17 +11:00
|
|
|
return item.Key != nil
|
|
|
|
})
|
|
|
|
|
2023-07-24 13:06:42 +10:00
|
|
|
return lo.Map(menuItems, func(item *types.MenuItem, _ int) []string {
|
2022-07-30 20:27:51 +10:00
|
|
|
displayStrings := item.LabelColumns
|
2023-09-05 21:49:33 +02:00
|
|
|
if item.DisabledReason != "" {
|
|
|
|
displayStrings[0] = style.FgDefault.SetStrikethrough().Sprint(displayStrings[0])
|
|
|
|
}
|
2023-05-21 11:02:36 +10:00
|
|
|
|
|
|
|
if !showKeys {
|
|
|
|
return displayStrings
|
|
|
|
}
|
|
|
|
|
|
|
|
// These keys are used for general navigation so we'll strike them out to
|
|
|
|
// avoid confusion
|
|
|
|
reservedKeys := []string{
|
|
|
|
self.c.UserConfig.Keybinding.Universal.Confirm,
|
|
|
|
self.c.UserConfig.Keybinding.Universal.Select,
|
|
|
|
self.c.UserConfig.Keybinding.Universal.Return,
|
2023-05-27 14:14:43 +10:00
|
|
|
self.c.UserConfig.Keybinding.Universal.StartSearch,
|
2023-05-21 11:02:36 +10:00
|
|
|
}
|
|
|
|
keyLabel := keybindings.LabelFromKey(item.Key)
|
|
|
|
keyStyle := style.FgCyan
|
|
|
|
if lo.Contains(reservedKeys, keyLabel) {
|
|
|
|
keyStyle = style.FgDefault.SetStrikethrough()
|
2022-02-05 17:04:10 +11:00
|
|
|
}
|
2023-05-21 11:02:36 +10:00
|
|
|
|
2023-07-24 13:06:42 +10:00
|
|
|
displayStrings = utils.Prepend(displayStrings, keyStyle.Sprint(keyLabel))
|
2022-03-27 17:15:17 +11:00
|
|
|
return displayStrings
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-08-08 13:26:26 +02:00
|
|
|
func (self *MenuViewModel) GetNonModelItems() []*NonModelItem {
|
|
|
|
// Don't display section headers when we are filtering. The reason is that
|
|
|
|
// filtering changes the order of the items (they are sorted by best match),
|
|
|
|
// so all the sections would be messed up.
|
|
|
|
if self.FilteredListViewModel.IsFiltering() {
|
|
|
|
return []*NonModelItem{}
|
|
|
|
}
|
|
|
|
|
|
|
|
result := []*NonModelItem{}
|
|
|
|
menuItems := self.FilteredListViewModel.GetItems()
|
|
|
|
var prevSection *types.MenuSection = nil
|
|
|
|
for i, menuItem := range menuItems {
|
|
|
|
menuItem := menuItem
|
|
|
|
if menuItem.Section != nil && menuItem.Section != prevSection {
|
|
|
|
if prevSection != nil {
|
|
|
|
result = append(result, &NonModelItem{
|
|
|
|
Index: i,
|
|
|
|
Column: 1,
|
|
|
|
Content: "",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
result = append(result, &NonModelItem{
|
|
|
|
Index: i,
|
|
|
|
Column: 1,
|
|
|
|
Content: style.FgGreen.SetBold().Sprintf("--- %s ---", menuItem.Section.Title),
|
|
|
|
})
|
|
|
|
prevSection = menuItem.Section
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2022-03-27 17:15:17 +11:00
|
|
|
func (self *MenuContext) GetKeybindings(opts types.KeybindingsOpts) []*types.Binding {
|
|
|
|
basicBindings := self.ListContextTrait.GetKeybindings(opts)
|
2023-07-24 13:06:42 +10:00
|
|
|
menuItemsWithKeys := lo.Filter(self.menuItems, func(item *types.MenuItem, _ int) bool {
|
2022-03-27 17:15:17 +11:00
|
|
|
return item.Key != nil
|
|
|
|
})
|
|
|
|
|
2023-07-24 13:06:42 +10:00
|
|
|
menuItemBindings := lo.Map(menuItemsWithKeys, func(item *types.MenuItem, _ int) *types.Binding {
|
2022-03-27 17:15:17 +11:00
|
|
|
return &types.Binding{
|
|
|
|
Key: item.Key,
|
|
|
|
Handler: func() error { return self.OnMenuPress(item) },
|
2022-03-19 19:12:58 +11:00
|
|
|
}
|
|
|
|
})
|
2022-03-27 17:15:17 +11:00
|
|
|
|
|
|
|
// appending because that means the menu item bindings have lower precedence.
|
|
|
|
// So if a basic binding is to escape from the menu, we want that to still be
|
|
|
|
// what happens when you press escape. This matters when we're showing the menu
|
|
|
|
// for all keybindings of say the files context.
|
|
|
|
return append(basicBindings, menuItemBindings...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *MenuContext) OnMenuPress(selectedItem *types.MenuItem) error {
|
2023-09-05 21:49:33 +02:00
|
|
|
if selectedItem != nil && selectedItem.DisabledReason != "" {
|
|
|
|
return self.c.ErrorMsg(selectedItem.DisabledReason)
|
|
|
|
}
|
|
|
|
|
2022-03-27 17:15:17 +11:00
|
|
|
if err := self.c.PopContext(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-07-20 21:05:52 +10:00
|
|
|
if selectedItem == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-03-27 17:15:17 +11:00
|
|
|
if err := selectedItem.OnPress(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2022-02-05 17:04:10 +11:00
|
|
|
}
|