Repository: jesseduffield/lazygit
File: pkg/gui/controllers/filtering_menu_action.go (Go)

Code before:
		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.setFiltering(strings.TrimSpace(response))
				},
			})
		},
	})

	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))
				},
			})
		},
	})

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

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

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

	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
	}

Review comment (suggestion):
This causes a linter warning ("wastedassign"). It could be avoided by using `lo.Ternary`.

I wonder, however, if we really need two different texts. I think I'd rather reword the existing text to just "Stop filtering" and use it for both cases.

If you do insist on having different texts, then I'd rename `ExitFilterMode` to `ExitFilterModePath`.

Code after:
			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)