mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-03-17 21:18:31 +02:00
move filtering menu action to controller
This commit is contained in:
parent
2cba98e3fe
commit
7848958326
78
pkg/gui/controllers/filtering_menu_action.go
Normal file
78
pkg/gui/controllers/filtering_menu_action.go
Normal file
@ -0,0 +1,78 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/jesseduffield/lazygit/pkg/gui/types"
|
||||
)
|
||||
|
||||
type FilteringMenuAction struct {
|
||||
c *ControllerCommon
|
||||
}
|
||||
|
||||
func (self *FilteringMenuAction) Call() error {
|
||||
fileName := ""
|
||||
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()
|
||||
}
|
||||
}
|
||||
|
||||
menuItems := []*types.MenuItem{}
|
||||
|
||||
if fileName != "" {
|
||||
menuItems = append(menuItems, &types.MenuItem{
|
||||
Label: fmt.Sprintf("%s '%s'", self.c.Tr.LcFilterBy, fileName),
|
||||
OnPress: func() error {
|
||||
return self.setFiltering(fileName)
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
menuItems = append(menuItems, &types.MenuItem{
|
||||
Label: self.c.Tr.LcFilterPathOption,
|
||||
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))
|
||||
},
|
||||
})
|
||||
},
|
||||
})
|
||||
|
||||
if self.c.Modes().Filtering.Active() {
|
||||
menuItems = append(menuItems, &types.MenuItem{
|
||||
Label: self.c.Tr.LcExitFilterMode,
|
||||
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
|
||||
}
|
||||
|
||||
return self.c.Refresh(types.RefreshOptions{Scope: []types.RefreshableView{types.COMMITS}, Then: func() {
|
||||
self.c.Contexts().LocalCommits.SetSelectedLineIdx(0)
|
||||
}})
|
||||
}
|
@ -68,6 +68,13 @@ func (self *GlobalController) GetKeybindings(opts types.KeybindingsOpts) []*type
|
||||
Description: self.c.Tr.LcOpenMenu,
|
||||
Handler: self.createOptionsMenu,
|
||||
},
|
||||
{
|
||||
ViewName: "",
|
||||
Key: opts.GetKey(opts.Config.Universal.FilteringMenu),
|
||||
Handler: self.createFilteringMenu,
|
||||
Description: self.c.Tr.LcOpenFilteringMenu,
|
||||
OpensMenu: true,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@ -98,3 +105,7 @@ func (self *GlobalController) prevScreenMode() error {
|
||||
func (self *GlobalController) createOptionsMenu() error {
|
||||
return (&OptionsMenuAction{c: self.c}).Call()
|
||||
}
|
||||
|
||||
func (self *GlobalController) createFilteringMenu() error {
|
||||
return (&FilteringMenuAction{c: self.c}).Call()
|
||||
}
|
||||
|
@ -16,18 +16,3 @@ func (gui *Gui) validateNotInFilterMode() bool {
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (gui *Gui) setFiltering(path string) error {
|
||||
gui.State.Modes.Filtering.SetPath(path)
|
||||
if gui.State.ScreenMode == types.SCREEN_NORMAL {
|
||||
gui.State.ScreenMode = types.SCREEN_HALF
|
||||
}
|
||||
|
||||
if err := gui.c.PushContext(gui.State.Contexts.LocalCommits); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return gui.c.Refresh(types.RefreshOptions{Scope: []types.RefreshableView{types.COMMITS}, Then: func() {
|
||||
gui.State.Contexts.LocalCommits.SetSelectedLineIdx(0)
|
||||
}})
|
||||
}
|
||||
|
@ -1,57 +0,0 @@
|
||||
package gui
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/jesseduffield/lazygit/pkg/gui/types"
|
||||
)
|
||||
|
||||
func (gui *Gui) handleCreateFilteringMenuPanel() error {
|
||||
fileName := ""
|
||||
switch gui.c.CurrentSideContext() {
|
||||
case gui.State.Contexts.Files:
|
||||
node := gui.State.Contexts.Files.GetSelected()
|
||||
if node != nil {
|
||||
fileName = node.GetPath()
|
||||
}
|
||||
case gui.State.Contexts.CommitFiles:
|
||||
node := gui.State.Contexts.CommitFiles.GetSelected()
|
||||
if node != nil {
|
||||
fileName = node.GetPath()
|
||||
}
|
||||
}
|
||||
|
||||
menuItems := []*types.MenuItem{}
|
||||
|
||||
if fileName != "" {
|
||||
menuItems = append(menuItems, &types.MenuItem{
|
||||
Label: fmt.Sprintf("%s '%s'", gui.c.Tr.LcFilterBy, fileName),
|
||||
OnPress: func() error {
|
||||
return gui.setFiltering(fileName)
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
menuItems = append(menuItems, &types.MenuItem{
|
||||
Label: gui.c.Tr.LcFilterPathOption,
|
||||
OnPress: func() error {
|
||||
return gui.c.Prompt(types.PromptOpts{
|
||||
FindSuggestionsFunc: gui.helpers.Suggestions.GetFilePathSuggestionsFunc(),
|
||||
Title: gui.c.Tr.EnterFileName,
|
||||
HandleConfirm: func(response string) error {
|
||||
return gui.setFiltering(strings.TrimSpace(response))
|
||||
},
|
||||
})
|
||||
},
|
||||
})
|
||||
|
||||
if gui.State.Modes.Filtering.Active() {
|
||||
menuItems = append(menuItems, &types.MenuItem{
|
||||
Label: gui.c.Tr.LcExitFilterMode,
|
||||
OnPress: gui.helpers.Mode.ClearFiltering,
|
||||
})
|
||||
}
|
||||
|
||||
return gui.c.Menu(types.CreateMenuOptions{Title: gui.c.Tr.FilteringMenuTitle, Items: menuItems})
|
||||
}
|
@ -188,13 +188,6 @@ func (self *Gui) GetInitialKeybindings() ([]*types.Binding, []*gocui.ViewMouseBi
|
||||
Handler: self.handleCopySelectedSideContextItemToClipboard,
|
||||
Description: self.c.Tr.LcCopyCommitFileNameToClipboard,
|
||||
},
|
||||
{
|
||||
ViewName: "",
|
||||
Key: opts.GetKey(opts.Config.Universal.FilteringMenu),
|
||||
Handler: self.handleCreateFilteringMenuPanel,
|
||||
Description: self.c.Tr.LcOpenFilteringMenu,
|
||||
OpensMenu: true,
|
||||
},
|
||||
{
|
||||
ViewName: "",
|
||||
Key: opts.GetKey(opts.Config.Universal.DiffingMenu),
|
||||
|
Loading…
x
Reference in New Issue
Block a user