Skip to content

Commit 4a34596

Browse files
committed
Add API to change the history size
1 parent f4edcf8 commit 4a34596

2 files changed

Lines changed: 44 additions & 0 deletions

File tree

Sources/SwiftTerm/Buffer.swift

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,32 @@ public final class Buffer {
470470
cols = newCols
471471
}
472472

473+
public func changeHistorySize (_ newScrollback: Int?)
474+
{
475+
self.scrollback = newScrollback
476+
self.hasScrollback = newScrollback != nil
477+
478+
let newMaxLength = getCorrectBufferLength(rows)
479+
let oldMaxLength = lines.maxLength
480+
481+
if newMaxLength != oldMaxLength {
482+
if newMaxLength > oldMaxLength {
483+
// Increase buffer size - just update maxLength
484+
lines.maxLength = newMaxLength
485+
} else {
486+
// Decrease buffer size - need to trim and adjust
487+
let amountToTrim = lines.count - newMaxLength
488+
if amountToTrim > 0 {
489+
lines.trimStart(count: amountToTrim)
490+
yBase = max(yBase - amountToTrim, 0)
491+
yDisp = max(yDisp - amountToTrim, 0)
492+
savedY = max(savedY - amountToTrim, 0)
493+
}
494+
lines.maxLength = newMaxLength
495+
}
496+
}
497+
}
498+
473499
func translateBufferLineToString (lineIndex: Int, trimRight: Bool, startCol: Int = 0, endCol: Int = -1) -> String
474500
{
475501
let line = _lines [lineIndex]

Sources/SwiftTerm/Terminal.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4902,6 +4902,24 @@ open class Terminal {
49024902
refresh (startRow: 0, endRow: self.rows - 1)
49034903
}
49044904

4905+
/**
4906+
* Changes the scrollback (history) size of the terminal after it has been instantiated.
4907+
* The new scrollback size only affects the normal buffer, not the alternate buffer.
4908+
*
4909+
* - Parameter newScrollback: The new scrollback size in lines. Pass `nil` to disable scrollback.
4910+
*/
4911+
public func changeHistorySize (_ newScrollback: Int?)
4912+
{
4913+
// Only the normal buffer has scrollback, the alt buffer should never have scrollback
4914+
normalBuffer.changeHistorySize(newScrollback)
4915+
4916+
// Update the options to reflect the new scrollback size
4917+
options.scrollback = newScrollback ?? 0
4918+
4919+
// Refresh the display to ensure proper rendering after history size change
4920+
refresh (startRow: 0, endRow: self.rows - 1)
4921+
}
4922+
49054923
func syncScrollArea ()
49064924
{
49074925
// This should call the viewport sync-scroll-area

0 commit comments

Comments
 (0)