2018-08-14 11:05:26 +02:00
|
|
|
package gui
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/jesseduffield/gocui"
|
|
|
|
"github.com/jesseduffield/lazygit/pkg/commands"
|
2018-09-17 13:02:30 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/utils"
|
2018-08-14 11:05:26 +02:00
|
|
|
)
|
|
|
|
|
2018-12-06 13:18:17 +02:00
|
|
|
// list panel functions
|
|
|
|
|
|
|
|
func (gui *Gui) getSelectedStashEntry(v *gocui.View) *commands.StashEntry {
|
|
|
|
selectedLine := gui.State.Panels.Stash.SelectedLine
|
|
|
|
if selectedLine == -1 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return gui.State.StashEntries[selectedLine]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (gui *Gui) handleStashEntrySelect(g *gocui.Gui, v *gocui.View) error {
|
|
|
|
stashEntry := gui.getSelectedStashEntry(v)
|
|
|
|
if stashEntry == nil {
|
|
|
|
return gui.renderString(g, "main", gui.Tr.SLocalize("NoStashEntries"))
|
|
|
|
}
|
|
|
|
if err := gui.focusPoint(0, gui.State.Panels.Stash.SelectedLine, v); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
// doing this asynchronously cos it can take time
|
|
|
|
diff, _ := gui.GitCommand.GetStashEntryDiff(stashEntry.Index)
|
2018-12-07 09:52:31 +02:00
|
|
|
_ = gui.renderString(g, "main", diff)
|
2018-12-06 13:18:17 +02:00
|
|
|
}()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-08-14 11:05:26 +02:00
|
|
|
func (gui *Gui) refreshStashEntries(g *gocui.Gui) error {
|
|
|
|
g.Update(func(g *gocui.Gui) error {
|
|
|
|
gui.State.StashEntries = gui.GitCommand.GetStashEntries()
|
2018-09-12 10:23:25 +02:00
|
|
|
|
2018-12-06 13:18:17 +02:00
|
|
|
gui.refreshSelectedLine(&gui.State.Panels.Stash.SelectedLine, len(gui.State.StashEntries))
|
|
|
|
|
2018-09-17 13:02:30 +02:00
|
|
|
list, err := utils.RenderList(gui.State.StashEntries)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-12-06 13:18:17 +02:00
|
|
|
|
2018-12-08 07:54:54 +02:00
|
|
|
v := gui.getStashView()
|
2018-12-06 13:18:17 +02:00
|
|
|
v.Clear()
|
2018-09-17 13:02:30 +02:00
|
|
|
fmt.Fprint(v, list)
|
2018-09-12 10:23:25 +02:00
|
|
|
|
2018-12-08 07:54:54 +02:00
|
|
|
if err := gui.resetOrigin(v); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2018-08-14 11:05:26 +02:00
|
|
|
})
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-12-06 13:18:17 +02:00
|
|
|
func (gui *Gui) handleStashNextLine(g *gocui.Gui, v *gocui.View) error {
|
|
|
|
panelState := gui.State.Panels.Stash
|
|
|
|
gui.changeSelectedLine(&panelState.SelectedLine, len(gui.State.StashEntries), false)
|
|
|
|
|
2018-12-08 07:54:54 +02:00
|
|
|
if err := gui.resetOrigin(gui.getMainView()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-12-06 13:18:17 +02:00
|
|
|
return gui.handleStashEntrySelect(gui.g, v)
|
2018-08-14 11:05:26 +02:00
|
|
|
}
|
|
|
|
|
2018-12-06 13:18:17 +02:00
|
|
|
func (gui *Gui) handleStashPrevLine(g *gocui.Gui, v *gocui.View) error {
|
|
|
|
panelState := gui.State.Panels.Stash
|
|
|
|
gui.changeSelectedLine(&panelState.SelectedLine, len(gui.State.StashEntries), true)
|
|
|
|
|
2018-12-08 07:54:54 +02:00
|
|
|
if err := gui.resetOrigin(gui.getMainView()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-12-06 13:18:17 +02:00
|
|
|
return gui.handleStashEntrySelect(gui.g, v)
|
2018-08-14 11:05:26 +02:00
|
|
|
}
|
|
|
|
|
2018-12-06 13:18:17 +02:00
|
|
|
// specific functions
|
|
|
|
|
2018-08-14 11:05:26 +02:00
|
|
|
func (gui *Gui) handleStashApply(g *gocui.Gui, v *gocui.View) error {
|
|
|
|
return gui.stashDo(g, v, "apply")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (gui *Gui) handleStashPop(g *gocui.Gui, v *gocui.View) error {
|
|
|
|
return gui.stashDo(g, v, "pop")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (gui *Gui) handleStashDrop(g *gocui.Gui, v *gocui.View) error {
|
2018-08-16 07:16:32 +02:00
|
|
|
title := gui.Tr.SLocalize("StashDrop")
|
|
|
|
message := gui.Tr.SLocalize("SureDropStashEntry")
|
2018-08-15 11:12:46 +02:00
|
|
|
return gui.createConfirmationPanel(g, v, title, message, func(g *gocui.Gui, v *gocui.View) error {
|
2018-08-14 11:05:26 +02:00
|
|
|
return gui.stashDo(g, v, "drop")
|
|
|
|
}, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (gui *Gui) stashDo(g *gocui.Gui, v *gocui.View, method string) error {
|
|
|
|
stashEntry := gui.getSelectedStashEntry(v)
|
|
|
|
if stashEntry == nil {
|
2018-08-15 11:12:46 +02:00
|
|
|
errorMessage := gui.Tr.TemplateLocalize(
|
|
|
|
"NoStashTo",
|
2018-08-16 11:31:50 +02:00
|
|
|
Teml{
|
2018-08-15 11:12:46 +02:00
|
|
|
"method": method,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
return gui.createErrorPanel(g, errorMessage)
|
2018-08-14 11:05:26 +02:00
|
|
|
}
|
|
|
|
if err := gui.GitCommand.StashDo(stashEntry.Index, method); err != nil {
|
|
|
|
gui.createErrorPanel(g, err.Error())
|
|
|
|
}
|
|
|
|
gui.refreshStashEntries(g)
|
2018-12-08 07:54:54 +02:00
|
|
|
return gui.refreshFiles()
|
2018-08-14 11:05:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (gui *Gui) handleStashSave(g *gocui.Gui, filesView *gocui.View) error {
|
|
|
|
if len(gui.trackedFiles()) == 0 && len(gui.stagedFiles()) == 0 {
|
2018-08-16 07:16:32 +02:00
|
|
|
return gui.createErrorPanel(g, gui.Tr.SLocalize("NoTrackedStagedFilesStash"))
|
2018-08-14 11:05:26 +02:00
|
|
|
}
|
2018-08-16 07:16:32 +02:00
|
|
|
gui.createPromptPanel(g, filesView, gui.Tr.SLocalize("StashChanges"), func(g *gocui.Gui, v *gocui.View) error {
|
2018-08-14 11:05:26 +02:00
|
|
|
if err := gui.GitCommand.StashSave(gui.trimmedContent(v)); err != nil {
|
|
|
|
gui.createErrorPanel(g, err.Error())
|
|
|
|
}
|
|
|
|
gui.refreshStashEntries(g)
|
2018-12-08 07:54:54 +02:00
|
|
|
return gui.refreshFiles()
|
2018-08-14 11:05:26 +02:00
|
|
|
})
|
|
|
|
return nil
|
|
|
|
}
|