1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-01-06 03:53:59 +02:00

Cancel filter/search when hitting escape

This commit is contained in:
Jesse Duffield 2023-05-27 20:38:49 +10:00
parent a9e2c8129f
commit 84870d4503
5 changed files with 28 additions and 8 deletions

View File

@ -211,7 +211,7 @@ func (self *ContextMgr) deactivateContext(c types.Context, opts types.OnFocusLos
} }
if filterableContext, ok := c.(types.IFilterableContext); ok { if filterableContext, ok := c.(types.IFilterableContext); ok {
if filterableContext.GetFilter() != "" { if filterableContext.IsFiltering() {
filterableContext.ClearFilter() filterableContext.ClearFilter()
self.gui.helpers.Search.Cancel() self.gui.helpers.Search.Cancel()
} }
@ -247,12 +247,12 @@ func (self *ContextMgr) ActivateContext(c types.Context, opts types.OnFocusOpts)
} }
if searchableContext, ok := c.(types.ISearchableContext); ok { if searchableContext, ok := c.(types.ISearchableContext); ok {
if searchableContext.GetSearchString() != "" { if searchableContext.IsSearching() {
self.gui.helpers.Search.DisplaySearchPrompt(searchableContext) self.gui.helpers.Search.DisplaySearchPrompt(searchableContext)
} }
} }
if filterableContext, ok := c.(types.IFilterableContext); ok { if filterableContext, ok := c.(types.IFilterableContext); ok {
if filterableContext.GetFilter() != "" { if filterableContext.IsFiltering() {
self.gui.helpers.Search.DisplayFilterPrompt(filterableContext) self.gui.helpers.Search.DisplayFilterPrompt(filterableContext)
} }
} }

View File

@ -68,3 +68,7 @@ func (self *SearchTrait) onSelectItemWrapper(innerFunc func(int) error) func(int
return nil return nil
} }
} }
func (self *SearchTrait) IsSearching() bool {
return self.searchString != ""
}

View File

@ -138,10 +138,6 @@ func (self *SearchHelper) ConfirmFilter() error {
func (self *SearchHelper) ConfirmSearch() error { func (self *SearchHelper) ConfirmSearch() error {
state := self.searchState() state := self.searchState()
if err := self.c.PopContext(); err != nil {
return err
}
context, ok := state.Context.(types.ISearchableContext) context, ok := state.Context.(types.ISearchableContext)
if !ok { if !ok {
self.c.Log.Warnf("Context %s is searchable", state.Context.GetKey()) self.c.Log.Warnf("Context %s is searchable", state.Context.GetKey())
@ -153,6 +149,10 @@ func (self *SearchHelper) ConfirmSearch() error {
view := context.GetView() view := context.GetView()
if err := self.c.PopContext(); err != nil {
return err
}
if err := view.Search(searchString); err != nil { if err := view.Search(searchString); err != nil {
return err return err
} }
@ -171,9 +171,10 @@ func (self *SearchHelper) Cancel() {
switch context := state.Context.(type) { switch context := state.Context.(type) {
case types.IFilterableContext: case types.IFilterableContext:
context.SetFilter("") context.ClearFilter()
_ = self.c.PostRefreshUpdate(context) _ = self.c.PostRefreshUpdate(context)
case types.ISearchableContext: case types.ISearchableContext:
context.ClearSearchString()
context.GetView().ClearSearch() context.GetView().ClearSearch()
default: default:
// do nothing // do nothing

View File

@ -50,6 +50,19 @@ func (self *QuitActions) confirmQuitDuringUpdate() error {
func (self *QuitActions) Escape() error { func (self *QuitActions) Escape() error {
currentContext := self.c.CurrentContext() currentContext := self.c.CurrentContext()
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, hasParent := currentContext.GetParentContext() parentContext, hasParent := currentContext.GetParentContext()
if hasParent && currentContext != nil && parentContext != nil { if hasParent && currentContext != nil && parentContext != nil {
// TODO: think about whether this should be marked as a return rather than adding to the stack // TODO: think about whether this should be marked as a return rather than adding to the stack

View File

@ -93,6 +93,7 @@ type IFilterableContext interface {
SetFilter(string) SetFilter(string)
GetFilter() string GetFilter() string
ClearFilter() ClearFilter()
IsFiltering() bool
IsFilterableContext() IsFilterableContext()
} }
@ -102,6 +103,7 @@ type ISearchableContext interface {
SetSearchString(string) SetSearchString(string)
GetSearchString() string GetSearchString() string
ClearSearchString() ClearSearchString()
IsSearching() bool
IsSearchableContext() IsSearchableContext()
} }