Skip to content

Commit 6a6316c

Browse files
committed
Use model searching in commits (and sub-commits) view
1 parent 779e6f9 commit 6a6316c

File tree

75 files changed

+9198
-4307
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+9198
-4307
lines changed

go.mod

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ require (
1616
github.com/integrii/flaggy v1.4.0
1717
github.com/jesseduffield/generics v0.0.0-20220320043834-727e535cbe68
1818
github.com/jesseduffield/go-git/v5 v5.1.2-0.20221018185014-fdd53fef665d
19-
github.com/jesseduffield/gocui v0.3.1-0.20240418080333-8cd33929c513
19+
github.com/jesseduffield/gocui v0.3.1-0.20240623092910-a42926c14fc9
2020
github.com/jesseduffield/kill v0.0.0-20220618033138-bfbe04675d10
2121
github.com/jesseduffield/lazycore v0.0.0-20221012050358-03d2e40243c5
2222
github.com/jesseduffield/minimal/gitignore v0.3.3-0.20211018110810-9cde264e6b1e
@@ -75,8 +75,8 @@ require (
7575
github.com/xanzy/ssh-agent v0.2.1 // indirect
7676
golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa // indirect
7777
golang.org/x/net v0.7.0 // indirect
78-
golang.org/x/sys v0.19.0 // indirect
79-
golang.org/x/term v0.19.0 // indirect
80-
golang.org/x/text v0.14.0 // indirect
78+
golang.org/x/sys v0.21.0 // indirect
79+
golang.org/x/term v0.21.0 // indirect
80+
golang.org/x/text v0.16.0 // indirect
8181
gopkg.in/warnings.v0 v0.1.2 // indirect
8282
)

go.sum

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,8 @@ github.com/jesseduffield/generics v0.0.0-20220320043834-727e535cbe68 h1:EQP2Tv8T
188188
github.com/jesseduffield/generics v0.0.0-20220320043834-727e535cbe68/go.mod h1:+LLj9/WUPAP8LqCchs7P+7X0R98HiFujVFANdNaxhGk=
189189
github.com/jesseduffield/go-git/v5 v5.1.2-0.20221018185014-fdd53fef665d h1:bO+OmbreIv91rCe8NmscRwhFSqkDJtzWCPV4Y+SQuXE=
190190
github.com/jesseduffield/go-git/v5 v5.1.2-0.20221018185014-fdd53fef665d/go.mod h1:nGNEErzf+NRznT+N2SWqmHnDnF9aLgANB1CUNEan09o=
191-
github.com/jesseduffield/gocui v0.3.1-0.20240418080333-8cd33929c513 h1:Y1bw5iItrsDCumATc/rklIJ/6K+68ieiWZJedhrNuXo=
192-
github.com/jesseduffield/gocui v0.3.1-0.20240418080333-8cd33929c513/go.mod h1:XtEbqCbn45keRXEu+OMZkjN5gw6AEob59afsgHjokZ8=
191+
github.com/jesseduffield/gocui v0.3.1-0.20240623092910-a42926c14fc9 h1:JJ0DrXgAUpGBGV5w8nzrQLMWTgcTvf745IKAk08qjcM=
192+
github.com/jesseduffield/gocui v0.3.1-0.20240623092910-a42926c14fc9/go.mod h1:XtEbqCbn45keRXEu+OMZkjN5gw6AEob59afsgHjokZ8=
193193
github.com/jesseduffield/kill v0.0.0-20220618033138-bfbe04675d10 h1:jmpr7KpX2+2GRiE91zTgfq49QvgiqB0nbmlwZ8UnOx0=
194194
github.com/jesseduffield/kill v0.0.0-20220618033138-bfbe04675d10/go.mod h1:aA97kHeNA+sj2Hbki0pvLslmE4CbDyhBeSSTUUnOuVo=
195195
github.com/jesseduffield/lazycore v0.0.0-20221012050358-03d2e40243c5 h1:CDuQmfOjAtb1Gms6a1p5L2P8RhbLUq5t8aL7PiQd2uY=
@@ -474,14 +474,14 @@ golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBc
474474
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
475475
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
476476
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
477-
golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o=
478-
golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
477+
golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws=
478+
golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
479479
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
480480
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
481481
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
482482
golang.org/x/term v0.17.0/go.mod h1:lLRBjIVuehSbZlaOtGMbcMncT+aqLLLmKrsjNrUguwk=
483-
golang.org/x/term v0.19.0 h1:+ThwsDv+tYfnJFhF4L8jITxu1tdTWRTZpdsWgEgjL6Q=
484-
golang.org/x/term v0.19.0/go.mod h1:2CuTdWZ7KHSQwUzKva0cbMg6q2DMI3Mmxp+gKJbskEk=
483+
golang.org/x/term v0.21.0 h1:WVXCp+/EBEHOj53Rvu+7KiT/iElMrO8ACK16SMZ3jaA=
484+
golang.org/x/term v0.21.0/go.mod h1:ooXLefLobQVslOqselCNF4SxFAaoS6KujMbsGzSDmX0=
485485
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
486486
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
487487
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
@@ -491,8 +491,9 @@ golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
491491
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
492492
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
493493
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
494-
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
495494
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
495+
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
496+
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
496497
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
497498
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
498499
golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=

pkg/gui/context/commit_files_context.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package context
22

33
import (
4+
"github.com/jesseduffield/gocui"
45
"github.com/jesseduffield/lazygit/pkg/commands/models"
56
"github.com/jesseduffield/lazygit/pkg/gui/filetree"
67
"github.com/jesseduffield/lazygit/pkg/gui/presentation"
@@ -73,3 +74,7 @@ func NewCommitFilesContext(c *ContextCommon) *CommitFilesContext {
7374
func (self *CommitFilesContext) GetDiffTerminals() []string {
7475
return []string{self.GetRef().RefName()}
7576
}
77+
78+
func (self *CommitFilesContext) ModelSearchResults(searchStr string, caseSensitive bool) []gocui.SearchPosition {
79+
return nil
80+
}

pkg/gui/context/list_renderer.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ type ListRenderer struct {
3535
numNonModelItems int
3636
viewIndicesByModelIndex []int
3737
modelIndicesByViewIndex []int
38+
columnPositions []int
3839
}
3940

4041
func (self *ListRenderer) GetList() types.IList {
@@ -59,6 +60,10 @@ func (self *ListRenderer) ViewIndexToModelIndex(viewIndex int) int {
5960
return viewIndex
6061
}
6162

63+
func (self *ListRenderer) ColumnPositions() []int {
64+
return self.columnPositions
65+
}
66+
6267
// startIdx and endIdx are view indices, not model indices. If you want to
6368
// render the whole list, pass -1 for both.
6469
func (self *ListRenderer) renderLines(startIdx int, endIdx int) string {
@@ -87,6 +92,7 @@ func (self *ListRenderer) renderLines(startIdx int, endIdx int) string {
8792
lines, columnPositions := utils.RenderDisplayStrings(
8893
self.getDisplayStrings(startModelIdx, endModelIdx),
8994
columnAlignments)
95+
self.columnPositions = columnPositions
9096
lines = self.insertNonModelItems(nonModelItems, endIdx, startIdx, lines, columnPositions)
9197
return strings.Join(lines, "\n")
9298
}

pkg/gui/context/local_commits_context.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ package context
22

33
import (
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"
@@ -153,6 +155,10 @@ func (self *LocalCommitsContext) GetDiffTerminals() []string {
153155
return []string{itemId}
154156
}
155157

158+
func (self *LocalCommitsContext) ModelSearchResults(searchStr string, caseSensitive bool) []gocui.SearchPosition {
159+
return searchModelCommits(caseSensitive, self.GetCommits(), self.ColumnPositions(), searchStr)
160+
}
161+
156162
func (self *LocalCommitsViewModel) SetLimitCommits(value bool) {
157163
self.limitCommits = value
158164
}
@@ -192,3 +198,18 @@ func shouldShowGraph(c *ContextCommon) bool {
192198
log.Fatalf("Unknown value for git.log.showGraph: %s. Expected one of: 'always', 'never', 'when-maximised'", value)
193199
return false
194200
}
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+
}

pkg/gui/context/patch_explorer_context.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,3 +142,7 @@ func (self *PatchExplorerContext) NavigateTo(isFocused bool, selectedLineIdx int
142142
func (self *PatchExplorerContext) GetMutex() *deadlock.Mutex {
143143
return self.mutex
144144
}
145+
146+
func (self *PatchExplorerContext) ModelSearchResults(searchStr string, caseSensitive bool) []gocui.SearchPosition {
147+
return nil
148+
}

pkg/gui/context/sub_commits_context.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"time"
66

7+
"github.com/jesseduffield/gocui"
78
"github.com/jesseduffield/lazygit/pkg/commands/git_commands"
89
"github.com/jesseduffield/lazygit/pkg/commands/models"
910
"github.com/jesseduffield/lazygit/pkg/gui/presentation"
@@ -200,3 +201,7 @@ func (self *SubCommitsContext) GetDiffTerminals() []string {
200201

201202
return []string{itemId}
202203
}
204+
205+
func (self *SubCommitsContext) ModelSearchResults(searchStr string, caseSensitive bool) []gocui.SearchPosition {
206+
return searchModelCommits(caseSensitive, self.GetCommits(), self.ColumnPositions(), searchStr)
207+
}

pkg/gui/context/working_tree_context.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package context
22

33
import (
4+
"github.com/jesseduffield/gocui"
45
"github.com/jesseduffield/lazygit/pkg/commands/models"
56
"github.com/jesseduffield/lazygit/pkg/gui/filetree"
67
"github.com/jesseduffield/lazygit/pkg/gui/presentation"
@@ -58,3 +59,7 @@ func NewWorkingTreeContext(c *ContextCommon) *WorkingTreeContext {
5859

5960
return ctx
6061
}
62+
63+
func (self *WorkingTreeContext) ModelSearchResults(searchStr string, caseSensitive bool) []gocui.SearchPosition {
64+
return nil
65+
}

pkg/gui/controllers/helpers/refresh_helper.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -759,6 +759,14 @@ func (self *RefreshHelper) refForLog() string {
759759
}
760760

761761
func (self *RefreshHelper) refreshView(context types.Context) error {
762+
// Re-applying the filter must be done before re-rendering the view, so that
763+
// the filtered list model is up to date for rendering.
762764
self.searchHelper.ReApplyFilter(context)
763-
return self.c.PostRefreshUpdate(context)
765+
766+
err := self.c.PostRefreshUpdate(context)
767+
768+
// Re-applying the search must be done after re-rendering the view though,
769+
// so that the "x of y" status is shown correctly.
770+
self.searchHelper.ReApplySearch(context)
771+
return err
764772
}

pkg/gui/controllers/helpers/search_helper.go

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ package helpers
22

33
import (
44
"fmt"
5+
"strings"
56

67
"github.com/jesseduffield/gocui"
78
"github.com/jesseduffield/lazygit/pkg/gui/context"
89
"github.com/jesseduffield/lazygit/pkg/gui/keybindings"
910
"github.com/jesseduffield/lazygit/pkg/gui/types"
1011
"github.com/jesseduffield/lazygit/pkg/theme"
12+
"github.com/jesseduffield/lazygit/pkg/utils"
1113
)
1214

1315
// NOTE: this helper supports both filtering and searching. Filtering is when
@@ -156,17 +158,26 @@ func (self *SearchHelper) ConfirmSearch() error {
156158
context.GetSearchHistory().Push(searchString)
157159
}
158160

159-
view := context.GetView()
160-
161161
if err := self.c.PopContext(); err != nil {
162162
return err
163163
}
164164

165-
if err := view.Search(searchString); err != nil {
166-
return err
165+
return context.GetView().Search(searchString, modelSearchResults(context))
166+
}
167+
168+
func modelSearchResults(context types.ISearchableContext) []gocui.SearchPosition {
169+
searchString := context.GetSearchString()
170+
171+
var normalizedSearchStr string
172+
// if we have any uppercase characters we'll do a case-sensitive search
173+
caseSensitive := utils.ContainsUppercase(searchString)
174+
if caseSensitive {
175+
normalizedSearchStr = searchString
176+
} else {
177+
normalizedSearchStr = strings.ToLower(searchString)
167178
}
168179

169-
return nil
180+
return context.ModelSearchResults(normalizedSearchStr, caseSensitive)
170181
}
171182

172183
func (self *SearchHelper) CancelPrompt() error {
@@ -239,6 +250,25 @@ func (self *SearchHelper) ReApplyFilter(context types.Context) {
239250
}
240251
}
241252

253+
func (self *SearchHelper) ReApplySearch(ctx types.Context) {
254+
// Reapply the search if the model has changed. This is needed for contexts
255+
// that use the model for searching, to pass the new model search positions
256+
// to the view.
257+
searchableContext, ok := ctx.(types.ISearchableContext)
258+
if ok {
259+
ctx.GetView().UpdateSearchResults(searchableContext.GetSearchString(), modelSearchResults(searchableContext))
260+
261+
state := self.searchState()
262+
if ctx == state.Context {
263+
// Re-render the "x of y" search status, unless the search prompt is
264+
// open for typing.
265+
if self.c.CurrentContext().GetKey() != context.SEARCH_CONTEXT_KEY {
266+
self.RenderSearchStatus(searchableContext)
267+
}
268+
}
269+
}
270+
}
271+
242272
func (self *SearchHelper) RenderSearchStatus(c types.Context) {
243273
if c.GetKey() == context.SEARCH_CONTEXT_KEY {
244274
return

0 commit comments

Comments
 (0)