summaryrefslogtreecommitdiffstats
path: root/pkg/gui/workspace_reset_options_panel.go
blob: 0e3cd59c6b3bc4ac7437dac3654519e8d0c37418 (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
98
99
100
package gui

import (
	"github.com/fatih/color"
	"github.com/jesseduffield/gocui"
)

type workspaceResetOption struct {
	handler     func() error
	description string
	command     string
}

// GetDisplayStrings is a function.
func (r *workspaceResetOption) GetDisplayStrings(isFocused bool) []string {
	return []string{r.description, color.New(color.FgRed).Sprint(r.command)}
}

func (gui *Gui) handleCreateResetMenu(g *gocui.Gui, v *gocui.View) error {
	options := []*workspaceResetOption{
		{
			description: gui.Tr.SLocalize("discardAllChangesToAllFiles"),
			command:     "reset --hard HEAD && git clean -fd",
			handler: func() error {
				if err := gui.GitCommand.ResetAndClean(); err != nil {
					return gui.createErrorPanel(gui.g, err.Error())
				}

				return gui.refreshFiles()
			},
		},
		{
			description: gui.Tr.SLocalize("discardAnyUnstagedChanges"),
			command:     "git checkout -- .",
			handler: func() error {
				if err := gui.GitCommand.DiscardAnyUnstagedFileChanges(); err != nil {
					return gui.createErrorPanel(gui.g, err.Error())
				}

				return gui.refreshFiles()
			},
		},
		{
			description: gui.Tr.SLocalize("discardUntrackedFiles"),
			command:     "git clean -fd",
			handler: func() error {
				if err := gui.GitCommand.RemoveUntrackedFiles(); err != nil {
					return gui.createErrorPanel(gui.g, err.Error())
				}

				return gui.refreshFiles()
			},
		},
		{
			description: gui.Tr.SLocalize("softReset"),
			command:     "git reset --soft HEAD",
			handler: func() error {
				if err := gui.GitCommand.ResetSoft("HEAD"); err != nil {
					return gui.createErrorPanel(gui.g, err.Error())
				}

				return gui.refreshFiles()
			},
		},
		{
			description: gui.Tr.SLocalize("hardReset"),
			command:     "git reset --hard HEAD",
			handler: func() error {
				if err := gui.GitCommand.ResetHard("HEAD"); err != nil {
					return gui.createErrorPanel(gui.g, err.Error())
				}

				return gui.refreshFiles()
			},
		},
		{
			description: gui.Tr.SLocalize("hardResetUpstream"),
			command:     "git reset --hard @{upstream}",
			handler: func() error {
				if err := gui.GitCommand.ResetHard("@{upstream}"); err != nil {
					return gui.createErrorPanel(gui.g, err.Error())
				}

				return gui.refreshSidePanels(gui.g)
			},
		},
		{
			description: gui.Tr.SLocalize("cancel"),
			handler: func() error {
				return nil
			},
		},
	}

	handleMenuPress := func(index int) error {
		return options[index].handler()
	}

	return gui.createMenu("", options, len(options), handleMenuPress)
}