From 665fdded145599d5eb96dd7d61520afea5aa31b1 Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Fri, 14 Feb 2020 22:32:31 +1100 Subject: [PATCH] continue refactor of menu panel --- pkg/gui/discard_changes_menu_panel.go | 4 ++-- pkg/gui/menu_panel.go | 7 ++++++- pkg/gui/options_menu_panel.go | 30 ++++++++++++++------------- 3 files changed, 24 insertions(+), 17 deletions(-) diff --git a/pkg/gui/discard_changes_menu_panel.go b/pkg/gui/discard_changes_menu_panel.go index b5a40b9e9..01b2426aa 100644 --- a/pkg/gui/discard_changes_menu_panel.go +++ b/pkg/gui/discard_changes_menu_panel.go @@ -15,7 +15,7 @@ func (gui *Gui) handleCreateDiscardMenu(g *gocui.Gui, v *gocui.View) error { menuItems := []*menuItem{ { - displayStrings: []string{gui.Tr.SLocalize("discardAllChanges")}, + displayString: gui.Tr.SLocalize("discardAllChanges"), onPress: func() error { if err := gui.GitCommand.DiscardAllFileChanges(file); err != nil { return err @@ -27,7 +27,7 @@ func (gui *Gui) handleCreateDiscardMenu(g *gocui.Gui, v *gocui.View) error { if file.HasStagedChanges && file.HasUnstagedChanges { menuItems = append(menuItems, &menuItem{ - displayStrings: []string{gui.Tr.SLocalize("discardUnstagedChanges")}, + displayString: gui.Tr.SLocalize("discardUnstagedChanges"), onPress: func() error { if err := gui.GitCommand.DiscardUnstagedFileChanges(file); err != nil { return err diff --git a/pkg/gui/menu_panel.go b/pkg/gui/menu_panel.go index 94a889506..8f368ce92 100644 --- a/pkg/gui/menu_panel.go +++ b/pkg/gui/menu_panel.go @@ -9,6 +9,7 @@ import ( ) type menuItem struct { + displayString string displayStrings []string onPress func() error } @@ -62,7 +63,11 @@ func (gui *Gui) createMenuNew(title string, items []*menuItem, createMenuOptions stringArrays := make([][]string, len(items)) for i, item := range items { - stringArrays[i] = item.displayStrings + if item.displayStrings == nil { + stringArrays[i] = []string{item.displayString} + } else { + stringArrays[i] = item.displayStrings + } } list := utils.RenderDisplayStrings(stringArrays) diff --git a/pkg/gui/options_menu_panel.go b/pkg/gui/options_menu_panel.go index 51abb64bb..934da00c6 100644 --- a/pkg/gui/options_menu_panel.go +++ b/pkg/gui/options_menu_panel.go @@ -3,8 +3,6 @@ package gui import ( "strings" - "github.com/go-errors/errors" - "github.com/jesseduffield/gocui" "github.com/jesseduffield/lazygit/pkg/utils" ) @@ -38,19 +36,23 @@ func (gui *Gui) getBindings(v *gocui.View) []*Binding { func (gui *Gui) handleCreateOptionsMenu(g *gocui.Gui, v *gocui.View) error { bindings := gui.getBindings(v) - handleMenuPress := func(index int) error { - if bindings[index].Key == nil { - return nil + menuItems := make([]*menuItem, len(bindings)) + + for i, binding := range bindings { + innerBinding := binding // note to self, never close over loop variables + menuItems[i] = &menuItem{ + displayStrings: []string{GetKeyDisplay(innerBinding.Key), innerBinding.Description}, + onPress: func() error { + if innerBinding.Key == nil { + return nil + } + if err := gui.handleMenuClose(g, v); err != nil { + return err + } + return innerBinding.Handler(g, v) + }, } - if index >= len(bindings) { - return errors.New("Index is greater than size of bindings") - } - err := gui.handleMenuClose(g, v) - if err != nil { - return err - } - return bindings[index].Handler(g, v) } - return gui.createMenu(strings.Title(gui.Tr.SLocalize("menu")), bindings, len(bindings), handleMenuPress) + return gui.createMenuNew(strings.Title(gui.Tr.SLocalize("menu")), menuItems, createMenuOptions{}) }