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:
Stefan Haller 2023-08-17 20:57:20 +02:00
parent 297a020abf
commit 198ead7c14
16 changed files with 98 additions and 61 deletions

View file

@ -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)