File: quit_actions.go

package info (click to toggle)
lazygit 0.50.0%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,808 kB
  • sloc: sh: 128; makefile: 76
file content (102 lines) | stat: -rw-r--r-- 2,297 bytes parent folder | download | duplicates (2)
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
101
102
package controllers

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

type QuitActions struct {
	c *ControllerCommon
}

func (self *QuitActions) Quit() error {
	self.c.State().SetRetainOriginalDir(false)
	return self.quitAux()
}

func (self *QuitActions) QuitWithoutChangingDirectory() error {
	self.c.State().SetRetainOriginalDir(true)
	return self.quitAux()
}

func (self *QuitActions) quitAux() error {
	if self.c.State().GetUpdating() {
		return self.confirmQuitDuringUpdate()
	}

	if self.c.UserConfig().ConfirmOnQuit {
		self.c.Confirm(types.ConfirmOpts{
			Title:  "",
			Prompt: self.c.Tr.ConfirmQuit,
			HandleConfirm: func() error {
				return gocui.ErrQuit
			},
		})

		return nil
	}

	return gocui.ErrQuit
}

func (self *QuitActions) confirmQuitDuringUpdate() error {
	self.c.Confirm(types.ConfirmOpts{
		Title:  self.c.Tr.ConfirmQuitDuringUpdateTitle,
		Prompt: self.c.Tr.ConfirmQuitDuringUpdate,
		HandleConfirm: func() error {
			return gocui.ErrQuit
		},
	})

	return nil
}

func (self *QuitActions) Escape() error {
	currentContext := self.c.Context().Current()

	if listContext, ok := currentContext.(types.IListContext); ok {
		if listContext.GetList().IsSelectingRange() {
			listContext.GetList().CancelRangeSelect()
			self.c.PostRefreshUpdate(listContext)
			return nil
		}
	}

	switch ctx := currentContext.(type) {
	case types.IFilterableContext:
		if ctx.IsFiltering() {
			self.c.Helpers().Search.Cancel()
			return nil
		}
	case types.ISearchableContext:
		if ctx.IsSearching() {
			self.c.Helpers().Search.Cancel()
			return nil
		}
	}

	parentContext := currentContext.GetParentContext()
	if parentContext != nil {
		// TODO: think about whether this should be marked as a return rather than adding to the stack
		self.c.Context().Push(parentContext, types.OnFocusOpts{})
		return nil
	}

	for _, mode := range self.c.Helpers().Mode.Statuses() {
		if mode.IsActive() {
			return mode.Reset()
		}
	}

	repoPathStack := self.c.State().GetRepoPathStack()
	if !repoPathStack.IsEmpty() {
		return self.c.Helpers().Repos.DispatchSwitchToRepo(repoPathStack.Pop(), context.NO_CONTEXT)
	}

	if self.c.UserConfig().QuitOnTopLevelReturn {
		return self.Quit()
	}

	return nil
}