@@ -12,6 +12,8 @@ export interface TerminalOutputState {
1212 readonly retainedBytes : number ;
1313 readonly resetVersion : number ;
1414 readonly nextOffset : number ;
15+ /** Chunks before this offset are replayed to emulators but are no longer displayed text. */
16+ readonly textStartOffset : number ;
1517}
1618
1719export interface TerminalOutputCursor {
@@ -54,6 +56,7 @@ export const EMPTY_TERMINAL_OUTPUT_STATE = Object.freeze<TerminalOutputState>({
5456 retainedBytes : 0 ,
5557 resetVersion : 0 ,
5658 nextOffset : 0 ,
59+ textStartOffset : 0 ,
5760} ) ;
5861
5962interface Utf8Chunk {
@@ -175,6 +178,7 @@ function appendOutput(
175178 retainedBytes : 0 ,
176179 resetVersion : current . resetVersion + 1 ,
177180 nextOffset : current . nextOffset + data . length ,
181+ textStartOffset : current . textStartOffset ,
178182 } ;
179183 }
180184 const appended = splitOutputChunks (
@@ -221,6 +225,7 @@ function appendOutput(
221225 retainedBytes,
222226 resetVersion : current . resetVersion ,
223227 nextOffset : appended . nextOffset ,
228+ textStartOffset : current . textStartOffset ,
224229 } ;
225230}
226231
@@ -241,13 +246,39 @@ function resetOutput(
241246 retainedBytes : reset . byteLength ,
242247 resetVersion : current . resetVersion + 1 ,
243248 nextOffset : reset . nextOffset ,
249+ textStartOffset : 0 ,
244250 } ;
245251}
246252
247- export function terminalOutputText ( output : TerminalOutputState ) : string {
253+ // Erases the screen and scrollback only. A full emulator reset would drop the
254+ // screen and input modes the still-running program negotiated.
255+ const CLEAR_TERMINAL_TEXT = "\u001b[H\u001b[2J\u001b[3J" ;
256+
257+ /** Earlier output stays retained so a replay from the start restores those modes. */
258+ function clearOutput ( current : TerminalOutputState , maxBufferBytes : number ) : TerminalOutputState {
259+ // The erase rides over the budget: trimming here would evict the output that set those modes.
260+ const cleared = appendOutput (
261+ current ,
262+ CLEAR_TERMINAL_TEXT ,
263+ maxBufferBytes <= 0
264+ ? maxBufferBytes
265+ : Math . max ( maxBufferBytes , current . retainedBytes + CLEAR_TERMINAL_TEXT . length ) ,
266+ ) ;
267+ return { ...cleared , textStartOffset : cleared . nextOffset } ;
268+ }
269+
270+ /** Everything retained, for an emulator that is fed the whole history. */
271+ export function terminalOutputReplay ( output : TerminalOutputState ) : string {
248272 return output . chunks . map ( ( chunk ) => chunk . data ) . join ( "" ) ;
249273}
250274
275+ /** The output as plain text, for surfaces that do not interpret escape sequences. */
276+ export function terminalOutputText ( output : TerminalOutputState ) : string {
277+ return output . chunks
278+ . map ( ( chunk ) => chunk . data . slice ( Math . max ( 0 , output . textStartOffset - chunk . startOffset ) ) )
279+ . join ( "" ) ;
280+ }
281+
251282export function readTerminalOutputUpdate (
252283 output : TerminalOutputState ,
253284 cursor : TerminalOutputCursor ,
@@ -263,7 +294,7 @@ export function readTerminalOutputUpdate(
263294 cursor . resetVersion !== output . resetVersion ||
264295 cursor . offset < ( firstChunk ?. startOffset ?? output . nextOffset )
265296 ) {
266- return { type : "reset" , data : terminalOutputText ( output ) , cursor : nextCursor } ;
297+ return { type : "reset" , data : terminalOutputReplay ( output ) , cursor : nextCursor } ;
267298 }
268299
269300 // Appends and compaction preserve offset order, so unread chunks form a suffix.
@@ -286,4 +317,4 @@ export function readTerminalOutputUpdate(
286317 } ;
287318}
288319
289- export { appendOutput , resetOutput } ;
320+ export { appendOutput , clearOutput , resetOutput } ;
0 commit comments