1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-12-14 11:23:09 +02:00
lazygit/pkg/gui/context/search_trait.go
2023-07-03 12:54:13 +10:00

75 lines
1.6 KiB
Go

package context
import (
"fmt"
"github.com/jesseduffield/lazygit/pkg/gui/keybindings"
"github.com/jesseduffield/lazygit/pkg/theme"
)
type SearchTrait struct {
c *ContextCommon
searchString string
}
func NewSearchTrait(c *ContextCommon) *SearchTrait {
return &SearchTrait{c: c}
}
func (self *SearchTrait) GetSearchString() string {
return self.searchString
}
func (self *SearchTrait) SetSearchString(searchString string) {
self.searchString = searchString
}
func (self *SearchTrait) ClearSearchString() {
self.SetSearchString("")
}
// used for type switch
func (self *SearchTrait) IsSearchableContext() {}
func (self *SearchTrait) onSelectItemWrapper(innerFunc func(int) error) func(int, int, int) error {
keybindingConfig := self.c.UserConfig.Keybinding
return func(y int, index int, total int) error {
if total == 0 {
self.c.SetViewContent(
self.c.Views().Search,
fmt.Sprintf(
self.c.Tr.NoMatchesFor,
self.searchString,
theme.OptionsFgColor.Sprintf(self.c.Tr.ExitSearchMode, keybindings.Label(keybindingConfig.Universal.Return)),
),
)
return nil
}
self.c.SetViewContent(
self.c.Views().Search,
fmt.Sprintf(
self.c.Tr.MatchesFor,
self.searchString,
index+1,
total,
theme.OptionsFgColor.Sprintf(
self.c.Tr.SearchKeybindings,
keybindings.Label(keybindingConfig.Universal.NextMatch),
keybindings.Label(keybindingConfig.Universal.PrevMatch),
keybindings.Label(keybindingConfig.Universal.Return),
),
),
)
if err := innerFunc(y); err != nil {
return err
}
return nil
}
}
func (self *SearchTrait) IsSearching() bool {
return self.searchString != ""
}