Skip to content

Commit

Permalink
ANSI sequences need to be interpreted for TUIs (#1956)
Browse files Browse the repository at this point in the history
  • Loading branch information
sourishkrout authored Feb 12, 2025
1 parent a042c2b commit ac1ec9b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 31 deletions.
6 changes: 1 addition & 5 deletions src/extension/executors/runner/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import {
getServerRunnerVersion,
isNotebookTerminalEnabledForCell,
} from '../../../utils/configuration'
import { ITerminalState, XTermState } from '../../terminal/terminalState'
import { ITerminalState } from '../../terminal/terminalState'
import { toggleTerminal } from '../../commands'
import { closeTerminalByEnvID, openTerminalByEnvID } from '../task'
import {
Expand Down Expand Up @@ -280,10 +280,6 @@ export const executeRunner: IKernelRunner = async ({

let mimeType = cellMimeType
if (interactive) {
if (terminalState instanceof XTermState) {
terminalState.resetBuffer()
}

if (revealNotebookTerminal) {
program.registerTerminalWindow('notebook')
await program.setActiveTerminalWindow('notebook')
Expand Down
39 changes: 13 additions & 26 deletions src/extension/terminal/terminalState.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Terminal as XTerm } from '@xterm/headless'
import { SerializeAddon } from '@xterm/addon-serialize'

import { OutputType } from '../../constants'
import { RunnerExitReason, RunProgramOptions } from '../runner'
Expand Down Expand Up @@ -30,26 +31,19 @@ export class XTermState implements ITerminalState {
readonly outputType = OutputType.terminal

protected xterm: XTerm
private serializer: SerializeAddon
private processInfo: IProcessInfoState | undefined
private programOptions: RunProgramOptions | undefined
protected buffer: RingBuffer<string>
private textDecoder = new TextDecoder()

constructor(readonly capacity: number) {
this.buffer = this.resetBuffer()

// TODO: lines/cols
this.xterm = new XTerm({
allowProposedApi: true,
scrollback: capacity,
})
}

setProgramOptions(programOptions: RunProgramOptions): void {
this.programOptions = programOptions
}

getProgramOptions(): RunProgramOptions | undefined {
return this.programOptions
this.serializer = new SerializeAddon()
this.xterm.loadAddon(this.serializer)
}

setProcessInfo(processInfo?: IProcessInfoState) {
Expand All @@ -60,27 +54,20 @@ export class XTermState implements ITerminalState {
return this.processInfo
}

serialize(): string {
return this.buffer.getAll().join('')
setProgramOptions(programOptions: RunProgramOptions): void {
this.programOptions = programOptions
}

write(data: string | Uint8Array): void {
this.xterm.write(data)
this.addToBuffer(data)
getProgramOptions(): RunProgramOptions | undefined {
return this.programOptions
}

addToBuffer(data: string | Uint8Array): void {
if (typeof data === 'string') {
this.buffer.push(data)
} else {
this.buffer.push(this.textDecoder.decode(data))
}
serialize(): string {
return this.serializer.serialize({ scrollback: this.capacity })
}

resetBuffer(): RingBuffer<string> {
// capacity is items vs lines because we don't handle line breaks
this.buffer = new RingBuffer<string>(this.capacity)
return this.buffer
write(data: string | Uint8Array): void {
this.xterm.write(data)
}

input(data: string, wasUserInput: boolean): void {
Expand Down

0 comments on commit ac1ec9b

Please sign in to comment.