From d7a6447a680d57fe491eb694deff271d6d468ae0 Mon Sep 17 00:00:00 2001 From: Travis Johnson Date: Thu, 9 Nov 2023 16:35:32 -0500 Subject: [PATCH 1/2] add AppendContent and DeleteTopContent methods --- viewport/viewport.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/viewport/viewport.go b/viewport/viewport.go index b13e33c0..5600a758 100644 --- a/viewport/viewport.go +++ b/viewport/viewport.go @@ -108,6 +108,36 @@ func (m *Model) SetContent(s string) { } } +// AppendContent adds content to the end of the current content. +func (m *Model) AppendContent(c []string) tea.Cmd { + wouldRender := m.PastBottom() + m.lines = append(m.lines, c...) + + if wouldRender && m.HighPerformanceRendering { + // This can potentially be replaced with an appropriate tea.ScrollDown + top, _ := m.scrollArea() + numLinesAlreadyOnScreen := len(m.lines) - len(c) - m.YOffset + numLinesToShow := m.Height - numLinesAlreadyOnScreen + linesToShow := c[:numLinesToShow] + + return tea.ScrollDown(linesToShow, top+numLinesAlreadyOnScreen, top+numLinesAlreadyOnScreen+numLinesToShow) + } else { + return nil + } +} + +// DeleteTopLines deletes n lines from the top of the current content. It also +// calls LineDown the appropriate amount. The return value should be passed to +// ViewDown if using high performance rendering. +func (m *Model) DeleteTopContent(n int) []string { + downLines := m.LineDown(min(n-m.YOffset, 0)) + + m.lines = m.lines[n:] + m.SetYOffset(m.YOffset - n) + + return downLines +} + // maxYOffset returns the maximum possible value of the y-offset based on the // viewport's content and set height. func (m Model) maxYOffset() int { From 871bc71f2cd0756400f694d06a993c516529f276 Mon Sep 17 00:00:00 2001 From: Travis Johnson Date: Thu, 9 Nov 2023 17:07:17 -0500 Subject: [PATCH 2/2] remove old comment --- viewport/viewport.go | 1 - 1 file changed, 1 deletion(-) diff --git a/viewport/viewport.go b/viewport/viewport.go index 5600a758..7aab1986 100644 --- a/viewport/viewport.go +++ b/viewport/viewport.go @@ -114,7 +114,6 @@ func (m *Model) AppendContent(c []string) tea.Cmd { m.lines = append(m.lines, c...) if wouldRender && m.HighPerformanceRendering { - // This can potentially be replaced with an appropriate tea.ScrollDown top, _ := m.scrollArea() numLinesAlreadyOnScreen := len(m.lines) - len(c) - m.YOffset numLinesToShow := m.Height - numLinesAlreadyOnScreen