summaryrefslogtreecommitdiffstats
path: root/pkg/gui/stash_panel.go
blob: b4fb1190267f57dee76defb1a5a244a4124926fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package gui

import (
	"fmt"

	"github.com/jesseduffield/gocui"
	"github.com/jesseduffield/lazygit/pkg/commands"
)

func (gui *Gui) refreshStashEntries(g *gocui.Gui) error {
	g.Update(func(g *gocui.Gui) error {
		v, err := g.View("stash")
		if err != nil {
			panic(err)
		}
		gui.State.StashEntries = gui.GitCommand.GetStashEntries()
		v.Clear()
		for _, stashEntry := range gui.State.StashEntries {
			fmt.Fprintln(v, stashEntry.DisplayString)
		}
		return gui.resetOrigin(v)
	})
	return nil
}

func (gui *Gui) getSelectedStashEntry(v *gocui.View) *commands.StashEntry {
	if len(gui.State.StashEntries) == 0 {
		return nil
	}
	lineNumber := gui.getItemPosition(v)
	return &gui.State.StashEntries[lineNumber]
}

func (gui *Gui) renderStashOptions(g *gocui.Gui) error {
	return gui.renderOptionsMap(g, map[string]string{
		"space":   "apply",
		"g":       "pop",
		"d":       "drop",
		"← → ↑ ↓": "navigate",
	})
}

func (gui *Gui) handleStashEntrySelect(g *gocui.Gui, v *gocui.View) error {
	if err := gui.renderStashOptions(g); err != nil {
		return err
	}
	go func() {
		stashEntry := gui.getSelectedStashEntry(v)
		if stashEntry == nil {
			gui.renderString(g, "main", "No stash entries")
			return
		}
		diff, _ := gui.GitCommand.GetStashEntryDiff(stashEntry.Index)
		gui.renderString(g, "main", diff)
	}()
	return nil
}

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 {
	return gui.createConfirmationPanel(g, v, "Stash drop", "Are you sure you want to drop this stash entry?", func(g *gocui.Gui, v *gocui.View) error {
		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 {
		return gui.createErrorPanel(g, "No stash to "+method)
	}
	if err := gui.GitCommand.StashDo(stashEntry.Index, method); err != nil {
		gui.createErrorPanel(g, err.Error())
	}
	gui.refreshStashEntries(g)
	return gui.refreshFiles(g)
}

func (gui *Gui) handleStashSave(g *gocui.Gui, filesView *gocui.View) error {
	if len(gui.trackedFiles()) == 0 && len(gui.stagedFiles()) == 0 {
		return gui.createErrorPanel(g, "You have no tracked/staged files to stash")
	}
	gui.createPromptPanel(g, filesView, "Stash changes", func(g *gocui.Gui, v *gocui.View) error {
		if err := gui.GitCommand.StashSave(gui.trimmedContent(v)); err != nil {
			gui.createErrorPanel(g, err.Error())
		}
		gui.refreshStashEntries(g)
		return gui.refreshFiles(g)
	})
	return nil
}