summaryrefslogtreecommitdiffstats
path: root/pkg/gui/controllers/filtering_menu_action.go
blob: 3771cdabde345e52d23b9c237733229da769d50e (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package controllers

import (
	"fmt"
	"strings"

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

type FilteringMenuAction struct {
	c *ControllerCommon
}

func (self *FilteringMenuAction) Call() error {
	fileName := ""
	author := ""
	switch self.c.CurrentSideContext() {
	case self.c.Contexts().Files:
		node := self.c.Contexts().Files.GetSelected()
		if node != nil {
			fileName = node.GetPath()
		}
	case self.c.Contexts().CommitFiles:
		node := self.c.Contexts().CommitFiles.GetSelected()
		if node != nil {
			fileName = node.GetPath()
		}
	case self.c.Contexts().LocalCommits:
		commit := self.c.Contexts().LocalCommits.GetSelected()
		if commit != nil {
			author = fmt.Sprintf("%s <%s>", commit.AuthorName, commit.AuthorEmail)
		}
	}

	menuItems := []*types.MenuItem{}
	tooltip := ""
	if self.c.Modes().Filtering.Active() {
		tooltip = self.c.Tr.WillCancelExistingFilterTooltip
	}

	if fileName != "" {
		menuItems = append(menuItems, &types.MenuItem{
			Label: fmt.Sprintf("%s '%s'", self.c.Tr.FilterBy, fileName),
			OnPress: func() error {
				return self.setFilteringPath(fileName)
			},
			Tooltip: tooltip,
		})
	}

	if author != "" {
		menuItems = append(menuItems, &types.MenuItem{
			Label: fmt.Sprintf("%s '%s'", self.c.Tr.FilterBy, author),
			OnPress: func() error {
				return self.setFilteringAuthor(author)
			},
			Tooltip: tooltip,
		})
	}

	menuItems = append(menuItems, &types.MenuItem{
		Label: self.c.Tr.FilterPathOption,
		OnPress: func() error {
			return self.c.Prompt(types.PromptOpts{
				FindSuggestionsFunc: self.c.Helpers().Suggestions.GetFilePathSuggestionsFunc(),
				Title:               self.c.Tr.EnterFileName,
				HandleConfirm: func(response string) error {
					return self.setFilteringPath(strings.TrimSpace(response))
				},
			})
		},
		Tooltip: tooltip,
	})

	menuItems = append(menuItems, &types.MenuItem{
		Label: self.c.Tr.FilterAuthorOption,
		OnPress: func() error {
			return self.c.Prompt(types.PromptOpts{
				FindSuggestionsFunc: self.c.Helpers().Suggestions.GetAuthorsSuggestionsFunc(),
				Title:               self.c.Tr.EnterAuthor,
				HandleConfirm: func(response string) error {
					return self.setFilteringAuthor(strings.TrimSpace(response))
				},
			})
		},
		Tooltip: tooltip,
	})

	if self.c.Modes().Filtering.Active() {
		menuItems = append(menuItems, &types.MenuItem{
			Label:   self.c.Tr.ExitFilterMode,
			OnPress: self.c.Helpers().Mode.ClearFiltering,
		})
	}

	return self.c.Menu(types.CreateMenuOptions{Title: self.c.Tr.FilteringMenuTitle, Items: menuItems})
}

func (self *FilteringMenuAction) setFilteringPath(path string) error {
	self.c.Modes().Filtering.Reset()
	self.c.Modes().Filtering.SetPath(path)
	return self.setFiltering()
}

func (self *FilteringMenuAction) setFilteringAuthor(author string) error {
	self.c.Modes().Filtering.Reset()
	self.c.Modes().Filtering.SetAuthor(author)
	return self.setFiltering()
}

func (self *FilteringMenuAction) setFiltering() error {
	self.c.Modes().Filtering.SetSelectedCommitHash(self.c.Contexts().LocalCommits.GetSelectedCommitHash())

	repoState := self.c.State().GetRepoState()
	if repoState.GetScreenMode() == types.SCREEN_NORMAL {
		repoState.SetScreenMode(types.SCREEN_HALF)
	}

	if err := self.c.PushContext(self.c.Contexts().LocalCommits); err != nil {
		return err
	}

	return self.c.Refresh(types.RefreshOptions{Scope: []types.RefreshableView{types.COMMITS}, Then: func() {
		self.c.Contexts().LocalCommits.SetSelection(0)
		self.c.Contexts().LocalCommits.FocusLine()
	}})
}