inquirerer 2.3.0 → 2.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/esm/keypress.js CHANGED
@@ -13,25 +13,27 @@ export const KEY_CODES = {
13
13
  BACKSPACE_LEGACY: '\x08' // For compatibility with some systems
14
14
  };
15
15
  const sharedInputStates = new WeakMap();
16
- function getOrCreateSharedState(input, proc) {
16
+ function getOrCreateSharedState(input) {
17
17
  let state = sharedInputStates.get(input);
18
18
  if (!state) {
19
19
  const dataHandler = (key) => {
20
20
  const currentState = sharedInputStates.get(input);
21
21
  if (!currentState)
22
22
  return;
23
- for (const instance of currentState.instances) {
24
- if (instance.isActive()) {
25
- instance.handleKey(key);
23
+ // Only dispatch to the top of the active stack (current owner)
24
+ const owner = currentState.activeStack[currentState.activeStack.length - 1];
25
+ if (owner) {
26
+ owner.handleKey(key);
27
+ // Handle Ctrl+C via the current owner's process wrapper
28
+ if (key === KEY_CODES.CTRL_C) {
29
+ owner.exitProcess(0);
26
30
  }
27
31
  }
28
- if (key === KEY_CODES.CTRL_C) {
29
- proc.exit(0);
30
- }
31
32
  };
32
33
  state = {
33
34
  dataHandler,
34
35
  instances: new Set(),
36
+ activeStack: [],
35
37
  rawModeSet: false
36
38
  };
37
39
  sharedInputStates.set(input, state);
@@ -44,17 +46,25 @@ function removeFromSharedState(input, instance) {
44
46
  if (!state)
45
47
  return;
46
48
  state.instances.delete(instance);
49
+ // Remove from active stack as well
50
+ const stackIndex = state.activeStack.indexOf(instance);
51
+ if (stackIndex !== -1) {
52
+ state.activeStack.splice(stackIndex, 1);
53
+ }
54
+ // If stack is now empty, disable raw mode
55
+ if (state.activeStack.length === 0 && state.rawModeSet) {
56
+ if (typeof input.setRawMode === 'function') {
57
+ input.setRawMode(false);
58
+ }
59
+ state.rawModeSet = false;
60
+ }
47
61
  if (state.instances.size === 0) {
48
62
  input.removeListener('data', state.dataHandler);
49
63
  sharedInputStates.delete(input);
50
- if (state.rawModeSet && typeof input.setRawMode === 'function') {
51
- input.setRawMode(false);
52
- }
53
64
  }
54
65
  }
55
66
  export class TerminalKeypress {
56
67
  listeners = {};
57
- active = true;
58
68
  noTty;
59
69
  input;
60
70
  proc;
@@ -70,17 +80,26 @@ export class TerminalKeypress {
70
80
  this.registerWithSharedState();
71
81
  }
72
82
  registerWithSharedState() {
73
- const state = getOrCreateSharedState(this.input, this.proc);
83
+ const state = getOrCreateSharedState(this.input);
74
84
  state.instances.add(this);
75
85
  }
76
86
  isTTY() {
77
87
  return !this.noTty;
78
88
  }
79
89
  isActive() {
80
- return this.active && !this.destroyed;
90
+ if (this.destroyed)
91
+ return false;
92
+ const state = sharedInputStates.get(this.input);
93
+ if (!state)
94
+ return false;
95
+ // Active only if this instance is the current owner (top of stack)
96
+ return state.activeStack[state.activeStack.length - 1] === this;
97
+ }
98
+ exitProcess(code) {
99
+ this.proc.exit(code);
81
100
  }
82
101
  handleKey(key) {
83
- if (!this.active || this.destroyed)
102
+ if (this.destroyed)
84
103
  return;
85
104
  const handlers = this.listeners[key];
86
105
  handlers?.forEach(handler => handler());
@@ -103,24 +122,44 @@ export class TerminalKeypress {
103
122
  this.listeners = {};
104
123
  }
105
124
  pause() {
106
- this.active = false;
107
- this.clearHandlers();
125
+ const state = sharedInputStates.get(this.input);
126
+ if (!state)
127
+ return;
128
+ // Remove from active stack (from anywhere, not just top)
129
+ const stackIndex = state.activeStack.indexOf(this);
130
+ if (stackIndex !== -1) {
131
+ state.activeStack.splice(stackIndex, 1);
132
+ }
133
+ // If stack is now empty, disable raw mode
134
+ if (state.activeStack.length === 0 && state.rawModeSet) {
135
+ if (this.isTTY() && typeof this.input.setRawMode === 'function') {
136
+ this.input.setRawMode(false);
137
+ }
138
+ state.rawModeSet = false;
139
+ }
108
140
  }
109
141
  resume() {
110
- this.active = true;
142
+ if (this.destroyed)
143
+ return;
144
+ const state = sharedInputStates.get(this.input);
145
+ if (!state)
146
+ return;
147
+ // Move-to-top semantics: remove from anywhere in stack, then push to top
148
+ const existingIndex = state.activeStack.indexOf(this);
149
+ if (existingIndex !== -1) {
150
+ state.activeStack.splice(existingIndex, 1);
151
+ }
152
+ state.activeStack.push(this);
153
+ // Enable raw mode if TTY
111
154
  if (this.isTTY() && typeof this.input.setRawMode === 'function') {
112
155
  this.input.setRawMode(true);
113
- const state = sharedInputStates.get(this.input);
114
- if (state) {
115
- state.rawModeSet = true;
116
- }
156
+ state.rawModeSet = true;
117
157
  }
118
158
  }
119
159
  destroy() {
120
160
  if (this.destroyed)
121
161
  return;
122
162
  this.destroyed = true;
123
- this.active = false;
124
163
  this.clearHandlers();
125
164
  removeFromSharedState(this.input, this);
126
165
  const state = sharedInputStates.get(this.input);
package/keypress.d.ts CHANGED
@@ -16,7 +16,6 @@ export declare const KEY_CODES: {
16
16
  };
17
17
  export declare class TerminalKeypress {
18
18
  private listeners;
19
- private active;
20
19
  private noTty;
21
20
  private input;
22
21
  private proc;
@@ -25,6 +24,7 @@ export declare class TerminalKeypress {
25
24
  private registerWithSharedState;
26
25
  isTTY(): boolean;
27
26
  isActive(): boolean;
27
+ exitProcess(code?: number): void;
28
28
  handleKey(key: string): void;
29
29
  on(key: string, callback: KeyHandler): void;
30
30
  off(key: string, callback: KeyHandler): void;
package/keypress.js CHANGED
@@ -16,25 +16,27 @@ exports.KEY_CODES = {
16
16
  BACKSPACE_LEGACY: '\x08' // For compatibility with some systems
17
17
  };
18
18
  const sharedInputStates = new WeakMap();
19
- function getOrCreateSharedState(input, proc) {
19
+ function getOrCreateSharedState(input) {
20
20
  let state = sharedInputStates.get(input);
21
21
  if (!state) {
22
22
  const dataHandler = (key) => {
23
23
  const currentState = sharedInputStates.get(input);
24
24
  if (!currentState)
25
25
  return;
26
- for (const instance of currentState.instances) {
27
- if (instance.isActive()) {
28
- instance.handleKey(key);
26
+ // Only dispatch to the top of the active stack (current owner)
27
+ const owner = currentState.activeStack[currentState.activeStack.length - 1];
28
+ if (owner) {
29
+ owner.handleKey(key);
30
+ // Handle Ctrl+C via the current owner's process wrapper
31
+ if (key === exports.KEY_CODES.CTRL_C) {
32
+ owner.exitProcess(0);
29
33
  }
30
34
  }
31
- if (key === exports.KEY_CODES.CTRL_C) {
32
- proc.exit(0);
33
- }
34
35
  };
35
36
  state = {
36
37
  dataHandler,
37
38
  instances: new Set(),
39
+ activeStack: [],
38
40
  rawModeSet: false
39
41
  };
40
42
  sharedInputStates.set(input, state);
@@ -47,17 +49,25 @@ function removeFromSharedState(input, instance) {
47
49
  if (!state)
48
50
  return;
49
51
  state.instances.delete(instance);
52
+ // Remove from active stack as well
53
+ const stackIndex = state.activeStack.indexOf(instance);
54
+ if (stackIndex !== -1) {
55
+ state.activeStack.splice(stackIndex, 1);
56
+ }
57
+ // If stack is now empty, disable raw mode
58
+ if (state.activeStack.length === 0 && state.rawModeSet) {
59
+ if (typeof input.setRawMode === 'function') {
60
+ input.setRawMode(false);
61
+ }
62
+ state.rawModeSet = false;
63
+ }
50
64
  if (state.instances.size === 0) {
51
65
  input.removeListener('data', state.dataHandler);
52
66
  sharedInputStates.delete(input);
53
- if (state.rawModeSet && typeof input.setRawMode === 'function') {
54
- input.setRawMode(false);
55
- }
56
67
  }
57
68
  }
58
69
  class TerminalKeypress {
59
70
  listeners = {};
60
- active = true;
61
71
  noTty;
62
72
  input;
63
73
  proc;
@@ -73,17 +83,26 @@ class TerminalKeypress {
73
83
  this.registerWithSharedState();
74
84
  }
75
85
  registerWithSharedState() {
76
- const state = getOrCreateSharedState(this.input, this.proc);
86
+ const state = getOrCreateSharedState(this.input);
77
87
  state.instances.add(this);
78
88
  }
79
89
  isTTY() {
80
90
  return !this.noTty;
81
91
  }
82
92
  isActive() {
83
- return this.active && !this.destroyed;
93
+ if (this.destroyed)
94
+ return false;
95
+ const state = sharedInputStates.get(this.input);
96
+ if (!state)
97
+ return false;
98
+ // Active only if this instance is the current owner (top of stack)
99
+ return state.activeStack[state.activeStack.length - 1] === this;
100
+ }
101
+ exitProcess(code) {
102
+ this.proc.exit(code);
84
103
  }
85
104
  handleKey(key) {
86
- if (!this.active || this.destroyed)
105
+ if (this.destroyed)
87
106
  return;
88
107
  const handlers = this.listeners[key];
89
108
  handlers?.forEach(handler => handler());
@@ -106,24 +125,44 @@ class TerminalKeypress {
106
125
  this.listeners = {};
107
126
  }
108
127
  pause() {
109
- this.active = false;
110
- this.clearHandlers();
128
+ const state = sharedInputStates.get(this.input);
129
+ if (!state)
130
+ return;
131
+ // Remove from active stack (from anywhere, not just top)
132
+ const stackIndex = state.activeStack.indexOf(this);
133
+ if (stackIndex !== -1) {
134
+ state.activeStack.splice(stackIndex, 1);
135
+ }
136
+ // If stack is now empty, disable raw mode
137
+ if (state.activeStack.length === 0 && state.rawModeSet) {
138
+ if (this.isTTY() && typeof this.input.setRawMode === 'function') {
139
+ this.input.setRawMode(false);
140
+ }
141
+ state.rawModeSet = false;
142
+ }
111
143
  }
112
144
  resume() {
113
- this.active = true;
145
+ if (this.destroyed)
146
+ return;
147
+ const state = sharedInputStates.get(this.input);
148
+ if (!state)
149
+ return;
150
+ // Move-to-top semantics: remove from anywhere in stack, then push to top
151
+ const existingIndex = state.activeStack.indexOf(this);
152
+ if (existingIndex !== -1) {
153
+ state.activeStack.splice(existingIndex, 1);
154
+ }
155
+ state.activeStack.push(this);
156
+ // Enable raw mode if TTY
114
157
  if (this.isTTY() && typeof this.input.setRawMode === 'function') {
115
158
  this.input.setRawMode(true);
116
- const state = sharedInputStates.get(this.input);
117
- if (state) {
118
- state.rawModeSet = true;
119
- }
159
+ state.rawModeSet = true;
120
160
  }
121
161
  }
122
162
  destroy() {
123
163
  if (this.destroyed)
124
164
  return;
125
165
  this.destroyed = true;
126
- this.active = false;
127
166
  this.clearHandlers();
128
167
  removeFromSharedState(this.input, this);
129
168
  const state = sharedInputStates.get(this.input);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "inquirerer",
3
- "version": "2.3.0",
3
+ "version": "2.3.1",
4
4
  "author": "Constructive <developers@constructive.io>",
5
5
  "description": "TypeScript-first library for building beautiful CLI interfaces with interactive prompts",
6
6
  "main": "index.js",
@@ -41,5 +41,5 @@
41
41
  "clean-ansi": "0.1.5",
42
42
  "makage": "0.1.8"
43
43
  },
44
- "gitHead": "264e5515ffaa2089b420f7c96afc287eec93196b"
44
+ "gitHead": "b8ebff70a30df8ee4fb60794f7ba7842730ba199"
45
45
  }