From 473d989cde342e38802c52d9117b12b1e5691077 Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Tue, 8 Aug 2023 08:10:26 +0200 Subject: [PATCH] Extract a renderLines function We'll make some changes to how the display strings are rendered, so it helps to have this code only once. This also fixes the problem that contexts using refreshViewportOnChange weren't able to use column alignments so far. We didn't need this yet, but it's just nice if everything works. :) --- pkg/gui/context/list_context_trait.go | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/pkg/gui/context/list_context_trait.go b/pkg/gui/context/list_context_trait.go index 9942fffe0..827ba2c11 100644 --- a/pkg/gui/context/list_context_trait.go +++ b/pkg/gui/context/list_context_trait.go @@ -57,10 +57,19 @@ 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, endIdx), + columnAlignments) +} + func (self *ListContextTrait) refreshViewport() { startIdx, length := self.GetViewTrait().ViewPortYBounds() - displayStrings := self.getDisplayStrings(startIdx, startIdx+length) - content := utils.RenderDisplayStrings(displayStrings, nil) + content := self.renderLines(startIdx, startIdx+length) self.GetViewTrait().SetViewPortContent(content) } @@ -93,14 +102,7 @@ func (self *ListContextTrait) HandleFocusLost(opts types.OnFocusLostOpts) error // OnFocus assumes that the content of the context has already been rendered to the view. OnRender is the function which actually renders the content to the view func (self *ListContextTrait) HandleRender() error { self.list.RefreshSelectedIdx() - var columnAlignments []utils.Alignment - if self.getColumnAlignments != nil { - columnAlignments = self.getColumnAlignments() - } - content := utils.RenderDisplayStrings( - self.getDisplayStrings(0, self.list.Len()), - columnAlignments, - ) + content := self.renderLines(0, self.list.Len()) self.GetViewTrait().SetContent(content) self.c.Render() self.setFooter()