Skip to content

Commit c238ded

Browse files
committed
Detect interrupts in terminal
1 parent 9e072d5 commit c238ded

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

src/vs/platform/terminal/common/capabilities/commandDetection/promptInputModel.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const enum PromptInputState {
1717
Unknown,
1818
Input,
1919
Execute,
20+
Interrupt,
2021
}
2122

2223
/**
@@ -125,20 +126,23 @@ export class PromptInputModel extends Disposable implements IPromptInputModel {
125126
this._value = '';
126127
this._cursorIndex = 0;
127128
this._onDidStartInput.fire(this._createStateObject());
129+
this._onDidChangeInput.fire(this._createStateObject());
128130
}
129131

130132
private _handleCommandExecuted() {
131133
if (this._state === PromptInputState.Execute) {
132134
return;
133135
}
134136

135-
this._state = PromptInputState.Execute;
136137
this._cursorIndex = -1;
137138
const event = this._createStateObject();
138-
if (this._lastUserInput === '\u0003') { // ETX end of text (ctrl+c)
139+
if (this._state === PromptInputState.Interrupt) {
139140
this._onDidInterrupt.fire(event);
140141
}
142+
143+
this._state = PromptInputState.Execute;
141144
this._onDidFinishInput.fire(event);
145+
this._onDidChangeInput.fire(event);
142146
}
143147

144148
@throttle(0)
@@ -212,6 +216,15 @@ export class PromptInputModel extends Disposable implements IPromptInputModel {
212216
this._logService.trace(`PromptInputModel#_sync: ${this.getCombinedString()}`);
213217
}
214218

219+
// Check for an interrupt, this is verified using both the last user input and the current
220+
// input.
221+
if (this._lastUserInput === '\u0003') { // ETX end of text (ctrl+c)
222+
if (this._value.endsWith('^C')) {
223+
this._state = PromptInputState.Interrupt;
224+
return;
225+
}
226+
}
227+
215228
if (this._value !== value || this._cursorIndex !== cursorIndex || this._ghostTextIndex !== ghostTextIndex) {
216229
this._value = value;
217230
this._cursorIndex = cursorIndex;

src/vs/platform/terminal/common/capabilities/commandDetectionCapability.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ export class CommandDetectionCapability extends Disposable implements ICommandDe
9191
) {
9292
super();
9393

94-
this._promptInputModel = this._register(new PromptInputModel(this._terminal, this.onCommandStarted, this.onCommandFinished, this._logService));
94+
this._promptInputModel = this._register(new PromptInputModel(this._terminal, this.onCommandStarted, this.onCommandExecuted, this._logService));
9595

9696
// Pull command line from the buffer if it was not set explicitly
9797
this._register(this.onCommandExecuted(command => {

0 commit comments

Comments
 (0)