Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion Sources/DevtailKit/TerminalOutputView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@ private struct TerminalNSView: NSViewRepresentable {
func update(buffer: TerminalBuffer, fontSize: CGFloat) {
guard let textView else { return }
guard let storage = textView.textStorage else { return }
guard let scrollView = textView.enclosingScrollView else { return }

// Check if we're at (or near) the bottom before updating content.
// If so, follow new output. If the user scrolled up, stay put.
let clipView = scrollView.contentView
let maxScrollY = max(textView.frame.height - clipView.bounds.height, 0)
let isAtBottom = clipView.bounds.origin.y >= maxScrollY - 20

ensureFontCache(fontSize: fontSize)
let attrStr = buildAttributedString(buffer: buffer, fontSize: fontSize)
Expand All @@ -82,7 +89,9 @@ private struct TerminalNSView: NSViewRepresentable {
storage.setAttributedString(attrStr)
storage.endEditing()

textView.scrollToEndOfDocument(nil)
if isAtBottom {
textView.scrollToEndOfDocument(nil)
}
}

private func ensureFontCache(fontSize: CGFloat) {
Expand Down
Loading