@@ -2,8 +2,10 @@ package context
22
33import (
44 "log"
5+ "strings"
56 "time"
67
8+ "github.com/jesseduffield/gocui"
79 "github.com/jesseduffield/lazygit/pkg/commands/models"
810 "github.com/jesseduffield/lazygit/pkg/commands/types/enums"
911 "github.com/jesseduffield/lazygit/pkg/gui/presentation"
@@ -18,8 +20,9 @@ type LocalCommitsContext struct {
1820}
1921
2022var (
21- _ types.IListContext = (* LocalCommitsContext )(nil )
22- _ types.DiffableContext = (* LocalCommitsContext )(nil )
23+ _ types.IListContext = (* LocalCommitsContext )(nil )
24+ _ types.DiffableContext = (* LocalCommitsContext )(nil )
25+ _ types.ISearchableContext = (* LocalCommitsContext )(nil )
2326)
2427
2528func NewLocalCommitsContext (c * ContextCommon ) * LocalCommitsContext {
@@ -85,10 +88,7 @@ func NewLocalCommitsContext(c *ContextCommon) *LocalCommitsContext {
8588 },
8689 }
8790
88- ctx .GetView ().SetOnSelectItem (ctx .SearchTrait .onSelectItemWrapper (func (selectedLineIdx int ) error {
89- ctx .GetList ().SetSelection (selectedLineIdx )
90- return ctx .HandleFocus (types.OnFocusOpts {})
91- }))
91+ ctx .GetView ().SetOnSelectItem (ctx .SearchTrait .onSelectItemWrapper (ctx .OnSearchSelect ))
9292
9393 return ctx
9494}
@@ -155,6 +155,10 @@ func (self *LocalCommitsContext) GetDiffTerminals() []string {
155155 return []string {itemId }
156156}
157157
158+ func (self * LocalCommitsContext ) ModelSearchResults (searchStr string , caseSensitive bool ) []gocui.SearchPosition {
159+ return searchModelCommits (caseSensitive , self .GetCommits (), self .ColumnPositions (), searchStr )
160+ }
161+
158162func (self * LocalCommitsViewModel ) SetLimitCommits (value bool ) {
159163 self .limitCommits = value
160164}
@@ -194,3 +198,18 @@ func shouldShowGraph(c *ContextCommon) bool {
194198 log .Fatalf ("Unknown value for git.log.showGraph: %s. Expected one of: 'always', 'never', 'when-maximised'" , value )
195199 return false
196200}
201+
202+ func searchModelCommits (caseSensitive bool , commits []* models.Commit , columnPositions []int , searchStr string ) []gocui.SearchPosition {
203+ normalize := lo .Ternary (caseSensitive , func (s string ) string { return s }, strings .ToLower )
204+ return lo .FilterMap (commits , func (commit * models.Commit , idx int ) (gocui.SearchPosition , bool ) {
205+ // The XStart and XEnd values are only used if the search string can't
206+ // be found in the view. This can really only happen if the user is
207+ // searching for a commit hash that is longer than the truncated hash
208+ // that we render. So we just set the XStart and XEnd values to the
209+ // start and end of the commit hash column, which is the second one.
210+ result := gocui.SearchPosition {XStart : columnPositions [1 ], XEnd : columnPositions [2 ] - 1 , Y : idx }
211+ return result , strings .Contains (normalize (commit .Hash ), searchStr ) ||
212+ strings .Contains (normalize (commit .Name ), searchStr ) ||
213+ strings .Contains (normalize (commit .ExtraInfo ), searchStr ) // allow searching for tags
214+ })
215+ }
0 commit comments