summaryrefslogtreecommitdiffstats
path: root/pkg
diff options
context:
space:
mode:
authorStefan Haller <stefan@haller-berlin.de>2024-05-31 17:24:50 +0200
committerStefan Haller <stefan@haller-berlin.de>2024-06-23 11:43:12 +0200
commit6a6316cfb632a28627e067c8a4d30786acffee38 (patch)
treee9092175bcdac6fbd3df73fffb9c72fe37d54ffa /pkg
parent779e6f95a3fca869fb886201305217deb2dca8f5 (diff)
Use model searching in commits (and sub-commits) view
Diffstat (limited to 'pkg')
-rw-r--r--pkg/gui/context/commit_files_context.go5
-rw-r--r--pkg/gui/context/list_renderer.go6
-rw-r--r--pkg/gui/context/local_commits_context.go21
-rw-r--r--pkg/gui/context/patch_explorer_context.go4
-rw-r--r--pkg/gui/context/sub_commits_context.go5
-rw-r--r--pkg/gui/context/working_tree_context.go5
-rw-r--r--pkg/gui/controllers/helpers/refresh_helper.go10
-rw-r--r--pkg/gui/controllers/helpers/search_helper.go40
-rw-r--r--pkg/gui/types/context.go4
9 files changed, 94 insertions, 6 deletions
diff --git a/pkg/gui/context/commit_files_context.go b/pkg/gui/context/commit_files_context.go
index d6df67a10..b56798fea 100644
--- a/pkg/gui/context/commit_files_context.go
+++ b/pkg/gui/context/commit_files_context.go
@@ -1,6 +1,7 @@
package context
import (
+ "github.com/jesseduffield/gocui"
"github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/gui/filetree"
"github.com/jesseduffield/lazygit/pkg/gui/presentation"
@@ -73,3 +74,7 @@ func NewCommitFilesContext(c *ContextCommon) *CommitFilesContext {
func (self *CommitFilesContext) GetDiffTerminals() []string {
return []string{self.GetRef().RefName()}
}
+
+func (self *CommitFilesContext) ModelSearchResults(searchStr string, caseSensitive bool) []gocui.SearchPosition {
+ return nil
+}
diff --git a/pkg/gui/context/list_renderer.go b/pkg/gui/context/list_renderer.go
index f29407055..b89eccf8b 100644
--- a/pkg/gui/context/list_renderer.go
+++ b/pkg/gui/context/list_renderer.go
@@ -35,6 +35,7 @@ type ListRenderer struct {
numNonModelItems int
viewIndicesByModelIndex []int
modelIndicesByViewIndex []int
+ columnPositions []int
}
func (self *ListRenderer) GetList() types.IList {
@@ -59,6 +60,10 @@ func (self *ListRenderer) ViewIndexToModelIndex(viewIndex int) int {
return viewIndex
}
+func (self *ListRenderer) ColumnPositions() []int {
+ return self.columnPositions
+}
+
// startIdx and endIdx are view indices, not model indices. If you want to
// render the whole list, pass -1 for both.
func (self *ListRenderer) renderLines(startIdx int, endIdx int) string {
@@ -87,6 +92,7 @@ func (self *ListRenderer) renderLines(startIdx int, endIdx int) string {
lines, columnPositions := utils.RenderDisplayStrings(
self.getDisplayStrings(startModelIdx, endModelIdx),
columnAlignments)
+ self.columnPositions = columnPositions
lines = self.insertNonModelItems(nonModelItems, endIdx, startIdx, lines, columnPositions)
return strings.Join(lines, "\n")
}
diff --git a/pkg/gui/context/local_commits_context.go b/pkg/gui/context/local_commits_context.go
index d92f71ff6..c02fa3fe6 100644
--- a/pkg/gui/context/local_commits_context.go
+++ b/pkg/gui/context/local_commits_context.go
@@ -2,8 +2,10 @@ package context
import (
"log"
+ "strings"
"time"
+ "github.com/jesseduffield/gocui"
"github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/commands/types/enums"
"github.com/jesseduffield/lazygit/pkg/gui/presentation"
@@ -153,6 +155,10 @@ func (self *LocalCommitsContext) GetDiffTerminals() []string {
return []string{itemId}
}
+func (self *LocalCommitsContext) ModelSearchResults(searchStr string, caseSensitive bool) []gocui.SearchPosition {
+ return searchModelCommits(caseSensitive, self.GetCommits(), self.ColumnPositions(), searchStr)
+}
+
func (self *LocalCommitsViewModel) SetLimitCommits(value bool) {
self.limitCommits = value
}
@@ -192,3 +198,18 @@ func shouldShowGraph(c *ContextCommon) bool {
log.Fatalf("Unknown value for git.log.showGraph: %s. Expected one of: 'always', 'never', 'when-maximised'", value)
return false
}
+
+func searchModelCommits(caseSensitive bool, commits []*models.Commit, columnPositions []int, searchStr string) []gocui.SearchPosition {
+ normalize := lo.Ternary(caseSensitive, func(s string) string { return s }, strings.ToLower)
+ return lo.FilterMap(commits, func(commit *models.Commit, idx int) (gocui.SearchPosition, bool) {
+ // The XStart and XEnd values are only used if the search string can't
+ // be found in the view. This can really only happen if the user is
+ // searching for a commit hash that is longer than the truncated hash
+ // that we render. So we just set the XStart and XEnd values to the
+ // start and end of the commit hash column, which is the second one.
+ result := gocui.SearchPosition{XStart: columnPositions[1], XEnd: columnPositions[2] - 1, Y: idx}
+ return result, strings.Contains(normalize(commit.Hash), searchStr) ||
+ strings.Contains(normalize(commit.Name), searchStr) ||
+ strings.Contains(normalize(commit.ExtraInfo), searchStr) // allow searching for tags
+ })
+}
diff --git a/pkg/gui/context/patch_explorer_context.go b/pkg/gui/context/patch_explorer_context.go
index dac66fa03..ee856c5d2 100644
--- a/pkg/gui/context/patch_explorer_context.go
+++ b/pkg/gui/context/patch_explorer_context.go
@@ -142,3 +142,7 @@ func (self *PatchExplorerContext) NavigateTo(isFocused bool, selectedLineIdx int
func (self *PatchExplorerContext) GetMutex() *deadlock.Mutex {
return self.mutex
}
+
+func (self *PatchExplorerContext) ModelSearchResults(searchStr string, caseSensitive bool) []gocui.SearchPosition {
+ return nil
+}
diff --git a/pkg/gui/context/sub_commits_context.go b/pkg/gui/context/sub_commits_context.go
index 8e2b5ace1..842b364b3 100644
--- a/pkg/gui/context/sub_commits_context.go
+++ b/pkg/gui/context/sub_commits_context.go
@@ -4,6 +4,7 @@ import (
"fmt"
"time"
+ "github.com/jesseduffield/gocui"
"github.com/jesseduffield/lazygit/pkg/commands/git_commands"
"github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/gui/presentation"
@@ -200,3 +201,7 @@ func (self *SubCommitsContext) GetDiffTerminals() []string {
return []string{itemId}
}
+
+func (self *SubCommitsContext) ModelSearchResults(searchStr string, caseSensitive bool) []gocui.SearchPosition {
+ return searchModelCommits(caseSensitive, self.GetCommits(), self.ColumnPositions(), searchStr)
+}
diff --git a/pkg/gui/context/working_tree_context.go b/pkg/gui/context/working_tree_context.go
index 17ff31de3..76eb56d57 100644
--- a/pkg/gui/context/working_tree_context.go
+++ b/pkg/gui/context/working_tree_context.go
@@ -1,6 +1,7 @@
package context
import (
+ "github.com/jesseduffield/gocui"
"github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/gui/filetree"
"github.com/jesseduffield/lazygit/pkg/gui/presentation"
@@ -58,3 +59,7 @@ func NewWorkingTreeContext(c *ContextCommon) *WorkingTreeContext {
return ctx
}
+
+func (self *WorkingTreeContext) ModelSearchResults(searchStr string, caseSensitive bool) []gocui.SearchPosition {
+ return nil
+}
diff --git a/pkg/gui/controllers/helpers/refresh_helper.go b/pkg/gui/controllers/helpers/refresh_helper.go
index 4872add08..21c20c646 100644
--- a/pkg/gui/controllers/helpers/refresh_helper.go
+++ b/pkg/gui/controllers/helpers/refresh_helper.go
@@ -759,6 +759,14 @@ func (self *RefreshHelper) refForLog() string {
}
func (self *RefreshHelper) refreshView(context types.Context) error {
+ // Re-applying the filter must be done before re-rendering the view, so that
+ // the filtered list model is up to date for rendering.
self.searchHelper.ReApplyFilter(context)
- return self.c.PostRefreshUpdate(context)
+
+ err := self.c.PostRefreshUpdate(context)
+
+ // Re-applying the search must be done after re-rendering the view though,
+ // so that the "x of y" status is shown correctly.
+ self.searchHelper.ReApplySearch(context)
+ return err
}
diff --git a/pkg/gui/controllers/helpers/search_helper.go b/pkg/gui/controllers/helpers/search_helper.go
index c31720949..2cffe418b 100644
--- a/pkg/gui/controllers/helpers/search_helper.go
+++ b/pkg/gui/controllers/helpers/search_helper.go
@@ -2,12 +2,14 @@ package helpers
import (
"fmt"
+ "strings"
"github.com/jesseduffield/gocui"
"github.com/jesseduffield/lazygit/pkg/gui/context"
"github.com/jesseduffield/lazygit/pkg/gui/keybindings"
"github.com/jesseduffield/lazygit/pkg/gui/types"
"github.com/jesseduffield/lazygit/pkg/theme"
+ "github.com/jesseduffield/lazygit/pkg/utils"
)
// NOTE: this helper supports both filtering and searching. Filtering is when
@@ -156,17 +158,26 @@ func (self *SearchHelper) ConfirmSearch() error {
context.GetSearchHistory().Push(searchString)
}
- view := context.GetView()
-
if err := self.c.PopContext(); err != nil {
return err
}
- if err := view.Search(searchString); err != nil {
- return err
+ return context.GetView().Search(searchString, modelSearchResults(context))
+}
+
+func modelSearchResults(context types.ISearchableContext) []gocui.SearchPosition {
+ searchString := context.GetSearchString()
+
+ var normalizedSearchStr string
+ // if we have any uppercase characters we'll do a case-sensitive search
+ caseSensitive := utils.ContainsUppercase(searchString)
+ if caseSensitive {
+ normalizedSearchStr = searchString
+ } else {
+ normalizedSearchStr = strings.ToLower(searchString)
}
- return nil
+ return context.ModelSearchResults(normalizedSearchStr, caseSensitive)
}
func (self *SearchHelper) CancelPrompt() error {
@@ -239,6 +250,25 @@ func (self *SearchHelper) ReApplyFilter(context types.Context) {
}
}
+func (self *SearchHelper) ReApplySearch(ctx types.Context) {
+ // Reapply the search if the model has changed. This is needed for contexts
+ // that use the model for searching, to pass the new model search positions
+ // to the view.
+ searchableContext, ok := ctx.(types.ISearchableContext)
+ if ok {
+ ctx.GetView().UpdateSearchResults(searchableContext.GetSearchString(), modelSearchResults(searchableContext))
+
+ state := self.searchState()
+ if ctx == state.Context {
+ // Re-render the "x of y" search status, unless the search prompt is
+ // open for typing.
+ if self.c.CurrentContext().GetKey() != context.SEARCH_CONTEXT_KEY {
+ self.RenderSearchStatus(searchableContext)
+ }
+ }
+ }
+}
+
func (self *SearchHelper) RenderSearchStatus(c types.Context) {
if c.GetKey() == context.SEARCH_CONTEXT_KEY {
return
diff --git a/pkg/gui/types/context.go b/pkg/gui/types/context.go
index 63c759eb6..b0e312c97 100644
--- a/pkg/gui/types/context.go
+++ b/pkg/gui/types/context.go
@@ -114,12 +114,16 @@ type ISearchableContext interface {
Context
ISearchHistoryContext
+ // These are all implemented by SearchTrait
SetSearchString(string)
GetSearchString() string
ClearSearchString()
IsSearching() bool
IsSearchableContext()
RenderSearchStatus(int, int)
+
+ // This must be implemented by each concrete context. Return nil if not searching the model.
+ ModelSearchResults(searchStr string, caseSensitive bool) []gocui.SearchPosition
}
type DiffableContext interface {