GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(app): terminal flakiness · f2dmarques/opencode@cdbb009

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • packages/app/src
    • addons
      • serialize.test.ts
      • serialize.ts
    • components
      • terminal.tsx
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎packages/app/src/addons/serialize.test.ts‎

Copy file name to clipboardExpand all lines: packages/app/src/addons/serialize.test.ts
+47Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,53 @@ describe("SerializeAddon", () => {
242242
expect(term2.buffer.active.getLine(2)?.translateToString(true)).toBe("total 42")
243243
})
244244

245+
test("serialized output should restore after Terminal.reset()", async () => {
246+
const { term, addon } = createTerminal()
247+
248+
const content = [
249+
"\x1b[1;32m❯\x1b[0m \x1b[34mcd\x1b[0m /some/path",
250+
"\x1b[1;32m❯\x1b[0m \x1b[34mls\x1b[0m -la",
251+
"total 42",
252+
].join("\r\n")
253+
254+
await writeAndWait(term, content)
255+
256+
const serialized = addon.serialize()
257+
258+
const { term: term2 } = createTerminal()
259+
terminals.push(term2)
260+
term2.reset()
261+
await writeAndWait(term2, serialized)
262+
263+
expect(term2.buffer.active.getLine(0)?.translateToString(true)).toContain("cd /some/path")
264+
expect(term2.buffer.active.getLine(1)?.translateToString(true)).toContain("ls -la")
265+
expect(term2.buffer.active.getLine(2)?.translateToString(true)).toBe("total 42")
266+
})
267+
268+
test("alternate buffer should round-trip without garbage", async () => {
269+
const { term, addon } = createTerminal(20, 5)
270+
271+
await writeAndWait(term, "normal\r\n")
272+
await writeAndWait(term, "\x1b[?1049h\x1b[HALT")
273+
274+
expect(term.buffer.active.type).toBe("alternate")
275+
276+
const serialized = addon.serialize()
277+
278+
const { term: term2 } = createTerminal(20, 5)
279+
terminals.push(term2)
280+
await writeAndWait(term2, serialized)
281+
282+
expect(term2.buffer.active.type).toBe("alternate")
283+
284+
const line = term2.buffer.active.getLine(0)
285+
expect(line?.translateToString(true)).toBe("ALT")
286+
287+
// Ensure a cell beyond content isn't garbage
288+
const cellCode = line?.getCell(10)?.getCode()
289+
expect(cellCode === 0 || cellCode === 32).toBe(true)
290+
})
291+
245292
test("serialized output written to new terminal should match original colors", async () => {
246293
const { term, addon } = createTerminal(40, 5)
247294

Collapse file

‎packages/app/src/addons/serialize.ts‎

Copy file name to clipboardExpand all lines: packages/app/src/addons/serialize.ts
+19-23Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -157,23 +157,6 @@ function equalFlags(cell1: IBufferCell, cell2: IBufferCell): boolean {
157157
abstract class BaseSerializeHandler {
158158
constructor(protected readonly _buffer: IBuffer) {}
159159

160-
private _isRealContent(codepoint: number): boolean {
161-
if (codepoint === 0) return false
162-
if (codepoint >= 0xf000) return false
163-
return true
164-
}
165-
166-
private _findLastContentColumn(line: IBufferLine): number {
167-
let lastContent = -1
168-
for (let col = 0; col < line.length; col++) {
169-
const cell = line.getCell(col)
170-
if (cell && this._isRealContent(cell.getCode())) {
171-
lastContent = col
172-
}
173-
}
174-
return lastContent + 1
175-
}
176-
177160
public serialize(range: IBufferRange, excludeFinalCursorPosition?: boolean): string {
178161
let oldCell = this._buffer.getNullCell()
179162

@@ -182,14 +165,14 @@ abstract class BaseSerializeHandler {
182165
const startColumn = range.start.x
183166
const endColumn = range.end.x
184167

185-
this._beforeSerialize(endRow - startRow, startRow, endRow)
168+
this._beforeSerialize(endRow - startRow + 1, startRow, endRow)
186169

187170
for (let row = startRow; row <= endRow; row++) {
188171
const line = this._buffer.getLine(row)
189172
if (line) {
190173
const startLineColumn = row === range.start.y ? startColumn : 0
191-
const maxColumn = row === range.end.y ? endColumn : this._findLastContentColumn(line)
192-
const endLineColumn = Math.min(maxColumn, line.length)
174+
const endLineColumn = Math.min(endColumn, line.length)
175+
193176
for (let col = startLineColumn; col < endLineColumn; col++) {
194177
const c = line.getCell(col)
195178
if (!c) {
@@ -243,6 +226,13 @@ class StringSerializeHandler extends BaseSerializeHandler {
243226

244227
protected _beforeSerialize(rows: number, start: number, _end: number): void {
245228
this._allRows = new Array<string>(rows)
229+
this._allRowSeparators = new Array<string>(rows)
230+
this._rowIndex = 0
231+
232+
this._currentRow = ""
233+
this._nullCellCount = 0
234+
this._cursorStyle = this._buffer.getNullCell()
235+
246236
this._lastContentCursorRow = start
247237
this._lastCursorRow = start
248238
this._firstRow = start
@@ -251,6 +241,11 @@ class StringSerializeHandler extends BaseSerializeHandler {
251241
protected _rowEnd(row: number, isLastRow: boolean): void {
252242
let rowSeparator = ""
253243

244+
if (this._nullCellCount > 0) {
245+
this._currentRow += " ".repeat(this._nullCellCount)
246+
this._nullCellCount = 0
247+
}
248+
254249
if (!isLastRow) {
255250
const nextLine = this._buffer.getLine(row + 1)
256251

@@ -388,7 +383,8 @@ class StringSerializeHandler extends BaseSerializeHandler {
388383
}
389384

390385
const codepoint = cell.getCode()
391-
const isGarbage = codepoint >= 0xf000
386+
const isInvalidCodepoint = codepoint > 0x10ffff || (codepoint >= 0xd800 && codepoint <= 0xdfff)
387+
const isGarbage = isInvalidCodepoint || (codepoint >= 0xf000 && cell.getWidth() === 1)
392388
const isEmptyCell = codepoint === 0 || cell.getChars() === "" || isGarbage
393389

394390
const sgrSeq = this._diffStyle(cell, this._cursorStyle)
@@ -397,7 +393,7 @@ class StringSerializeHandler extends BaseSerializeHandler {
397393

398394
if (styleChanged) {
399395
if (this._nullCellCount > 0) {
400-
this._currentRow += `\u001b[${this._nullCellCount}C`
396+
this._currentRow += " ".repeat(this._nullCellCount)
401397
this._nullCellCount = 0
402398
}
403399

@@ -417,7 +413,7 @@ class StringSerializeHandler extends BaseSerializeHandler {
417413
this._nullCellCount += cell.getWidth()
418414
} else {
419415
if (this._nullCellCount > 0) {
420-
this._currentRow += `\u001b[${this._nullCellCount}C`
416+
this._currentRow += " ".repeat(this._nullCellCount)
421417
this._nullCellCount = 0
422418
}
423419

Collapse file

‎packages/app/src/components/terminal.tsx‎

Copy file name to clipboardExpand all lines: packages/app/src/components/terminal.tsx
+6-5Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,12 @@ export const Terminal = (props: TerminalProps) => {
146146
term.resize(local.pty.cols, local.pty.rows)
147147
}
148148
term.reset()
149-
term.write(local.pty.buffer)
150-
if (local.pty.scrollY) {
151-
term.scrollToLine(local.pty.scrollY)
152-
}
153-
fitAddon.fit()
149+
term.write(local.pty.buffer, () => {
150+
if (local.pty.scrollY) {
151+
term.scrollToLine(local.pty.scrollY)
152+
}
153+
fitAddon.fit()
154+
})
154155
}
155156

156157
fitAddon.observeResize()

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.