1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-07-30 03:23:08 +03:00

many more generics

This commit is contained in:
Jesse Duffield
2022-03-19 19:12:58 +11:00
parent bf4f06ab4e
commit 1b75ed3740
31 changed files with 278 additions and 320 deletions

View File

@ -4,6 +4,7 @@ import (
"fmt"
"strings"
"github.com/jesseduffield/generics/slices"
"github.com/jesseduffield/lazygit/pkg/commands"
"github.com/jesseduffield/lazygit/pkg/commands/git_commands"
"github.com/jesseduffield/lazygit/pkg/commands/types/enums"
@ -51,17 +52,14 @@ func (self *MergeAndRebaseHelper) CreateRebaseOptionsMenu() error {
options = append(options, REBASE_OPTION_SKIP)
}
menuItems := make([]*types.MenuItem, len(options))
for i, option := range options {
// note to self. Never, EVER, close over loop variables in a function
option := option
menuItems[i] = &types.MenuItem{
menuItems := slices.Map(options, func(option string) *types.MenuItem {
return &types.MenuItem{
DisplayString: option,
OnPress: func() error {
return self.genericMergeCommand(option)
},
}
}
})
var title string
if self.git.Status.WorkingTreeState() == enums.REBASE_MODE_MERGING {

View File

@ -4,6 +4,7 @@ import (
"fmt"
"strings"
"github.com/jesseduffield/generics/slices"
"github.com/jesseduffield/lazygit/pkg/commands"
"github.com/jesseduffield/lazygit/pkg/commands/git_commands"
"github.com/jesseduffield/lazygit/pkg/commands/models"
@ -134,10 +135,8 @@ func (self *RefsHelper) ResetToRef(ref string, strength string, envVars []string
func (self *RefsHelper) CreateGitResetMenu(ref string) error {
strengths := []string{"soft", "mixed", "hard"}
menuItems := make([]*types.MenuItem, len(strengths))
for i, strength := range strengths {
strength := strength
menuItems[i] = &types.MenuItem{
menuItems := slices.Map(strengths, func(strength string) *types.MenuItem {
return &types.MenuItem{
DisplayStrings: []string{
fmt.Sprintf("%s reset", strength),
style.FgRed.Sprintf("reset --%s %s", strength, ref),
@ -147,7 +146,7 @@ func (self *RefsHelper) CreateGitResetMenu(ref string) error {
return self.ResetToRef(ref, strength, []string{})
},
}
}
})
return self.c.Menu(types.CreateMenuOptions{
Title: fmt.Sprintf("%s %s", self.c.Tr.LcResetTo, ref),

View File

@ -4,6 +4,8 @@ import (
"fmt"
"os"
"github.com/jesseduffield/generics/slices"
"github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/gui/presentation"
"github.com/jesseduffield/lazygit/pkg/gui/types"
"github.com/jesseduffield/lazygit/pkg/utils"
@ -51,22 +53,18 @@ func NewSuggestionsHelper(
}
func (self *SuggestionsHelper) getRemoteNames() []string {
result := make([]string, len(self.model.Remotes))
for i, remote := range self.model.Remotes {
result[i] = remote.Name
}
return result
return slices.Map(self.model.Remotes, func(remote *models.Remote) string {
return remote.Name
})
}
func matchesToSuggestions(matches []string) []*types.Suggestion {
suggestions := make([]*types.Suggestion, len(matches))
for i, match := range matches {
suggestions[i] = &types.Suggestion{
return slices.Map(matches, func(match string) *types.Suggestion {
return &types.Suggestion{
Value: match,
Label: match,
}
}
return suggestions
})
}
func (self *SuggestionsHelper) GetRemoteSuggestionsFunc() func(string) []*types.Suggestion {
@ -76,11 +74,9 @@ func (self *SuggestionsHelper) GetRemoteSuggestionsFunc() func(string) []*types.
}
func (self *SuggestionsHelper) getBranchNames() []string {
result := make([]string, len(self.model.Branches))
for i, branch := range self.model.Branches {
result[i] = branch.Name
}
return result
return slices.Map(self.model.Branches, func(branch *models.Branch) string {
return branch.Name
})
}
func (self *SuggestionsHelper) GetBranchNameSuggestionsFunc() func(string) []*types.Suggestion {
@ -94,15 +90,12 @@ func (self *SuggestionsHelper) GetBranchNameSuggestionsFunc() func(string) []*ty
matchingBranchNames = utils.FuzzySearch(input, branchNames)
}
suggestions := make([]*types.Suggestion, len(matchingBranchNames))
for i, branchName := range matchingBranchNames {
suggestions[i] = &types.Suggestion{
return slices.Map(matchingBranchNames, func(branchName string) *types.Suggestion {
return &types.Suggestion{
Value: branchName,
Label: presentation.GetBranchTextStyle(branchName).Sprint(branchName),
}
}
return suggestions
})
}
}
@ -148,26 +141,16 @@ func (self *SuggestionsHelper) GetFilePathSuggestionsFunc() func(string) []*type
// doing another fuzzy search for good measure
matchingNames = utils.FuzzySearch(input, matchingNames)
suggestions := make([]*types.Suggestion, len(matchingNames))
for i, name := range matchingNames {
suggestions[i] = &types.Suggestion{
Value: name,
Label: name,
}
}
return suggestions
return matchesToSuggestions(matchingNames)
}
}
func (self *SuggestionsHelper) getRemoteBranchNames(separator string) []string {
result := []string{}
for _, remote := range self.model.Remotes {
for _, branch := range remote.Branches {
result = append(result, fmt.Sprintf("%s%s%s", remote.Name, separator, branch.Name))
}
}
return result
return slices.FlatMap(self.model.Remotes, func(remote *models.Remote) []string {
return slices.Map(remote.Branches, func(branch *models.RemoteBranch) string {
return fmt.Sprintf("%s%s%s", remote.Name, separator, branch.Name)
})
})
}
func (self *SuggestionsHelper) GetRemoteBranchesSuggestionsFunc(separator string) func(string) []*types.Suggestion {
@ -175,11 +158,9 @@ func (self *SuggestionsHelper) GetRemoteBranchesSuggestionsFunc(separator string
}
func (self *SuggestionsHelper) getTagNames() []string {
result := make([]string, len(self.model.Tags))
for i, tag := range self.model.Tags {
result[i] = tag.Name
}
return result
return slices.Map(self.model.Tags, func(tag *models.Tag) string {
return tag.Name
})
}
func (self *SuggestionsHelper) GetRefsSuggestionsFunc() func(string) []*types.Suggestion {