1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-01-20 05:19:24 +02: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
import (
"github.com/jesseduffield/generics/slices"
"github.com/jesseduffield/lazygit/pkg/commands/models"
"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"
)
@ -17,17 +20,24 @@ var (
_ types.DiffableContext = (*CommitFilesContext)(nil)
)
func NewCommitFilesContext(
getDisplayStrings func(startIdx int, length int) [][]string,
c *types.HelperCommon,
) *CommitFilesContext {
func NewCommitFilesContext(c *types.HelperCommon) *CommitFilesContext {
viewModel := filetree.NewCommitFileTreeViewModel(
func() []*models.CommitFile { return c.Model().CommitFiles },
c.Log,
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{
CommitFileTreeViewModel: viewModel,
DynamicTitleBuilder: NewDynamicTitleBuilder(c.Tr.CommitFilesDynamicTitle),

View File

@ -1,7 +1,11 @@
package context
import (
"log"
"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"
)
@ -15,16 +19,41 @@ var (
_ types.DiffableContext = (*LocalCommitsContext)(nil)
)
func NewLocalCommitsContext(
getDisplayStrings func(startIdx int, length int) [][]string,
c *types.HelperCommon,
) *LocalCommitsContext {
func NewLocalCommitsContext(c *types.HelperCommon) *LocalCommitsContext {
viewModel := NewLocalCommitsViewModel(
func() []*models.Commit { return c.Model().Commits },
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{
LocalCommitsViewModel: viewModel,
ViewportListContextTrait: &ViewportListContextTrait{
@ -111,3 +140,22 @@ func (self *LocalCommitsViewModel) GetShowWholeGitGraph() bool {
func (self *LocalCommitsViewModel) GetCommits() []*models.Commit {
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 (
"github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/gui/presentation"
"github.com/jesseduffield/lazygit/pkg/gui/types"
)
@ -15,13 +16,20 @@ var (
_ types.DiffableContext = (*ReflogCommitsContext)(nil)
)
func NewReflogCommitsContext(
getDisplayStrings func(startIdx int, length int) [][]string,
c *types.HelperCommon,
) *ReflogCommitsContext {
func NewReflogCommitsContext(c *types.HelperCommon) *ReflogCommitsContext {
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{
BasicViewModel: viewModel,
ListContextTrait: &ListContextTrait{

View File

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

View File

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

View File

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

View File

@ -3,7 +3,9 @@ package context
import (
"fmt"
"github.com/jesseduffield/lazygit/pkg/commands/git_commands"
"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"
)
@ -20,8 +22,6 @@ var (
)
func NewSubCommitsContext(
getDisplayStrings func(startIdx int, length int) [][]string,
c *types.HelperCommon,
) *SubCommitsContext {
viewModel := &SubCommitsViewModel{
@ -32,6 +32,31 @@ func NewSubCommitsContext(
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{
SubCommitsViewModel: viewModel,
DynamicTitleBuilder: NewDynamicTitleBuilder(c.Tr.SubCommitsDynamicTitle),

View File

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

View File

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

View File

@ -10,13 +10,19 @@ import (
type controllerCommon struct {
c *types.HelperCommon
os *oscommands.OSCommand
git *commands.GitCommand
helpers *helpers.Helpers
model *types.Model
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(

View File

@ -1,14 +1,7 @@
package gui
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/presentation"
"github.com/jesseduffield/lazygit/pkg/gui/style"
"github.com/jesseduffield/lazygit/pkg/gui/types"
)
@ -25,162 +18,39 @@ func (gui *Gui) branchesListContext() *context.BranchesContext {
}
func (gui *Gui) remotesListContext() *context.RemotesContext {
return context.NewRemotesContext(
func(startIdx int, length int) [][]string {
return presentation.GetRemoteListDisplayStrings(gui.State.Model.Remotes, gui.State.Modes.Diffing.Ref)
},
gui.c,
)
return context.NewRemotesContext(gui.c)
}
func (gui *Gui) remoteBranchesListContext() *context.RemoteBranchesContext {
return context.NewRemoteBranchesContext(
func(startIdx int, length int) [][]string {
return presentation.GetRemoteBranchListDisplayStrings(gui.State.Model.RemoteBranches, gui.State.Modes.Diffing.Ref)
},
gui.c,
)
return context.NewRemoteBranchesContext(gui.c)
}
func (gui *Gui) tagsListContext() *context.TagsContext {
return context.NewTagsContext(
func(startIdx int, length int) [][]string {
return presentation.GetTagListDisplayStrings(gui.State.Model.Tags, gui.State.Modes.Diffing.Ref)
},
gui.c,
)
return context.NewTagsContext(gui.c)
}
func (gui *Gui) branchCommitsListContext() *context.LocalCommitsContext {
return context.NewLocalCommitsContext(
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,
)
return context.NewLocalCommitsContext(gui.c)
}
func (gui *Gui) subCommitsListContext() *context.SubCommitsContext {
return context.NewSubCommitsContext(
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
return context.NewSubCommitsContext(gui.c)
}
func (gui *Gui) reflogCommitsListContext() *context.ReflogCommitsContext {
return context.NewReflogCommitsContext(
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,
)
return context.NewReflogCommitsContext(gui.c)
}
func (gui *Gui) stashListContext() *context.StashContext {
return context.NewStashContext(
func(startIdx int, length int) [][]string {
return presentation.GetStashEntryListDisplayStrings(gui.State.Model.StashEntries, gui.State.Modes.Diffing.Ref)
},
gui.c,
)
return context.NewStashContext(gui.c)
}
func (gui *Gui) commitFilesListContext() *context.CommitFilesContext {
return context.NewCommitFilesContext(
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,
)
return context.NewCommitFilesContext(gui.c)
}
func (gui *Gui) submodulesListContext() *context.SubmodulesContext {
return context.NewSubmodulesContext(
func(startIdx int, length int) [][]string {
return presentation.GetSubmoduleListDisplayStrings(gui.State.Model.Submodules)
},
gui.c,
)
return context.NewSubmodulesContext(gui.c)
}
func (gui *Gui) suggestionsListContext() *context.SuggestionsContext {