Skip to content
Merged
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion Sources/MikuCodeApp/Terminal/Core/TerminalBuffer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,8 @@ struct TerminalBuffer: Sendable {
let viewportEnd = (fixedScreen || isAlternateScreen)
? min(currentLines.count - 1, viewportStart + rows - 1)
: currentLines.count - 1
setCursor(row: min(viewportEnd, max(viewportStart, cursorRow + distance)), column: cursorColumn)
let column = min(cursorColumn, max(0, columns - 1))
setCursor(row: min(viewportEnd, max(viewportStart, cursorRow + distance)), column: column)
}

private mutating func handleCSI(parameters values: [Int], final: UInt8, isPrivate: Bool) {
Expand Down
46 changes: 46 additions & 0 deletions Tests/MikuCodeAppTests/TerminalBufferTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,52 @@ final class TerminalBufferTests: XCTestCase {
XCTAssertEqual(buffer.snapshot.plainText, "three")
}

func testAlternateScreenIsolatedFromPrimaryScrollbackAndSupportsRowAddressing() {
var buffer = TerminalBuffer(columns: 16, rows: 3)
buffer.ingest("shell prompt>")

buffer.ingest("\u{1B}[?1049h\u{1B}[2J\u{1B}[Htop\u{1B}[2;1Hbottom")

XCTAssertEqual(
buffer.snapshot.lines.prefix(2).map(\.plainText),
["top", "bottom"],
"A full-screen TUI must draw on an isolated two-dimensional alternate screen."
)
XCTAssertEqual(buffer.snapshot.lines.count, 3)

buffer.ingest("\u{1B}[?1049l")

XCTAssertEqual(
buffer.snapshot.lines[0].plainText,
"shell prompt>",
"Leaving the alternate screen must restore the shell screen instead of retaining TUI frames."
)
}

func testAlternateScreenVerticalMovementNormalizesPendingFinalColumn() {
var buffer = TerminalBuffer(columns: 3, rows: 2)

buffer.ingest("\u{1B}[?1049habc\u{1B}[1BZ")

XCTAssertEqual(
buffer.snapshot.lines.prefix(2).map(\.plainText),
["abc", " Z"],
"Moving vertically from the last column must not trigger a phantom wrap and scroll."
)
}

func testFixedPrimaryVerticalMovementNormalizesPendingFinalColumnAfterScrollback() {
var buffer = TerminalBuffer(columns: 3, rows: 2, maxLines: 8, maxCells: 256)

buffer.ingest("one\ntwo\nabc\u{1B}[1AZ")

XCTAssertEqual(
buffer.visibleSnapshot.lines.map(\.plainText),
["twZ", "abc"],
"Vertical motion within a scrolled primary viewport must clear pending wrap before the next glyph."
)
}

func testClampsLargeCursorForwardWithinCellLimit() {
let maxCells = 32
var buffer = TerminalBuffer(maxCells: maxCells)
Expand Down
20 changes: 10 additions & 10 deletions Tests/MikuCodeAppTests/TerminalSessionPersistenceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -197,21 +197,21 @@ final class TerminalSessionPersistenceTests: XCTestCase {
withIntermediateDirectories: true
)
let persistence = TerminalSessionPersistence(url: temporaryURL())
let session = TerminalSessionModel(persistence: persistence, shell: "/bin/sh")
session.start()
for byte in Data("cd \(directory.path)".utf8) {
session.sendRawInput(Data([byte]))
}
session.sendRawInput(Data([0x0D]))
session.stop()
try persistence.save(PersistedTerminalSession(
shell: "/bin/sh",
columns: 80,
rows: 24,
snapshot: TerminalSnapshot(lines: [TerminalLine(text: "saved")]),
workingDirectory: directory.path
))

let reopened = TerminalSessionModel(persistence: persistence, shell: "/bin/sh")
reopened.start()
defer { reopened.stop() }

reopened.sendRawInput(Data("pwd\n".utf8))
try await waitUntil {
reopened.snapshot.plainText.contains(directory.lastPathComponent)
reopened.submit("pwd")
try await waitUntil(timeout: .seconds(10)) {
reopened.snapshot.lines.map(\.plainText).joined().contains(directory.path)
}
}

Expand Down