Skip to content

Commit

Permalink
Extract a ListRenderer struct
Browse files Browse the repository at this point in the history
I'm doing this not so much because it's a great abstraction, but just because it
will make it much easier to write tests for it.
  • Loading branch information
stefanhaller committed Aug 28, 2023
1 parent 297a020 commit 198ead7
Show file tree
Hide file tree
Showing 16 changed files with 98 additions and 61 deletions.
8 changes: 5 additions & 3 deletions pkg/gui/context/branches_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
}

Expand Down
8 changes: 5 additions & 3 deletions pkg/gui/context/commit_files_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
}

Expand Down
22 changes: 2 additions & 20 deletions pkg/gui/context/list_context_trait.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down
27 changes: 27 additions & 0 deletions pkg/gui/context/list_renderer.go
Original file line number Diff line number Diff line change
@@ -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)
}
6 changes: 4 additions & 2 deletions pkg/gui/context/local_commits_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down
10 changes: 6 additions & 4 deletions pkg/gui/context/menu_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
}
}
Expand Down
8 changes: 5 additions & 3 deletions pkg/gui/context/reflog_commits_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
}
}
Expand Down
8 changes: 5 additions & 3 deletions pkg/gui/context/remote_branches_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,11 @@ func NewRemoteBranchesContext(
Focusable: true,
Transient: true,
})),
list: viewModel,
getDisplayStrings: getDisplayStrings,
c: c,
ListRenderer: ListRenderer{
list: viewModel,
getDisplayStrings: getDisplayStrings,
},
c: c,
},
}
}
Expand Down
8 changes: 5 additions & 3 deletions pkg/gui/context/remotes_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
}
}
Expand Down
8 changes: 5 additions & 3 deletions pkg/gui/context/stash_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
}
}
Expand Down
6 changes: 4 additions & 2 deletions pkg/gui/context/sub_commits_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,10 @@ func NewSubCommitsContext(
Focusable: true,
Transient: true,
})),
list: viewModel,
getDisplayStrings: getDisplayStrings,
ListRenderer: ListRenderer{
list: viewModel,
getDisplayStrings: getDisplayStrings,
},
c: c,
refreshViewportOnChange: true,
},
Expand Down
8 changes: 5 additions & 3 deletions pkg/gui/context/submodules_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
}
}
Expand Down
8 changes: 5 additions & 3 deletions pkg/gui/context/suggestions_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,11 @@ func NewSuggestionsContext(
Focusable: true,
HasUncontrolledBounds: true,
})),
list: viewModel,
getDisplayStrings: getDisplayStrings,
c: c,
ListRenderer: ListRenderer{
list: viewModel,
getDisplayStrings: getDisplayStrings,
},
c: c,
},
}
}
Expand Down
8 changes: 5 additions & 3 deletions pkg/gui/context/tags_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
}
}
Expand Down
8 changes: 5 additions & 3 deletions pkg/gui/context/working_tree_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
}

Expand Down
8 changes: 5 additions & 3 deletions pkg/gui/context/worktrees_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
}
}
Expand Down

0 comments on commit 198ead7

Please sign in to comment.