Detect interrupts in terminal

This commit is contained in:
Daniel Imms 2024-04-19 09:36:35 -07:00
parent 9e072d5658
commit c238ded248
No known key found for this signature in database
GPG key ID: E5CF412B63651C69
2 changed files with 16 additions and 3 deletions

View file

@ -17,6 +17,7 @@ const enum PromptInputState {
Unknown,
Input,
Execute,
Interrupt,
}
/**
@ -125,6 +126,7 @@ export class PromptInputModel extends Disposable implements IPromptInputModel {
this._value = '';
this._cursorIndex = 0;
this._onDidStartInput.fire(this._createStateObject());
this._onDidChangeInput.fire(this._createStateObject());
}
private _handleCommandExecuted() {
@ -132,13 +134,15 @@ export class PromptInputModel extends Disposable implements IPromptInputModel {
return;
}
this._state = PromptInputState.Execute;
this._cursorIndex = -1;
const event = this._createStateObject();
if (this._lastUserInput === '\u0003') { // ETX end of text (ctrl+c)
if (this._state === PromptInputState.Interrupt) {
this._onDidInterrupt.fire(event);
}
this._state = PromptInputState.Execute;
this._onDidFinishInput.fire(event);
this._onDidChangeInput.fire(event);
}
@throttle(0)
@ -212,6 +216,15 @@ export class PromptInputModel extends Disposable implements IPromptInputModel {
this._logService.trace(`PromptInputModel#_sync: ${this.getCombinedString()}`);
}
// Check for an interrupt, this is verified using both the last user input and the current
// input.
if (this._lastUserInput === '\u0003') { // ETX end of text (ctrl+c)
if (this._value.endsWith('^C')) {
this._state = PromptInputState.Interrupt;
return;
}
}
if (this._value !== value || this._cursorIndex !== cursorIndex || this._ghostTextIndex !== ghostTextIndex) {
this._value = value;
this._cursorIndex = cursorIndex;

View file

@ -91,7 +91,7 @@ export class CommandDetectionCapability extends Disposable implements ICommandDe
) {
super();
this._promptInputModel = this._register(new PromptInputModel(this._terminal, this.onCommandStarted, this.onCommandFinished, this._logService));
this._promptInputModel = this._register(new PromptInputModel(this._terminal, this.onCommandStarted, this.onCommandExecuted, this._logService));
// Pull command line from the buffer if it was not set explicitly
this._register(this.onCommandExecuted(command => {