diff --git a/pkg/gui/context/branches_context.go b/pkg/gui/context/branches_context.go index bf90446ae..ac1fae52c 100644 --- a/pkg/gui/context/branches_context.go +++ b/pkg/gui/context/branches_context.go @@ -45,9 +45,11 @@ func NewBranchesContext(c *ContextCommon) *BranchesContext { Kind: types.SIDE_CONTEXT, Focusable: true, })), - list: viewModel, - getDisplayStrings: getDisplayStrings, - c: c, + ListRenderer: ListRenderer{ + list: viewModel, + getDisplayStrings: getDisplayStrings, + }, + c: c, }, } diff --git a/pkg/gui/context/commit_files_context.go b/pkg/gui/context/commit_files_context.go index 2229cf2d6..037554c91 100644 --- a/pkg/gui/context/commit_files_context.go +++ b/pkg/gui/context/commit_files_context.go @@ -54,9 +54,11 @@ func NewCommitFilesContext(c *ContextCommon) *CommitFilesContext { Transient: true, }), ), - list: viewModel, - getDisplayStrings: getDisplayStrings, - c: c, + ListRenderer: ListRenderer{ + list: viewModel, + getDisplayStrings: getDisplayStrings, + }, + c: c, }, } diff --git a/pkg/gui/context/list_context_trait.go b/pkg/gui/context/list_context_trait.go index 7cb13b0aa..df178e567 100644 --- a/pkg/gui/context/list_context_trait.go +++ b/pkg/gui/context/list_context_trait.go @@ -4,17 +4,13 @@ import ( "fmt" "github.com/jesseduffield/lazygit/pkg/gui/types" - "github.com/jesseduffield/lazygit/pkg/utils" ) type ListContextTrait struct { types.Context + ListRenderer - c *ContextCommon - list types.IList - getDisplayStrings func(startIdx int, endIdx int) [][]string - // Alignment for each column. If nil, the default is left alignment - getColumnAlignments func() []utils.Alignment + c *ContextCommon // Some contexts, like the commit context, will highlight the path from the selected commit // to its parents, because it's ambiguous otherwise. For these, we need to refresh the viewport // so that we show the highlighted path. @@ -26,10 +22,6 @@ type ListContextTrait struct { func (self *ListContextTrait) IsListContext() {} -func (self *ListContextTrait) GetList() types.IList { - return self.list -} - func (self *ListContextTrait) FocusLine() { // Doing this at the end of the layout function because we need the view to be // resized before we focus the line, otherwise if we're in accordion mode @@ -57,16 +49,6 @@ func (self *ListContextTrait) FocusLine() { } } -func (self *ListContextTrait) renderLines(startIdx int, endIdx int) string { - var columnAlignments []utils.Alignment - if self.getColumnAlignments != nil { - columnAlignments = self.getColumnAlignments() - } - return utils.RenderDisplayStrings( - self.getDisplayStrings(startIdx, utils.Min(endIdx, self.list.Len())), - columnAlignments) -} - func (self *ListContextTrait) refreshViewport() { startIdx, length := self.GetViewTrait().ViewPortYBounds() content := self.renderLines(startIdx, startIdx+length) diff --git a/pkg/gui/context/list_renderer.go b/pkg/gui/context/list_renderer.go new file mode 100644 index 000000000..e324d8eb6 --- /dev/null +++ b/pkg/gui/context/list_renderer.go @@ -0,0 +1,27 @@ +package context + +import ( + "github.com/jesseduffield/lazygit/pkg/gui/types" + "github.com/jesseduffield/lazygit/pkg/utils" +) + +type ListRenderer struct { + list types.IList + getDisplayStrings func(startIdx int, endIdx int) [][]string + // Alignment for each column. If nil, the default is left alignment + getColumnAlignments func() []utils.Alignment +} + +func (self *ListRenderer) GetList() types.IList { + return self.list +} + +func (self *ListRenderer) renderLines(startIdx int, endIdx int) string { + var columnAlignments []utils.Alignment + if self.getColumnAlignments != nil { + columnAlignments = self.getColumnAlignments() + } + return utils.RenderDisplayStrings( + self.getDisplayStrings(startIdx, utils.Min(endIdx, self.list.Len())), + columnAlignments) +} diff --git a/pkg/gui/context/local_commits_context.go b/pkg/gui/context/local_commits_context.go index e34e04249..fa91e6a79 100644 --- a/pkg/gui/context/local_commits_context.go +++ b/pkg/gui/context/local_commits_context.go @@ -74,8 +74,10 @@ func NewLocalCommitsContext(c *ContextCommon) *LocalCommitsContext { Kind: types.SIDE_CONTEXT, Focusable: true, })), - list: viewModel, - getDisplayStrings: getDisplayStrings, + ListRenderer: ListRenderer{ + list: viewModel, + getDisplayStrings: getDisplayStrings, + }, c: c, refreshViewportOnChange: true, }, diff --git a/pkg/gui/context/menu_context.go b/pkg/gui/context/menu_context.go index c9dda81f4..30fe8a4b4 100644 --- a/pkg/gui/context/menu_context.go +++ b/pkg/gui/context/menu_context.go @@ -34,10 +34,12 @@ func NewMenuContext( Focusable: true, HasUncontrolledBounds: true, })), - getDisplayStrings: viewModel.GetDisplayStrings, - list: viewModel, - c: c, - getColumnAlignments: func() []utils.Alignment { return viewModel.columnAlignment }, + ListRenderer: ListRenderer{ + list: viewModel, + getDisplayStrings: viewModel.GetDisplayStrings, + getColumnAlignments: func() []utils.Alignment { return viewModel.columnAlignment }, + }, + c: c, }, } } diff --git a/pkg/gui/context/reflog_commits_context.go b/pkg/gui/context/reflog_commits_context.go index baaa1e154..a90507e86 100644 --- a/pkg/gui/context/reflog_commits_context.go +++ b/pkg/gui/context/reflog_commits_context.go @@ -49,9 +49,11 @@ func NewReflogCommitsContext(c *ContextCommon) *ReflogCommitsContext { Kind: types.SIDE_CONTEXT, Focusable: true, })), - list: viewModel, - getDisplayStrings: getDisplayStrings, - c: c, + ListRenderer: ListRenderer{ + list: viewModel, + getDisplayStrings: getDisplayStrings, + }, + c: c, }, } } diff --git a/pkg/gui/context/remote_branches_context.go b/pkg/gui/context/remote_branches_context.go index ef519ab71..144a8c369 100644 --- a/pkg/gui/context/remote_branches_context.go +++ b/pkg/gui/context/remote_branches_context.go @@ -43,9 +43,11 @@ func NewRemoteBranchesContext( Focusable: true, Transient: true, })), - list: viewModel, - getDisplayStrings: getDisplayStrings, - c: c, + ListRenderer: ListRenderer{ + list: viewModel, + getDisplayStrings: getDisplayStrings, + }, + c: c, }, } } diff --git a/pkg/gui/context/remotes_context.go b/pkg/gui/context/remotes_context.go index 2720d139e..035fb2321 100644 --- a/pkg/gui/context/remotes_context.go +++ b/pkg/gui/context/remotes_context.go @@ -38,9 +38,11 @@ func NewRemotesContext(c *ContextCommon) *RemotesContext { Kind: types.SIDE_CONTEXT, Focusable: true, })), - list: viewModel, - getDisplayStrings: getDisplayStrings, - c: c, + ListRenderer: ListRenderer{ + list: viewModel, + getDisplayStrings: getDisplayStrings, + }, + c: c, }, } } diff --git a/pkg/gui/context/stash_context.go b/pkg/gui/context/stash_context.go index 01768c6d1..2b86d945f 100644 --- a/pkg/gui/context/stash_context.go +++ b/pkg/gui/context/stash_context.go @@ -40,9 +40,11 @@ func NewStashContext( Kind: types.SIDE_CONTEXT, Focusable: true, })), - list: viewModel, - getDisplayStrings: getDisplayStrings, - c: c, + ListRenderer: ListRenderer{ + list: viewModel, + getDisplayStrings: getDisplayStrings, + }, + c: c, }, } } diff --git a/pkg/gui/context/sub_commits_context.go b/pkg/gui/context/sub_commits_context.go index 048d91f0e..43cc513c0 100644 --- a/pkg/gui/context/sub_commits_context.go +++ b/pkg/gui/context/sub_commits_context.go @@ -93,8 +93,10 @@ func NewSubCommitsContext( Focusable: true, Transient: true, })), - list: viewModel, - getDisplayStrings: getDisplayStrings, + ListRenderer: ListRenderer{ + list: viewModel, + getDisplayStrings: getDisplayStrings, + }, c: c, refreshViewportOnChange: true, }, diff --git a/pkg/gui/context/submodules_context.go b/pkg/gui/context/submodules_context.go index f5018987d..2cffd82d6 100644 --- a/pkg/gui/context/submodules_context.go +++ b/pkg/gui/context/submodules_context.go @@ -35,9 +35,11 @@ func NewSubmodulesContext(c *ContextCommon) *SubmodulesContext { Kind: types.SIDE_CONTEXT, Focusable: true, })), - list: viewModel, - getDisplayStrings: getDisplayStrings, - c: c, + ListRenderer: ListRenderer{ + list: viewModel, + getDisplayStrings: getDisplayStrings, + }, + c: c, }, } } diff --git a/pkg/gui/context/suggestions_context.go b/pkg/gui/context/suggestions_context.go index c4442400c..e3c1f5f26 100644 --- a/pkg/gui/context/suggestions_context.go +++ b/pkg/gui/context/suggestions_context.go @@ -54,9 +54,11 @@ func NewSuggestionsContext( Focusable: true, HasUncontrolledBounds: true, })), - list: viewModel, - getDisplayStrings: getDisplayStrings, - c: c, + ListRenderer: ListRenderer{ + list: viewModel, + getDisplayStrings: getDisplayStrings, + }, + c: c, }, } } diff --git a/pkg/gui/context/tags_context.go b/pkg/gui/context/tags_context.go index 777a68099..4a9f525f6 100644 --- a/pkg/gui/context/tags_context.go +++ b/pkg/gui/context/tags_context.go @@ -40,9 +40,11 @@ func NewTagsContext( Kind: types.SIDE_CONTEXT, Focusable: true, })), - list: viewModel, - getDisplayStrings: getDisplayStrings, - c: c, + ListRenderer: ListRenderer{ + list: viewModel, + getDisplayStrings: getDisplayStrings, + }, + c: c, }, } } diff --git a/pkg/gui/context/working_tree_context.go b/pkg/gui/context/working_tree_context.go index 36b1f1adb..0e0b8d72b 100644 --- a/pkg/gui/context/working_tree_context.go +++ b/pkg/gui/context/working_tree_context.go @@ -41,9 +41,11 @@ func NewWorkingTreeContext(c *ContextCommon) *WorkingTreeContext { Kind: types.SIDE_CONTEXT, Focusable: true, })), - list: viewModel, - getDisplayStrings: getDisplayStrings, - c: c, + ListRenderer: ListRenderer{ + list: viewModel, + getDisplayStrings: getDisplayStrings, + }, + c: c, }, } diff --git a/pkg/gui/context/worktrees_context.go b/pkg/gui/context/worktrees_context.go index a5a30e990..c616dd49e 100644 --- a/pkg/gui/context/worktrees_context.go +++ b/pkg/gui/context/worktrees_context.go @@ -38,9 +38,11 @@ func NewWorktreesContext(c *ContextCommon) *WorktreesContext { Kind: types.SIDE_CONTEXT, Focusable: true, })), - list: viewModel, - getDisplayStrings: getDisplayStrings, - c: c, + ListRenderer: ListRenderer{ + list: viewModel, + getDisplayStrings: getDisplayStrings, + }, + c: c, }, } }