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

import (
	"os"

	"github.com/jesseduffield/gocui"
	"github.com/jesseduffield/lazygit/pkg/gui/types"
)

// when a user runs lazygit with the LAZYGIT_NEW_DIR_FILE env variable defined
// we will write the current directory to that file on exit so that their
// shell can then change to that directory. That means you don't get kicked
// back to the directory that you started with.
func (gui *Gui) recordCurrentDirectory() error {
	// determine current directory, set it in LAZYGIT_NEW_DIR_FILE
	dirName, err := os.Getwd()
	if err != nil {
		return err
	}
	return gui.recordDirectory(dirName)
}

func (gui *Gui) recordDirectory(dirName string) error {
	newDirFilePath := os.Getenv("LAZYGIT_NEW_DIR_FILE")
	if newDirFilePath == "" {
		return nil
	}
	return gui.OSCommand.CreateFileWithContent(newDirFilePath, dirName)
}

func (gui *Gui) handleQuitWithoutChangingDirectory() error {
	gui.RetainOriginalDir = true
	return gui.quit()
}

func (gui *Gui) handleQuit() error {
	gui.RetainOriginalDir = false
	return gui.quit()
}

func (gui *Gui) handleTopLevelReturn() error {
	currentContext := gui.currentContext()

	parentContext, hasParent := currentContext.GetParentContext()
	if hasParent && currentContext != nil && parentContext != nil {
		// TODO: think about whether this should be marked as a return rather than adding to the stack
		return gui.c.PushContext(parentContext)
	}

	for _, mode := range gui.modeStatuses() {
		if mode.isActive() {
			return mode.reset()
		}
	}

	repoPathStack := gui.RepoPathStack
	if !repoPathStack.IsEmpty() {
		path := repoPathStack.Pop()

		return gui.dispatchSwitchToRepo(path, true)
	}

	if gui.c.UserConfig.QuitOnTopLevelReturn {
		return gui.handleQuit()
	}

	return nil
}

func (gui *Gui) quit() error {
	if gui.State.Updating {
		return gui.createUpdateQuitConfirmation()
	}

	if gui.c.UserConfig.ConfirmOnQuit {
		return gui.c.Ask(types.AskOpts{
			Title:  "",
			Prompt: gui.c.Tr.ConfirmQuit,
			HandleConfirm: func() error {
				return gocui.ErrQuit
			},
		})
	}

	return gocui.ErrQuit
}