@sussudio/platform
Version:
Internal APIs for VS Code's service injection the base services.
63 lines (62 loc) • 2.02 kB
JavaScript
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { timeout } from '@sussudio/base/common/async.mjs';
import { Disposable } from '@sussudio/base/common/lifecycle.mjs';
import { isWindows } from '@sussudio/base/common/platform.mjs';
/**
* Tracks a terminal process's data stream and responds immediately when a matching string is
* received. This is done in a low overhead way and is ideally run on the same process as the
* where the process is handled to minimize latency.
*/
export class TerminalAutoResponder extends Disposable {
_pointer = 0;
_paused = false;
/**
* Each reply is throttled by a second to avoid resource starvation and responding to screen
* reprints on Winodws.
*/
_throttled = false;
constructor(proc, matchWord, response, logService) {
super();
this._register(
proc.onProcessData((e) => {
if (this._paused || this._throttled) {
return;
}
const data = typeof e === 'string' ? e : e.data;
for (let i = 0; i < data.length; i++) {
if (data[i] === matchWord[this._pointer]) {
this._pointer++;
} else {
this._reset();
}
// Auto reply and reset
if (this._pointer === matchWord.length) {
logService.debug(`Auto reply match: "${matchWord}", response: "${response}"`);
proc.input(response);
this._throttled = true;
timeout(1000).then(() => (this._throttled = false));
this._reset();
}
}
}),
);
}
_reset() {
this._pointer = 0;
}
/**
* No auto response will happen after a resize on Windows in case the resize is a result of
* reprinting the screen.
*/
handleResize() {
if (isWindows) {
this._paused = true;
}
}
handleInput() {
this._paused = false;
}
}