From 0f0fda16605059ebae73d29f0e4b9b5d1455ce73 Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Thu, 30 May 2019 22:45:56 +1000 Subject: allow stashing staged changes reinstate old stash functionality with the 's' keybinding --- pkg/gui/files_panel.go | 43 +++++++++++++++++++++++++++++++++++++++++++ pkg/gui/keybindings.go | 10 ++++++++-- pkg/gui/menu_panel.go | 2 +- pkg/gui/stash_panel.go | 9 ++++----- 4 files changed, 56 insertions(+), 8 deletions(-) (limited to 'pkg/gui') diff --git a/pkg/gui/files_panel.go b/pkg/gui/files_panel.go index 3d2001ac1..043c6ceab 100644 --- a/pkg/gui/files_panel.go +++ b/pkg/gui/files_panel.go @@ -627,3 +627,46 @@ func (gui *Gui) handleCustomCommand(g *gocui.Gui, v *gocui.View) error { return gui.Errors.ErrSubProcess }) } + +type stashOption struct { + description string + handler func() error +} + +// GetDisplayStrings is a function. +func (o *stashOption) GetDisplayStrings(isFocused bool) []string { + return []string{o.description} +} + +func (gui *Gui) handleCreateStashMenu(g *gocui.Gui, v *gocui.View) error { + options := []*stashOption{ + { + description: gui.Tr.SLocalize("stashAllChanges"), + handler: func() error { + return gui.handleStashSave(gui.GitCommand.StashSave) + }, + }, + { + description: gui.Tr.SLocalize("stashStagedChanges"), + handler: func() error { + return gui.handleStashSave(gui.GitCommand.StashSaveStagedChanges) + }, + }, + { + description: gui.Tr.SLocalize("cancel"), + handler: func() error { + return nil + }, + }, + } + + handleMenuPress := func(index int) error { + return options[index].handler() + } + + return gui.createMenu(gui.Tr.SLocalize("stashOptions"), options, len(options), handleMenuPress) +} + +func (gui *Gui) handleStashChanges(g *gocui.Gui, v *gocui.View) error { + return gui.handleStashSave(gui.GitCommand.StashSave) +} diff --git a/pkg/gui/keybindings.go b/pkg/gui/keybindings.go index 5cbd7b6eb..ce12cf66f 100644 --- a/pkg/gui/keybindings.go +++ b/pkg/gui/keybindings.go @@ -228,12 +228,18 @@ func (gui *Gui) GetInitialKeybindings() []*Binding { Modifier: gocui.ModNone, Handler: gui.handleRefreshFiles, Description: gui.Tr.SLocalize("refreshFiles"), + }, { + ViewName: "files", + Key: 's', + Modifier: gocui.ModNone, + Handler: gui.handleStashChanges, + Description: gui.Tr.SLocalize("stashAllChanges"), }, { ViewName: "files", Key: 'S', Modifier: gocui.ModNone, - Handler: gui.handleStashSave, - Description: gui.Tr.SLocalize("stashFiles"), + Handler: gui.handleCreateStashMenu, + Description: gui.Tr.SLocalize("viewStashOptions"), }, { ViewName: "files", Key: 'a', diff --git a/pkg/gui/menu_panel.go b/pkg/gui/menu_panel.go index 66dabd03e..370c7fa5a 100644 --- a/pkg/gui/menu_panel.go +++ b/pkg/gui/menu_panel.go @@ -70,7 +70,7 @@ func (gui *Gui) createMenu(title string, items interface{}, itemCount int, handl wrappedHandlePress := func(g *gocui.Gui, v *gocui.View) error { selectedLine := gui.State.Panels.Menu.SelectedLine if err := handlePress(selectedLine); err != nil { - return err + return gui.createErrorPanel(gui.g, err.Error()) } if _, err := gui.g.View("menu"); err == nil { if _, err := gui.g.SetViewOnBottom("menu"); err != nil { diff --git a/pkg/gui/stash_panel.go b/pkg/gui/stash_panel.go index 9c1851971..4a2b463ce 100644 --- a/pkg/gui/stash_panel.go +++ b/pkg/gui/stash_panel.go @@ -130,16 +130,15 @@ func (gui *Gui) stashDo(g *gocui.Gui, v *gocui.View, method string) error { return gui.refreshFiles() } -func (gui *Gui) handleStashSave(g *gocui.Gui, filesView *gocui.View) error { +func (gui *Gui) handleStashSave(stashFunc func(message string) error) error { if len(gui.trackedFiles()) == 0 && len(gui.stagedFiles()) == 0 { - return gui.createErrorPanel(g, gui.Tr.SLocalize("NoTrackedStagedFilesStash")) + return gui.createErrorPanel(gui.g, gui.Tr.SLocalize("NoTrackedStagedFilesStash")) } - gui.createPromptPanel(g, filesView, gui.Tr.SLocalize("StashChanges"), func(g *gocui.Gui, v *gocui.View) error { - if err := gui.GitCommand.StashSave(gui.trimmedContent(v)); err != nil { + return gui.createPromptPanel(gui.g, gui.getFilesView(), gui.Tr.SLocalize("StashChanges"), func(g *gocui.Gui, v *gocui.View) error { + if err := stashFunc(gui.trimmedContent(v)); err != nil { gui.createErrorPanel(g, err.Error()) } gui.refreshStashEntries(g) return gui.refreshFiles() }) - return nil } -- cgit v1.2.3