summaryrefslogtreecommitdiffstats
path: root/pkg/gui/reflog_reset_options_panel.go
blob: c76dfed81aa93310c4a9340a9d86697a8572145b (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
package gui

import (
	"fmt"

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

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

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

func (gui *Gui) handleCreateReflogResetMenu(g *gocui.Gui, v *gocui.View) error {
	commit := gui.getSelectedReflogCommit()

	resetFunction := func(reset func(string) error) func() error {
		return func() error {
			if err := reset(commit.Sha); err != nil {
				return gui.createErrorPanel(gui.g, err.Error())
			}

			gui.State.Panels.ReflogCommits.SelectedLine = 0

			return gui.refreshSidePanels(gui.g)
		}
	}

	options := []*reflogResetOption{
		{
			description: gui.Tr.SLocalize("hardReset"),
			command:     fmt.Sprintf("reset --hard %s", commit.Sha),
			handler:     resetFunction(gui.GitCommand.ResetHard),
		},
		{
			description: gui.Tr.SLocalize("softReset"),
			command:     fmt.Sprintf("reset --soft %s", commit.Sha),
			handler:     resetFunction(gui.GitCommand.ResetSoft),
		},
		{
			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)
}