Skip to content

Commit c2d9915

Browse files
committed
fix(shortcuts): re-enter raw mode after each shortcut action
A Node child whose stdout/stderr is the terminal restores, on exit, the terminal mode it saw at startup. The Vite dev server starts before the shortcuts attach, so when R (or w) stops it the terminal drops back to cooked mode: every later key is echoed by the terminal and only reaches the CLI after Enter, which looks like an empty shortcut table. Re-apply raw mode once an action finishes, clearing it first since Node ignores setRawMode(true) while it believes the terminal is already raw.
1 parent 095d91e commit c2d9915

2 files changed

Lines changed: 60 additions & 2 deletions

File tree

‎lib/services/key-shortcuts.ts‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,16 @@ export function findShortcut<TContext extends KeyContextBase>(
9898
return shortcut;
9999
}
100100

101+
/**
102+
* Node skips `setRawMode(true)` while it believes the terminal is already
103+
* raw, so the mode is cleared first to make the call reach the terminal even
104+
* when something else has changed it since.
105+
*/
106+
function enterRawMode(stdin: NodeJS.ReadStream): void {
107+
stdin.setRawMode(false);
108+
stdin.setRawMode(true);
109+
}
110+
101111
/**
102112
* Raw mode outlives the process that set it, so a CI runner or a redirected
103113
* stdin must never get it; `NS_KEY_SHORTCUTS=false` is the manual opt-out.
@@ -428,8 +438,7 @@ export class KeyShortcutService implements IKeyShortcutService {
428438
return false;
429439
}
430440

431-
stdin.setRawMode(false);
432-
stdin.setRawMode(true);
441+
enterRawMode(stdin);
433442
stdin.resume();
434443
stdin.on("data", this.onData);
435444
process.once("exit", this.onExit);
@@ -592,6 +601,12 @@ export class KeyShortcutService implements IKeyShortcutService {
592601
} finally {
593602
this.running = false;
594603
if (process.stdin.setRawMode) {
604+
// A Node child writing to the terminal, such as the Vite dev server
605+
// an action stops, restores the terminal mode it started with when
606+
// it exits; that can be cooked mode, where keys wait for Enter.
607+
if (this.attached && process.stdin.isTTY) {
608+
enterRawMode(process.stdin);
609+
}
595610
process.stdin.resume();
596611
}
597612
}

‎test/services/key-shortcuts.ts‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,16 @@ import {
1919
class FakeStdin extends EventEmitter {
2020
public isTTY: boolean = true;
2121
public rawMode: boolean = null;
22+
/** The mode the terminal is really in, which another process can change. */
23+
public terminalRaw: boolean = false;
2224
public resumeCount: number = 0;
2325
public pauseCount: number = 0;
2426

2527
public setRawMode(value: boolean): any {
28+
// Like Node, only a change from the mode it last set reaches the terminal.
29+
if (value !== this.rawMode) {
30+
this.terminalRaw = value;
31+
}
2632
this.rawMode = value;
2733
return this;
2834
}
@@ -697,6 +703,43 @@ describe("key shortcuts", () => {
697703
assert.equal(stdin.pauseCount, 1);
698704
});
699705

706+
it("takes the terminal back into raw mode after an action", async () => {
707+
service.attach({
708+
shortcuts: [
709+
{
710+
key: "R",
711+
description: "Restart",
712+
action: () => {
713+
// A child that shared the terminal exits and restores the
714+
// cooked mode it started with.
715+
stdin.terminalRaw = false;
716+
},
717+
},
718+
],
719+
});
720+
assert.isTrue(stdin.terminalRaw);
721+
722+
await press("R");
723+
724+
assert.isTrue(stdin.terminalRaw);
725+
});
726+
727+
it("leaves the terminal alone after an action that detached", async () => {
728+
service.attach({
729+
shortcuts: [
730+
{
731+
key: "x",
732+
description: "Detach",
733+
action: () => service.detach(),
734+
},
735+
],
736+
});
737+
738+
await press("x");
739+
740+
assert.isFalse(stdin.terminalRaw);
741+
});
742+
700743
it("tears down when the process exits", () => {
701744
const before = new Set(process.listeners("exit"));
702745
service.attach({ shortcuts: [] });

0 commit comments

Comments
 (0)