feat(vt): implement scrollback buffer support#605
Open
Gaurav-Gosain wants to merge 1 commit intocharmbracelet:mainfrom
Open
feat(vt): implement scrollback buffer support#605Gaurav-Gosain wants to merge 1 commit intocharmbracelet:mainfrom
Gaurav-Gosain wants to merge 1 commit intocharmbracelet:mainfrom
Conversation
Adds comprehensive scrollback buffer functionality to the VT terminal emulator, allowing applications to store and access lines that have scrolled off the top of the visible screen. Features: - Scrollback buffer with configurable maximum lines (default 10,000) - Automatic line capture during full-width scrolling operations - Thread-safe access with read/write mutexes - Deep copying of lines to prevent aliasing issues - ED 3 (ESC[3J) sequence support for clearing scrollback - Separate scrollback for main and alternate screens - Comprehensive API for accessing and managing scrollback API: - Scrollback() - Get scrollback buffer reference - ScrollbackLen() - Get number of lines in scrollback - ScrollbackLine(index) - Retrieve line at index - ClearScrollback() - Clear all scrollback history - SetScrollbackMaxLines(n) - Configure buffer size The scrollback only captures full-width scrolling from Y=0, not limited scroll regions, following standard terminal emulator behavior.
| // scrolled off the top of the visible screen. | ||
| type Scrollback struct { | ||
| // lines stores the scrollback lines, with the oldest at index 0 | ||
| lines [][]uv.Cell |
Contributor
There was a problem hiding this comment.
Suggested change
| lines [][]uv.Cell | |
| lines []uv.Line |
|
|
||
| // Lines returns a slice of all lines in the scrollback buffer, from oldest | ||
| // to newest. The returned slice should not be modified. | ||
| func (sb *Scrollback) Lines() [][]uv.Cell { |
Contributor
There was a problem hiding this comment.
Suggested change
| func (sb *Scrollback) Lines() [][]uv.Cell { | |
| func (sb *Scrollback) Lines() []uv.Line { |
| // number of lines. If maxLines is 0, a default of 10000 lines is used. | ||
| func NewScrollback(maxLines int) *Scrollback { | ||
| if maxLines <= 0 { | ||
| maxLines = 10000 // Default scrollback size |
Contributor
There was a problem hiding this comment.
Move this into a const DefaultScrollbackSize = 10000
| // are discarded to fit the new limit. | ||
| func (sb *Scrollback) SetMaxLines(maxLines int) { | ||
| if maxLines <= 0 { | ||
| maxLines = 10000 // Default scrollback size |
Contributor
There was a problem hiding this comment.
Suggested change
| maxLines = 10000 // Default scrollback size | |
| maxLines = DefaultScrollbackSize |
| // scrollback is the scrollback buffer for lines that have scrolled off the top. | ||
| scrollback *Scrollback | ||
| // mutex for the screen. | ||
| mu sync.RWMutex |
Contributor
There was a problem hiding this comment.
The current VT implementation doesn't use any mutexes and doesn't guarantee thread safety. Caller should handle that and I think it should stay that way for now
aymanbagabas
pushed a commit
that referenced
this pull request
Mar 5, 2026
* feat: add vt scrollback * fixup: address ayman's comments * use slices package Supersedes: #605
Contributor
|
Hi @Gaurav-Gosain, I've merged #793. Let me know if that works with your use cases 🙂 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR implements comprehensive scrollback buffer functionality for the VT terminal emulator. The scrollback buffer stores lines that scroll off the top of the visible screen, allowing applications to access terminal history.
Motivation
Scrollback support is a fundamental feature of modern terminal emulators. Several TODOs in the codebase indicated this functionality was missing:
handlers.go: ED 3 sequence (clear scrollback) was not implementedcc.go: Scrollback support was marked as TODOcsi_mode.go: Scrollback-related modes needed implementationThis implementation resolves these TODOs and provides a robust foundation for terminal history management.
Implementation
Core Components
Scrollback struct (
scrollback.go):Screen integration (
screen.go):API Methods (
emulator.go):Scrollback()- Access scrollback bufferScrollbackLen()- Get number of stored linesScrollbackLine(index)- Retrieve specific lineClearScrollback()- Clear historySetScrollbackMaxLines(n)- Configure buffer sizeANSI Sequence Support (
handlers.go):Design Decisions
Usage Example
Testing
Comprehensive test suite with 13 tests covering:
All tests pass successfully.
Performance Considerations
Breaking Changes
None. This is purely additive functionality.
Related
Resolves TODOs in:
handlers.go(ED 3 sequence)cc.go(scrollback support)csi_mode.go(scrollback modes)