2018-06-05 10:48:46 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-07-21 07:51:18 +02:00
|
|
|
"fmt"
|
2018-06-05 10:48:46 +02:00
|
|
|
|
2018-07-21 07:51:18 +02:00
|
|
|
"github.com/jesseduffield/gocui"
|
2018-06-05 10:48:46 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func refreshStashEntries(g *gocui.Gui) error {
|
2018-07-21 07:51:18 +02:00
|
|
|
g.Update(func(g *gocui.Gui) error {
|
|
|
|
v, err := g.View("stash")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
state.StashEntries = getGitStashEntries()
|
|
|
|
v.Clear()
|
|
|
|
for _, stashEntry := range state.StashEntries {
|
|
|
|
fmt.Fprintln(v, stashEntry.DisplayString)
|
|
|
|
}
|
|
|
|
return resetOrigin(v)
|
|
|
|
})
|
|
|
|
return nil
|
2018-06-05 10:48:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func getSelectedStashEntry(v *gocui.View) *StashEntry {
|
2018-07-21 07:51:18 +02:00
|
|
|
if len(state.StashEntries) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
lineNumber := getItemPosition(v)
|
|
|
|
return &state.StashEntries[lineNumber]
|
2018-06-05 10:48:46 +02:00
|
|
|
}
|
|
|
|
|
2018-06-09 11:06:33 +02:00
|
|
|
func renderStashOptions(g *gocui.Gui) error {
|
2018-07-21 07:51:18 +02:00
|
|
|
return renderOptionsMap(g, map[string]string{
|
2018-07-29 02:40:54 +02:00
|
|
|
"space": "apply",
|
|
|
|
"k": "pop",
|
|
|
|
"d": "drop",
|
|
|
|
"← → ↑ ↓": "navigate",
|
2018-07-21 07:51:18 +02:00
|
|
|
})
|
2018-06-09 11:06:33 +02:00
|
|
|
}
|
|
|
|
|
2018-06-05 10:48:46 +02:00
|
|
|
func handleStashEntrySelect(g *gocui.Gui, v *gocui.View) error {
|
2018-07-21 07:51:18 +02:00
|
|
|
if err := renderStashOptions(g); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
stashEntry := getSelectedStashEntry(v)
|
|
|
|
if stashEntry == nil {
|
|
|
|
renderString(g, "main", "No stash entries")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
diff, _ := getStashEntryDiff(stashEntry.Index)
|
|
|
|
renderString(g, "main", diff)
|
|
|
|
}()
|
|
|
|
return nil
|
2018-06-05 10:48:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func handleStashApply(g *gocui.Gui, v *gocui.View) error {
|
2018-07-21 07:51:18 +02:00
|
|
|
return stashDo(g, v, "apply")
|
2018-06-05 10:48:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func handleStashPop(g *gocui.Gui, v *gocui.View) error {
|
2018-07-21 07:51:18 +02:00
|
|
|
return stashDo(g, v, "pop")
|
2018-06-05 10:48:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func handleStashDrop(g *gocui.Gui, v *gocui.View) error {
|
2018-07-21 10:37:00 +02:00
|
|
|
return createConfirmationPanel(g, v, "Stash drop", "Are you sure you want to drop this stash entry?", func(g *gocui.Gui, v *gocui.View) error {
|
|
|
|
return stashDo(g, v, "drop")
|
|
|
|
}, nil)
|
2018-06-05 10:48:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func stashDo(g *gocui.Gui, v *gocui.View, method string) error {
|
2018-07-21 07:51:18 +02:00
|
|
|
stashEntry := getSelectedStashEntry(v)
|
|
|
|
if stashEntry == nil {
|
|
|
|
return createErrorPanel(g, "No stash to "+method)
|
|
|
|
}
|
|
|
|
if output, err := gitStashDo(stashEntry.Index, method); err != nil {
|
|
|
|
createErrorPanel(g, output)
|
|
|
|
}
|
|
|
|
refreshStashEntries(g)
|
|
|
|
return refreshFiles(g)
|
2018-06-05 10:48:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func handleStashSave(g *gocui.Gui, filesView *gocui.View) error {
|
2018-07-21 07:51:18 +02:00
|
|
|
createPromptPanel(g, filesView, "Stash changes", func(g *gocui.Gui, v *gocui.View) error {
|
|
|
|
if output, err := gitStashSave(trimmedContent(v)); err != nil {
|
|
|
|
createErrorPanel(g, output)
|
|
|
|
}
|
|
|
|
refreshStashEntries(g)
|
|
|
|
return refreshFiles(g)
|
|
|
|
})
|
|
|
|
return nil
|
2018-06-05 10:48:46 +02:00
|
|
|
}
|