mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-06-04 23:37:41 +02:00
Extract a ListRenderer struct
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.
This commit is contained in:
parent
297a020abf
commit
198ead7c14
@ -45,9 +45,11 @@ func NewBranchesContext(c *ContextCommon) *BranchesContext {
|
|||||||
Kind: types.SIDE_CONTEXT,
|
Kind: types.SIDE_CONTEXT,
|
||||||
Focusable: true,
|
Focusable: true,
|
||||||
})),
|
})),
|
||||||
list: viewModel,
|
ListRenderer: ListRenderer{
|
||||||
getDisplayStrings: getDisplayStrings,
|
list: viewModel,
|
||||||
c: c,
|
getDisplayStrings: getDisplayStrings,
|
||||||
|
},
|
||||||
|
c: c,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -54,9 +54,11 @@ func NewCommitFilesContext(c *ContextCommon) *CommitFilesContext {
|
|||||||
Transient: true,
|
Transient: true,
|
||||||
}),
|
}),
|
||||||
),
|
),
|
||||||
list: viewModel,
|
ListRenderer: ListRenderer{
|
||||||
getDisplayStrings: getDisplayStrings,
|
list: viewModel,
|
||||||
c: c,
|
getDisplayStrings: getDisplayStrings,
|
||||||
|
},
|
||||||
|
c: c,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,17 +4,13 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/jesseduffield/lazygit/pkg/gui/types"
|
"github.com/jesseduffield/lazygit/pkg/gui/types"
|
||||||
"github.com/jesseduffield/lazygit/pkg/utils"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type ListContextTrait struct {
|
type ListContextTrait struct {
|
||||||
types.Context
|
types.Context
|
||||||
|
ListRenderer
|
||||||
|
|
||||||
c *ContextCommon
|
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
|
|
||||||
// Some contexts, like the commit context, will highlight the path from the selected commit
|
// 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
|
// to its parents, because it's ambiguous otherwise. For these, we need to refresh the viewport
|
||||||
// so that we show the highlighted path.
|
// so that we show the highlighted path.
|
||||||
@ -26,10 +22,6 @@ type ListContextTrait struct {
|
|||||||
|
|
||||||
func (self *ListContextTrait) IsListContext() {}
|
func (self *ListContextTrait) IsListContext() {}
|
||||||
|
|
||||||
func (self *ListContextTrait) GetList() types.IList {
|
|
||||||
return self.list
|
|
||||||
}
|
|
||||||
|
|
||||||
func (self *ListContextTrait) FocusLine() {
|
func (self *ListContextTrait) FocusLine() {
|
||||||
// Doing this at the end of the layout function because we need the view to be
|
// 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
|
// 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() {
|
func (self *ListContextTrait) refreshViewport() {
|
||||||
startIdx, length := self.GetViewTrait().ViewPortYBounds()
|
startIdx, length := self.GetViewTrait().ViewPortYBounds()
|
||||||
content := self.renderLines(startIdx, startIdx+length)
|
content := self.renderLines(startIdx, startIdx+length)
|
||||||
|
27
pkg/gui/context/list_renderer.go
Normal file
27
pkg/gui/context/list_renderer.go
Normal file
@ -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)
|
||||||
|
}
|
@ -74,8 +74,10 @@ func NewLocalCommitsContext(c *ContextCommon) *LocalCommitsContext {
|
|||||||
Kind: types.SIDE_CONTEXT,
|
Kind: types.SIDE_CONTEXT,
|
||||||
Focusable: true,
|
Focusable: true,
|
||||||
})),
|
})),
|
||||||
list: viewModel,
|
ListRenderer: ListRenderer{
|
||||||
getDisplayStrings: getDisplayStrings,
|
list: viewModel,
|
||||||
|
getDisplayStrings: getDisplayStrings,
|
||||||
|
},
|
||||||
c: c,
|
c: c,
|
||||||
refreshViewportOnChange: true,
|
refreshViewportOnChange: true,
|
||||||
},
|
},
|
||||||
|
@ -34,10 +34,12 @@ func NewMenuContext(
|
|||||||
Focusable: true,
|
Focusable: true,
|
||||||
HasUncontrolledBounds: true,
|
HasUncontrolledBounds: true,
|
||||||
})),
|
})),
|
||||||
getDisplayStrings: viewModel.GetDisplayStrings,
|
ListRenderer: ListRenderer{
|
||||||
list: viewModel,
|
list: viewModel,
|
||||||
c: c,
|
getDisplayStrings: viewModel.GetDisplayStrings,
|
||||||
getColumnAlignments: func() []utils.Alignment { return viewModel.columnAlignment },
|
getColumnAlignments: func() []utils.Alignment { return viewModel.columnAlignment },
|
||||||
|
},
|
||||||
|
c: c,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -49,9 +49,11 @@ func NewReflogCommitsContext(c *ContextCommon) *ReflogCommitsContext {
|
|||||||
Kind: types.SIDE_CONTEXT,
|
Kind: types.SIDE_CONTEXT,
|
||||||
Focusable: true,
|
Focusable: true,
|
||||||
})),
|
})),
|
||||||
list: viewModel,
|
ListRenderer: ListRenderer{
|
||||||
getDisplayStrings: getDisplayStrings,
|
list: viewModel,
|
||||||
c: c,
|
getDisplayStrings: getDisplayStrings,
|
||||||
|
},
|
||||||
|
c: c,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -43,9 +43,11 @@ func NewRemoteBranchesContext(
|
|||||||
Focusable: true,
|
Focusable: true,
|
||||||
Transient: true,
|
Transient: true,
|
||||||
})),
|
})),
|
||||||
list: viewModel,
|
ListRenderer: ListRenderer{
|
||||||
getDisplayStrings: getDisplayStrings,
|
list: viewModel,
|
||||||
c: c,
|
getDisplayStrings: getDisplayStrings,
|
||||||
|
},
|
||||||
|
c: c,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -38,9 +38,11 @@ func NewRemotesContext(c *ContextCommon) *RemotesContext {
|
|||||||
Kind: types.SIDE_CONTEXT,
|
Kind: types.SIDE_CONTEXT,
|
||||||
Focusable: true,
|
Focusable: true,
|
||||||
})),
|
})),
|
||||||
list: viewModel,
|
ListRenderer: ListRenderer{
|
||||||
getDisplayStrings: getDisplayStrings,
|
list: viewModel,
|
||||||
c: c,
|
getDisplayStrings: getDisplayStrings,
|
||||||
|
},
|
||||||
|
c: c,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -40,9 +40,11 @@ func NewStashContext(
|
|||||||
Kind: types.SIDE_CONTEXT,
|
Kind: types.SIDE_CONTEXT,
|
||||||
Focusable: true,
|
Focusable: true,
|
||||||
})),
|
})),
|
||||||
list: viewModel,
|
ListRenderer: ListRenderer{
|
||||||
getDisplayStrings: getDisplayStrings,
|
list: viewModel,
|
||||||
c: c,
|
getDisplayStrings: getDisplayStrings,
|
||||||
|
},
|
||||||
|
c: c,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -93,8 +93,10 @@ func NewSubCommitsContext(
|
|||||||
Focusable: true,
|
Focusable: true,
|
||||||
Transient: true,
|
Transient: true,
|
||||||
})),
|
})),
|
||||||
list: viewModel,
|
ListRenderer: ListRenderer{
|
||||||
getDisplayStrings: getDisplayStrings,
|
list: viewModel,
|
||||||
|
getDisplayStrings: getDisplayStrings,
|
||||||
|
},
|
||||||
c: c,
|
c: c,
|
||||||
refreshViewportOnChange: true,
|
refreshViewportOnChange: true,
|
||||||
},
|
},
|
||||||
|
@ -35,9 +35,11 @@ func NewSubmodulesContext(c *ContextCommon) *SubmodulesContext {
|
|||||||
Kind: types.SIDE_CONTEXT,
|
Kind: types.SIDE_CONTEXT,
|
||||||
Focusable: true,
|
Focusable: true,
|
||||||
})),
|
})),
|
||||||
list: viewModel,
|
ListRenderer: ListRenderer{
|
||||||
getDisplayStrings: getDisplayStrings,
|
list: viewModel,
|
||||||
c: c,
|
getDisplayStrings: getDisplayStrings,
|
||||||
|
},
|
||||||
|
c: c,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -54,9 +54,11 @@ func NewSuggestionsContext(
|
|||||||
Focusable: true,
|
Focusable: true,
|
||||||
HasUncontrolledBounds: true,
|
HasUncontrolledBounds: true,
|
||||||
})),
|
})),
|
||||||
list: viewModel,
|
ListRenderer: ListRenderer{
|
||||||
getDisplayStrings: getDisplayStrings,
|
list: viewModel,
|
||||||
c: c,
|
getDisplayStrings: getDisplayStrings,
|
||||||
|
},
|
||||||
|
c: c,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -40,9 +40,11 @@ func NewTagsContext(
|
|||||||
Kind: types.SIDE_CONTEXT,
|
Kind: types.SIDE_CONTEXT,
|
||||||
Focusable: true,
|
Focusable: true,
|
||||||
})),
|
})),
|
||||||
list: viewModel,
|
ListRenderer: ListRenderer{
|
||||||
getDisplayStrings: getDisplayStrings,
|
list: viewModel,
|
||||||
c: c,
|
getDisplayStrings: getDisplayStrings,
|
||||||
|
},
|
||||||
|
c: c,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -41,9 +41,11 @@ func NewWorkingTreeContext(c *ContextCommon) *WorkingTreeContext {
|
|||||||
Kind: types.SIDE_CONTEXT,
|
Kind: types.SIDE_CONTEXT,
|
||||||
Focusable: true,
|
Focusable: true,
|
||||||
})),
|
})),
|
||||||
list: viewModel,
|
ListRenderer: ListRenderer{
|
||||||
getDisplayStrings: getDisplayStrings,
|
list: viewModel,
|
||||||
c: c,
|
getDisplayStrings: getDisplayStrings,
|
||||||
|
},
|
||||||
|
c: c,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -38,9 +38,11 @@ func NewWorktreesContext(c *ContextCommon) *WorktreesContext {
|
|||||||
Kind: types.SIDE_CONTEXT,
|
Kind: types.SIDE_CONTEXT,
|
||||||
Focusable: true,
|
Focusable: true,
|
||||||
})),
|
})),
|
||||||
list: viewModel,
|
ListRenderer: ListRenderer{
|
||||||
getDisplayStrings: getDisplayStrings,
|
list: viewModel,
|
||||||
c: c,
|
getDisplayStrings: getDisplayStrings,
|
||||||
|
},
|
||||||
|
c: c,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user