1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-08-09 09:22:48 +03:00

move getDisplayStrings funcs into contexts

This commit is contained in:
Jesse Duffield
2023-03-23 12:02:03 +11:00
parent 0c6ab4b43e
commit f081358943
11 changed files with 155 additions and 177 deletions

View File

@@ -1,8 +1,11 @@
package context package context
import ( import (
"github.com/jesseduffield/generics/slices"
"github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/gui/filetree" "github.com/jesseduffield/lazygit/pkg/gui/filetree"
"github.com/jesseduffield/lazygit/pkg/gui/presentation"
"github.com/jesseduffield/lazygit/pkg/gui/style"
"github.com/jesseduffield/lazygit/pkg/gui/types" "github.com/jesseduffield/lazygit/pkg/gui/types"
) )
@@ -17,17 +20,24 @@ var (
_ types.DiffableContext = (*CommitFilesContext)(nil) _ types.DiffableContext = (*CommitFilesContext)(nil)
) )
func NewCommitFilesContext( func NewCommitFilesContext(c *types.HelperCommon) *CommitFilesContext {
getDisplayStrings func(startIdx int, length int) [][]string,
c *types.HelperCommon,
) *CommitFilesContext {
viewModel := filetree.NewCommitFileTreeViewModel( viewModel := filetree.NewCommitFileTreeViewModel(
func() []*models.CommitFile { return c.Model().CommitFiles }, func() []*models.CommitFile { return c.Model().CommitFiles },
c.Log, c.Log,
c.UserConfig.Gui.ShowFileTree, c.UserConfig.Gui.ShowFileTree,
) )
getDisplayStrings := func(startIdx int, length int) [][]string {
if viewModel.Len() == 0 {
return [][]string{{style.FgRed.Sprint("(none)")}}
}
lines := presentation.RenderCommitFileTree(viewModel, c.Modes().Diffing.Ref, c.Git().Patch.PatchBuilder)
return slices.Map(lines, func(line string) []string {
return []string{line}
})
}
return &CommitFilesContext{ return &CommitFilesContext{
CommitFileTreeViewModel: viewModel, CommitFileTreeViewModel: viewModel,
DynamicTitleBuilder: NewDynamicTitleBuilder(c.Tr.CommitFilesDynamicTitle), DynamicTitleBuilder: NewDynamicTitleBuilder(c.Tr.CommitFilesDynamicTitle),

View File

@@ -1,7 +1,11 @@
package context package context
import ( import (
"log"
"github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/commands/types/enums"
"github.com/jesseduffield/lazygit/pkg/gui/presentation"
"github.com/jesseduffield/lazygit/pkg/gui/types" "github.com/jesseduffield/lazygit/pkg/gui/types"
) )
@@ -15,16 +19,41 @@ var (
_ types.DiffableContext = (*LocalCommitsContext)(nil) _ types.DiffableContext = (*LocalCommitsContext)(nil)
) )
func NewLocalCommitsContext( func NewLocalCommitsContext(c *types.HelperCommon) *LocalCommitsContext {
getDisplayStrings func(startIdx int, length int) [][]string,
c *types.HelperCommon,
) *LocalCommitsContext {
viewModel := NewLocalCommitsViewModel( viewModel := NewLocalCommitsViewModel(
func() []*models.Commit { return c.Model().Commits }, func() []*models.Commit { return c.Model().Commits },
c, c,
) )
getDisplayStrings := func(startIdx int, length int) [][]string {
selectedCommitSha := ""
if c.CurrentContext().GetKey() == LOCAL_COMMITS_CONTEXT_KEY {
selectedCommit := viewModel.GetSelected()
if selectedCommit != nil {
selectedCommitSha = selectedCommit.Sha
}
}
showYouAreHereLabel := c.Model().WorkingTreeStateAtLastCommitRefresh == enums.REBASE_MODE_REBASING
return presentation.GetCommitListDisplayStrings(
c.Common,
c.Model().Commits,
c.State().GetRepoState().GetScreenMode() != types.SCREEN_NORMAL,
c.Modes().CherryPicking.SelectedShaSet(),
c.Modes().Diffing.Ref,
c.UserConfig.Gui.TimeFormat,
c.UserConfig.Git.ParseEmoji,
selectedCommitSha,
startIdx,
length,
shouldShowGraph(c),
c.Model().BisectInfo,
showYouAreHereLabel,
)
}
return &LocalCommitsContext{ return &LocalCommitsContext{
LocalCommitsViewModel: viewModel, LocalCommitsViewModel: viewModel,
ViewportListContextTrait: &ViewportListContextTrait{ ViewportListContextTrait: &ViewportListContextTrait{
@@ -111,3 +140,22 @@ func (self *LocalCommitsViewModel) GetShowWholeGitGraph() bool {
func (self *LocalCommitsViewModel) GetCommits() []*models.Commit { func (self *LocalCommitsViewModel) GetCommits() []*models.Commit {
return self.getModel() return self.getModel()
} }
func shouldShowGraph(c *types.HelperCommon) bool {
if c.Modes().Filtering.Active() {
return false
}
value := c.UserConfig.Git.Log.ShowGraph
switch value {
case "always":
return true
case "never":
return false
case "when-maximised":
return c.State().GetRepoState().GetScreenMode() != types.SCREEN_NORMAL
}
log.Fatalf("Unknown value for git.log.showGraph: %s. Expected one of: 'always', 'never', 'when-maximised'", value)
return false
}

View File

@@ -2,6 +2,7 @@ package context
import ( import (
"github.com/jesseduffield/lazygit/pkg/commands/models" "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/gui/types"
) )
@@ -15,13 +16,20 @@ var (
_ types.DiffableContext = (*ReflogCommitsContext)(nil) _ types.DiffableContext = (*ReflogCommitsContext)(nil)
) )
func NewReflogCommitsContext( func NewReflogCommitsContext(c *types.HelperCommon) *ReflogCommitsContext {
getDisplayStrings func(startIdx int, length int) [][]string,
c *types.HelperCommon,
) *ReflogCommitsContext {
viewModel := NewBasicViewModel(func() []*models.Commit { return c.Model().FilteredReflogCommits }) viewModel := NewBasicViewModel(func() []*models.Commit { return c.Model().FilteredReflogCommits })
getDisplayStrings := func(startIdx int, length int) [][]string {
return presentation.GetReflogCommitListDisplayStrings(
c.Model().FilteredReflogCommits,
c.State().GetRepoState().GetScreenMode() != types.SCREEN_NORMAL,
c.Modes().CherryPicking.SelectedShaSet(),
c.Modes().Diffing.Ref,
c.UserConfig.Gui.TimeFormat,
c.UserConfig.Git.ParseEmoji,
)
}
return &ReflogCommitsContext{ return &ReflogCommitsContext{
BasicViewModel: viewModel, BasicViewModel: viewModel,
ListContextTrait: &ListContextTrait{ ListContextTrait: &ListContextTrait{

View File

@@ -2,6 +2,7 @@ package context
import ( import (
"github.com/jesseduffield/lazygit/pkg/commands/models" "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/gui/types"
) )
@@ -17,12 +18,14 @@ var (
) )
func NewRemoteBranchesContext( func NewRemoteBranchesContext(
getDisplayStrings func(startIdx int, length int) [][]string,
c *types.HelperCommon, c *types.HelperCommon,
) *RemoteBranchesContext { ) *RemoteBranchesContext {
viewModel := NewBasicViewModel(func() []*models.RemoteBranch { return c.Model().RemoteBranches }) viewModel := NewBasicViewModel(func() []*models.RemoteBranch { return c.Model().RemoteBranches })
getDisplayStrings := func(startIdx int, length int) [][]string {
return presentation.GetRemoteBranchListDisplayStrings(c.Model().RemoteBranches, c.Modes().Diffing.Ref)
}
return &RemoteBranchesContext{ return &RemoteBranchesContext{
BasicViewModel: viewModel, BasicViewModel: viewModel,
DynamicTitleBuilder: NewDynamicTitleBuilder(c.Tr.RemoteBranchesDynamicTitle), DynamicTitleBuilder: NewDynamicTitleBuilder(c.Tr.RemoteBranchesDynamicTitle),

View File

@@ -2,6 +2,7 @@ package context
import ( import (
"github.com/jesseduffield/lazygit/pkg/commands/models" "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/gui/types"
) )
@@ -15,13 +16,13 @@ var (
_ types.DiffableContext = (*RemotesContext)(nil) _ types.DiffableContext = (*RemotesContext)(nil)
) )
func NewRemotesContext( func NewRemotesContext(c *types.HelperCommon) *RemotesContext {
getDisplayStrings func(startIdx int, length int) [][]string,
c *types.HelperCommon,
) *RemotesContext {
viewModel := NewBasicViewModel(func() []*models.Remote { return c.Model().Remotes }) viewModel := NewBasicViewModel(func() []*models.Remote { return c.Model().Remotes })
getDisplayStrings := func(startIdx int, length int) [][]string {
return presentation.GetRemoteListDisplayStrings(c.Model().Remotes, c.Modes().Diffing.Ref)
}
return &RemotesContext{ return &RemotesContext{
BasicViewModel: viewModel, BasicViewModel: viewModel,
ListContextTrait: &ListContextTrait{ ListContextTrait: &ListContextTrait{

View File

@@ -2,6 +2,7 @@ package context
import ( import (
"github.com/jesseduffield/lazygit/pkg/commands/models" "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/gui/types"
) )
@@ -16,12 +17,14 @@ var (
) )
func NewStashContext( func NewStashContext(
getDisplayStrings func(startIdx int, length int) [][]string,
c *types.HelperCommon, c *types.HelperCommon,
) *StashContext { ) *StashContext {
viewModel := NewBasicViewModel(func() []*models.StashEntry { return c.Model().StashEntries }) viewModel := NewBasicViewModel(func() []*models.StashEntry { return c.Model().StashEntries })
getDisplayStrings := func(startIdx int, length int) [][]string {
return presentation.GetStashEntryListDisplayStrings(c.Model().StashEntries, c.Modes().Diffing.Ref)
}
return &StashContext{ return &StashContext{
BasicViewModel: viewModel, BasicViewModel: viewModel,
ListContextTrait: &ListContextTrait{ ListContextTrait: &ListContextTrait{

View File

@@ -3,7 +3,9 @@ package context
import ( import (
"fmt" "fmt"
"github.com/jesseduffield/lazygit/pkg/commands/git_commands"
"github.com/jesseduffield/lazygit/pkg/commands/models" "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/gui/types"
"github.com/jesseduffield/lazygit/pkg/utils" "github.com/jesseduffield/lazygit/pkg/utils"
) )
@@ -20,8 +22,6 @@ var (
) )
func NewSubCommitsContext( func NewSubCommitsContext(
getDisplayStrings func(startIdx int, length int) [][]string,
c *types.HelperCommon, c *types.HelperCommon,
) *SubCommitsContext { ) *SubCommitsContext {
viewModel := &SubCommitsViewModel{ viewModel := &SubCommitsViewModel{
@@ -32,6 +32,31 @@ func NewSubCommitsContext(
limitCommits: true, limitCommits: true,
} }
getDisplayStrings := func(startIdx int, length int) [][]string {
selectedCommitSha := ""
if c.CurrentContext().GetKey() == SUB_COMMITS_CONTEXT_KEY {
selectedCommit := viewModel.GetSelected()
if selectedCommit != nil {
selectedCommitSha = selectedCommit.Sha
}
}
return presentation.GetCommitListDisplayStrings(
c.Common,
c.Model().SubCommits,
c.State().GetRepoState().GetScreenMode() != types.SCREEN_NORMAL,
c.Modes().CherryPicking.SelectedShaSet(),
c.Modes().Diffing.Ref,
c.UserConfig.Gui.TimeFormat,
c.UserConfig.Git.ParseEmoji,
selectedCommitSha,
startIdx,
length,
shouldShowGraph(c),
git_commands.NewNullBisectInfo(),
false,
)
}
return &SubCommitsContext{ return &SubCommitsContext{
SubCommitsViewModel: viewModel, SubCommitsViewModel: viewModel,
DynamicTitleBuilder: NewDynamicTitleBuilder(c.Tr.SubCommitsDynamicTitle), DynamicTitleBuilder: NewDynamicTitleBuilder(c.Tr.SubCommitsDynamicTitle),

View File

@@ -2,6 +2,7 @@ package context
import ( import (
"github.com/jesseduffield/lazygit/pkg/commands/models" "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/gui/types"
) )
@@ -12,13 +13,13 @@ type SubmodulesContext struct {
var _ types.IListContext = (*SubmodulesContext)(nil) var _ types.IListContext = (*SubmodulesContext)(nil)
func NewSubmodulesContext( func NewSubmodulesContext(c *types.HelperCommon) *SubmodulesContext {
getDisplayStrings func(startIdx int, length int) [][]string,
c *types.HelperCommon,
) *SubmodulesContext {
viewModel := NewBasicViewModel(func() []*models.SubmoduleConfig { return c.Model().Submodules }) viewModel := NewBasicViewModel(func() []*models.SubmoduleConfig { return c.Model().Submodules })
getDisplayStrings := func(startIdx int, length int) [][]string {
return presentation.GetSubmoduleListDisplayStrings(c.Model().Submodules)
}
return &SubmodulesContext{ return &SubmodulesContext{
BasicViewModel: viewModel, BasicViewModel: viewModel,
ListContextTrait: &ListContextTrait{ ListContextTrait: &ListContextTrait{

View File

@@ -2,6 +2,7 @@ package context
import ( import (
"github.com/jesseduffield/lazygit/pkg/commands/models" "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/gui/types"
) )
@@ -16,12 +17,14 @@ var (
) )
func NewTagsContext( func NewTagsContext(
getDisplayStrings func(startIdx int, length int) [][]string,
c *types.HelperCommon, c *types.HelperCommon,
) *TagsContext { ) *TagsContext {
viewModel := NewBasicViewModel(func() []*models.Tag { return c.Model().Tags }) viewModel := NewBasicViewModel(func() []*models.Tag { return c.Model().Tags })
getDisplayStrings := func(startIdx int, length int) [][]string {
return presentation.GetTagListDisplayStrings(c.Model().Tags, c.Modes().Diffing.Ref)
}
return &TagsContext{ return &TagsContext{
BasicViewModel: viewModel, BasicViewModel: viewModel,
ListContextTrait: &ListContextTrait{ ListContextTrait: &ListContextTrait{

View File

@@ -10,13 +10,19 @@ import (
type controllerCommon struct { type controllerCommon struct {
c *types.HelperCommon c *types.HelperCommon
os *oscommands.OSCommand
git *commands.GitCommand
helpers *helpers.Helpers helpers *helpers.Helpers
model *types.Model
contexts *context.ContextTree contexts *context.ContextTree
modes *types.Modes
mutexes *types.Mutexes // TODO: use helperCommon's .OS() method instead of this
os *oscommands.OSCommand
// TODO: use helperCommon's .Git() method instead of this
git *commands.GitCommand
// TODO: use helperCommon's .Model() method instead of this
model *types.Model
// TODO: use helperCommon's .Modes() method instead of this
modes *types.Modes
// TODO: use helperCommon's .Mutexes() method instead of this
mutexes *types.Mutexes
} }
func NewControllerCommon( func NewControllerCommon(

View File

@@ -1,14 +1,7 @@
package gui package gui
import ( import (
"log"
"github.com/jesseduffield/generics/slices"
"github.com/jesseduffield/lazygit/pkg/commands/git_commands"
"github.com/jesseduffield/lazygit/pkg/commands/types/enums"
"github.com/jesseduffield/lazygit/pkg/gui/context" "github.com/jesseduffield/lazygit/pkg/gui/context"
"github.com/jesseduffield/lazygit/pkg/gui/presentation"
"github.com/jesseduffield/lazygit/pkg/gui/style"
"github.com/jesseduffield/lazygit/pkg/gui/types" "github.com/jesseduffield/lazygit/pkg/gui/types"
) )
@@ -25,162 +18,39 @@ func (gui *Gui) branchesListContext() *context.BranchesContext {
} }
func (gui *Gui) remotesListContext() *context.RemotesContext { func (gui *Gui) remotesListContext() *context.RemotesContext {
return context.NewRemotesContext( return context.NewRemotesContext(gui.c)
func(startIdx int, length int) [][]string {
return presentation.GetRemoteListDisplayStrings(gui.State.Model.Remotes, gui.State.Modes.Diffing.Ref)
},
gui.c,
)
} }
func (gui *Gui) remoteBranchesListContext() *context.RemoteBranchesContext { func (gui *Gui) remoteBranchesListContext() *context.RemoteBranchesContext {
return context.NewRemoteBranchesContext( return context.NewRemoteBranchesContext(gui.c)
func(startIdx int, length int) [][]string {
return presentation.GetRemoteBranchListDisplayStrings(gui.State.Model.RemoteBranches, gui.State.Modes.Diffing.Ref)
},
gui.c,
)
} }
func (gui *Gui) tagsListContext() *context.TagsContext { func (gui *Gui) tagsListContext() *context.TagsContext {
return context.NewTagsContext( return context.NewTagsContext(gui.c)
func(startIdx int, length int) [][]string {
return presentation.GetTagListDisplayStrings(gui.State.Model.Tags, gui.State.Modes.Diffing.Ref)
},
gui.c,
)
} }
func (gui *Gui) branchCommitsListContext() *context.LocalCommitsContext { func (gui *Gui) branchCommitsListContext() *context.LocalCommitsContext {
return context.NewLocalCommitsContext( return context.NewLocalCommitsContext(gui.c)
func(startIdx int, length int) [][]string {
selectedCommitSha := ""
if gui.c.CurrentContext().GetKey() == context.LOCAL_COMMITS_CONTEXT_KEY {
selectedCommit := gui.State.Contexts.LocalCommits.GetSelected()
if selectedCommit != nil {
selectedCommitSha = selectedCommit.Sha
}
}
showYouAreHereLabel := gui.State.Model.WorkingTreeStateAtLastCommitRefresh == enums.REBASE_MODE_REBASING
return presentation.GetCommitListDisplayStrings(
gui.Common,
gui.State.Model.Commits,
gui.State.ScreenMode != types.SCREEN_NORMAL,
gui.c.Modes().CherryPicking.SelectedShaSet(),
gui.State.Modes.Diffing.Ref,
gui.c.UserConfig.Gui.TimeFormat,
gui.c.UserConfig.Git.ParseEmoji,
selectedCommitSha,
startIdx,
length,
gui.shouldShowGraph(),
gui.State.Model.BisectInfo,
showYouAreHereLabel,
)
},
gui.c,
)
} }
func (gui *Gui) subCommitsListContext() *context.SubCommitsContext { func (gui *Gui) subCommitsListContext() *context.SubCommitsContext {
return context.NewSubCommitsContext( return context.NewSubCommitsContext(gui.c)
func(startIdx int, length int) [][]string {
selectedCommitSha := ""
if gui.c.CurrentContext().GetKey() == context.SUB_COMMITS_CONTEXT_KEY {
selectedCommit := gui.State.Contexts.SubCommits.GetSelected()
if selectedCommit != nil {
selectedCommitSha = selectedCommit.Sha
}
}
return presentation.GetCommitListDisplayStrings(
gui.Common,
gui.State.Model.SubCommits,
gui.State.ScreenMode != types.SCREEN_NORMAL,
gui.c.Modes().CherryPicking.SelectedShaSet(),
gui.State.Modes.Diffing.Ref,
gui.c.UserConfig.Gui.TimeFormat,
gui.c.UserConfig.Git.ParseEmoji,
selectedCommitSha,
startIdx,
length,
gui.shouldShowGraph(),
git_commands.NewNullBisectInfo(),
false,
)
},
gui.c,
)
}
func (gui *Gui) shouldShowGraph() bool {
if gui.State.Modes.Filtering.Active() {
return false
}
value := gui.c.UserConfig.Git.Log.ShowGraph
switch value {
case "always":
return true
case "never":
return false
case "when-maximised":
return gui.State.ScreenMode != types.SCREEN_NORMAL
}
log.Fatalf("Unknown value for git.log.showGraph: %s. Expected one of: 'always', 'never', 'when-maximised'", value)
return false
} }
func (gui *Gui) reflogCommitsListContext() *context.ReflogCommitsContext { func (gui *Gui) reflogCommitsListContext() *context.ReflogCommitsContext {
return context.NewReflogCommitsContext( return context.NewReflogCommitsContext(gui.c)
func(startIdx int, length int) [][]string {
return presentation.GetReflogCommitListDisplayStrings(
gui.State.Model.FilteredReflogCommits,
gui.State.ScreenMode != types.SCREEN_NORMAL,
gui.c.Modes().CherryPicking.SelectedShaSet(),
gui.State.Modes.Diffing.Ref,
gui.c.UserConfig.Gui.TimeFormat,
gui.c.UserConfig.Git.ParseEmoji,
)
},
gui.c,
)
} }
func (gui *Gui) stashListContext() *context.StashContext { func (gui *Gui) stashListContext() *context.StashContext {
return context.NewStashContext( return context.NewStashContext(gui.c)
func(startIdx int, length int) [][]string {
return presentation.GetStashEntryListDisplayStrings(gui.State.Model.StashEntries, gui.State.Modes.Diffing.Ref)
},
gui.c,
)
} }
func (gui *Gui) commitFilesListContext() *context.CommitFilesContext { func (gui *Gui) commitFilesListContext() *context.CommitFilesContext {
return context.NewCommitFilesContext( return context.NewCommitFilesContext(gui.c)
func(startIdx int, length int) [][]string {
if gui.State.Contexts.CommitFiles.CommitFileTreeViewModel.Len() == 0 {
return [][]string{{style.FgRed.Sprint("(none)")}}
}
lines := presentation.RenderCommitFileTree(gui.State.Contexts.CommitFiles.CommitFileTreeViewModel, gui.State.Modes.Diffing.Ref, gui.git.Patch.PatchBuilder)
return slices.Map(lines, func(line string) []string {
return []string{line}
})
},
gui.c,
)
} }
func (gui *Gui) submodulesListContext() *context.SubmodulesContext { func (gui *Gui) submodulesListContext() *context.SubmodulesContext {
return context.NewSubmodulesContext( return context.NewSubmodulesContext(gui.c)
func(startIdx int, length int) [][]string {
return presentation.GetSubmoduleListDisplayStrings(gui.State.Model.Submodules)
},
gui.c,
)
} }
func (gui *Gui) suggestionsListContext() *context.SuggestionsContext { func (gui *Gui) suggestionsListContext() *context.SuggestionsContext {