mirror of
https://github.com/jesseduffield/lazygit.git
synced 2026-09-24 07:25:20 -05:00
Use model searching in commits (and sub-commits) view
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
|
||||
@@ -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
|
||||
})
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user