How can I implement keypress on a cli with inquirer?

230 views Asked by At

I created a CLI in typescript using Inquirer but I can't implement a logic to support the command ctrl+v! I found, on inquirer/core, the method useKeyPress but I don't understand how I can use it in a classic prompt like:

const answer = await inquirer.prompt([
{
  type: 'input',
  name: 'question',
  message: 'Insert someting:',
validate: function (value: string) {
  if (!value) {
    return "IYou must insert something.";
  }
  return true;
},
},    
]);

Can someone help me?

edit: Thankfully to @jsejcksn I create this class: import { createPrompt, useState, useKeypress, usePrefix, isEnterKey, isBackspaceKey, AsyncPromptConfig, } from '@inquirer/core'; import type {} from '@inquirer/type'; type InputConfig = AsyncPromptConfig & { default?: string; transformer?: (value: string, { isFinal }: { isFinal: boolean }) => string; };

export default createPrompt<string, InputConfig>((config, done) => { const [status, setStatus] = useState('pending'); const [defaultValue, setDefaultValue] = useState<string | undefined>(config.default); const [errorMsg, setError] = useState<string | undefined>(undefined); const [value, setValue] = useState('');

const isLoading = status === 'loading'; const prefix = usePrefix(isLoading);

useKeypress(async (key, rl) => { // Ignore keypress while our prompt is doing other processing. if (status !== 'pending') { return; }

if (isEnterKey(key)) {
  const answer = value || defaultValue || '';
  setStatus('loading');
  const isValid = await config.validate(answer);
  if (isValid === true) {
    setValue(answer);
    setStatus('done');
    done(answer);
  } else {
    // TODO: Can we keep the value after validation failure?
    // `rl.line = value` works but it looses the cursor position.
    setValue('');
    setError(isValid || 'You must provide a valid value');
    setStatus('pending');
  }
} else if (isBackspaceKey(key) && !value) {
  setDefaultValue(undefined);
} else {
  setValue(rl.line);
  setError(undefined);
}

});

const message = config.message; let formattedValue = value; if (typeof config.transformer === 'function') { formattedValue = config.transformer(value, { isFinal: status === 'done' }); }

let defaultStr = ''; if (defaultValue && status !== 'done' && !value) { defaultStr = (${defaultValue}); }

let error = ''; if (errorMsg) { error = > ${errorMsg}; } return [${prefix} ${message}${defaultStr} ${formattedValue}, error]; });

but the compiler returns: error TS2345: Argument of type '{ type: string; name: string; message: string; validate: (value: string) => true | "Il valore non può essere nullo."; }[]' is not assignable to parameter of type 'InputConfig'. Property 'message' is missing in type '{ type: string; name: string; message: string; validate: (value: string) => true | "IYou must insert something."; }[]' but required in type 'AsyncPromptConfig'. and also: error TS2339: Property 'question' does not exist on type 'string'.

where I try to retrieve the answer of the question!

0

There are 0 answers