Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add AppendContent and DeleteTopContent methods #431

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 29 additions & 0 deletions viewport/viewport.go
Expand Up @@ -108,6 +108,35 @@ 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 {
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 {
Expand Down