MCPcopy
hub / github.com/bombshell-dev/clack / onKeypress

Method onKeypress

packages/core/src/prompts/prompt.ts:212–264  ·  view source on GitHub ↗
(char: string | undefined, key: Key)

Source from the content-addressed store, hash-verified

210 }
211
212 private onKeypress(char: string | undefined, key: Key) {
213 if (this._track && key.name !== 'return') {
214 if (key.name && this._isActionKey(char, key)) {
215 this.rl?.write(null, { ctrl: true, name: 'h' });
216 }
217 this._cursor = this.rl?.cursor ?? 0;
218 this._setUserInput(this.rl?.line);
219 }
220
221 if (this.state === 'error') {
222 this.state = 'active';
223 }
224 if (key?.name) {
225 if (!this._track && settings.aliases.has(key.name)) {
226 this.emit('cursor', settings.aliases.get(key.name));
227 }
228 if (settings.actions.has(key.name as Action)) {
229 this.emit('cursor', key.name as Action);
230 }
231 }
232 if (char && (char.toLowerCase() === 'y' || char.toLowerCase() === 'n')) {
233 this.emit('confirm', char.toLowerCase() === 'y');
234 }
235
236 // Call the key event handler and emit the key event
237 this.emit('key', char, key);
238
239 if (key?.name === 'return' && this._shouldSubmit(char, key)) {
240 if (this.opts.validate) {
241 const problem = runValidation(this.opts.validate, this.value);
242 if (problem) {
243 this.error = problem instanceof Error ? problem.message : problem;
244 this.state = 'error';
245 this.rl?.write(this.userInput);
246 }
247 }
248 if (this.state !== 'error') {
249 this.state = 'submit';
250 }
251 }
252
253 if (isActionKey([char, key?.name, key?.sequence], 'cancel')) {
254 this.state = 'cancel';
255 }
256
257 if (this.state === 'submit' || this.state === 'cancel') {
258 this.emit('finalize');
259 }
260 this.render();
261 if (this.state === 'submit' || this.state === 'cancel') {
262 this.close();
263 }
264 }
265
266 protected close() {
267 this.input.unpipe();

Callers

nothing calls this directly

Calls 8

_isActionKeyMethod · 0.95
_setUserInputMethod · 0.95
emitMethod · 0.95
_shouldSubmitMethod · 0.95
renderMethod · 0.95
closeMethod · 0.95
runValidationFunction · 0.85
isActionKeyFunction · 0.85

Tested by

no test coverage detected