Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update the a11y terminal buffer dynamically #176413

Merged
merged 26 commits into from
Mar 11, 2023
Merged
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export class AccessibleBufferWidget extends DisposableStore {
private _lastContentLength: number = 0;
private _font: ITerminalFont;
private _xtermElement: HTMLElement;
private _content: string = '';

constructor(
private readonly _xterm: IXtermTerminal & { raw: Terminal },
Expand Down Expand Up @@ -87,6 +88,12 @@ export class AccessibleBufferWidget extends DisposableStore {
this._font = _xterm.getFont();
}
}));
this.add(this._xterm.raw.onData(() => {
meganrogge marked this conversation as resolved.
Show resolved Hide resolved
if (this._accessibleBuffer.classList.contains('active')) {
meganrogge marked this conversation as resolved.
Show resolved Hide resolved
this._refreshSelection = true;
meganrogge marked this conversation as resolved.
Show resolved Hide resolved
this._refresh();
}
}));
}

private _hide(): void {
Expand All @@ -95,42 +102,61 @@ export class AccessibleBufferWidget extends DisposableStore {
this._xterm.raw.focus();
}

async show(): Promise<void> {
const commandDetection = this._capabilities.get(TerminalCapability.CommandDetection);
const fragment = !!commandDetection ? this._getShellIntegrationContent() : this._getAllContent();
const model = await this._getTextModel(URI.from({ scheme: AccessibleBufferConstants.Scheme, fragment }));
private async _updateContent(refresh?: boolean): Promise<ITextModel> {
const sb = new StringBuilder(10000);
if (refresh) {
sb.appendString(this._content);
sb.appendString(this._getContent(this._lastContentLength));
meganrogge marked this conversation as resolved.
Show resolved Hide resolved
} else {
const commandDetection = this._capabilities.get(TerminalCapability.CommandDetection);
sb.appendString(!!commandDetection ? this._getShellIntegrationContent() : this._getContent());
}
this._content = sb.build();
meganrogge marked this conversation as resolved.
Show resolved Hide resolved
const model = await this._getTextModel(URI.from({ scheme: AccessibleBufferConstants.Scheme, fragment: this._content }));
if (model) {
this._bufferEditor.setModel(model);
} else {
throw new Error('Could not create model');
}
return model;
}

private async _refresh(): Promise<void> {
const model = await this._updateContent();
const lineNumber = model.getLineCount() - 1;
if (this._lastContentLength !== this._content.length || this._refreshSelection) {
this._bufferEditor.setSelection({ startLineNumber: lineNumber, startColumn: 1, endLineNumber: lineNumber, endColumn: 1 });
this._bufferEditor.setScrollTop(this._bufferEditor.getScrollHeight());
this._refreshSelection = false;
this._lastContentLength = this._content.length;
}
this._accessibleBuffer.replaceChildren(this._editorContainer);
this._bufferEditor.focus();
}

async show(): Promise<void> {
if (!this._registered) {
meganrogge marked this conversation as resolved.
Show resolved Hide resolved
this._bufferEditor.layout({ width: this._xtermElement.clientWidth, height: this._xtermElement.clientHeight });
this._bufferEditor.onKeyDown((e) => {
if (e.keyCode === KeyCode.Escape || e.keyCode === KeyCode.Tab) {
this._hide();
}
});
if (commandDetection) {
this._commandFinishedDisposable = commandDetection.onCommandFinished(() => this._refreshSelection = true);
this.add(this._commandFinishedDisposable);
}
this._registered = true;
this._registerListeners();
}
await this._refresh();
this._accessibleBuffer.tabIndex = -1;
this._accessibleBuffer.classList.add('active');
this._xtermElement.classList.add('hide');
if (this._lastContentLength !== fragment.length || this._refreshSelection) {
let lineNumber = 1;
const lineCount = model?.getLineCount();
if (lineCount && model) {
lineNumber = commandDetection ? lineCount - 1 : lineCount > 2 ? lineCount - 2 : 1;
}

private _registerListeners(): void {
const commandDetection = this._capabilities.get(TerminalCapability.CommandDetection);
this._bufferEditor.layout({ width: this._xtermElement.clientWidth, height: this._xtermElement.clientHeight });
this._bufferEditor.onKeyDown((e) => {
if (e.keyCode === KeyCode.Escape || e.keyCode === KeyCode.Tab) {
this._hide();
}
this._bufferEditor.setSelection({ startLineNumber: lineNumber, startColumn: 1, endLineNumber: lineNumber, endColumn: 1 });
this._bufferEditor.setScrollTop(this._bufferEditor.getScrollHeight());
this._refreshSelection = false;
this._lastContentLength = fragment.length;
});
if (commandDetection) {
this._commandFinishedDisposable = commandDetection.onCommandFinished(() => this._refreshSelection = true);
this.add(this._commandFinishedDisposable);
}
this._accessibleBuffer.replaceChildren(this._editorContainer);
this._bufferEditor.focus();
this._registered = true;
}

private async _getTextModel(resource: URI): Promise<ITextModel | null> {
Expand All @@ -146,7 +172,7 @@ export class AccessibleBufferWidget extends DisposableStore {
const commands = this._capabilities.get(TerminalCapability.CommandDetection)?.commands;
const sb = new StringBuilder(10000);
if (!commands?.length) {
return this._getAllContent();
return this._getContent();
}
for (const command of commands) {
sb.appendString(command.command.replace(new RegExp(' ', 'g'), '\xA0'));
Expand All @@ -159,15 +185,15 @@ export class AccessibleBufferWidget extends DisposableStore {
return sb.build();
}

private _getAllContent(): string {
private _getContent(startLine?: number): string {
const lines: string[] = [];
let currentLine: string = '';
const buffer = this._xterm?.raw.buffer.active;
if (!buffer) {
return '';
}
const end = buffer.length;
for (let i = 0; i < end; i++) {
for (let i = startLine ?? 0; i < end; i++) {
const line = buffer.getLine(i);
if (!line) {
continue;
Expand Down