Inquirer.js icon indicating copy to clipboard operation
Inquirer.js copied to clipboard

How do I stop Inquirer / close its readline?

Open AaronAcerboni opened this issue 4 years ago • 7 comments

My issue is essentially this https://github.com/SBoudrias/Inquirer.js/issues/894 but sort of in reverse.

I am using Inquirer as well as another cli/terminal library (blessed) in the same program.

I want to use inquirer just for the first part of my app then switch fully to blessed. The problem is that I don't know how to close the readline input started from Inquirers prompt() method. It remains open for whatever reason and causes trouble (characters double up) when typing in the input prompt made by the blessed library.

I've tried wrapping my init call in a setTimeout to try and let Inquirer "finish" but that hasn't worked.

I hope that's all clear.

AaronAcerboni avatar Jul 09 '20 15:07 AaronAcerboni

same issue

shavyg2 avatar Jul 16 '20 16:07 shavyg2

same. Inquirer does not seem to play nice with other CLI systems, for example using readline.createInterface.prompt() as your CLI interface will get stuck if you try to use inquirer inside of it as an option for users.

AdamBD avatar Nov 27 '20 15:11 AdamBD

Yeah, I'm trying to use readline, even though I don't want to, and am only doing so because they never fixed the bug in #214, and it won't work. It outputs the readline prompt, and then exits the script before I can type anything in. This is the only problem stopping me from having a finished project.

honestly, just wtf

LeftoverChineseFood avatar Mar 04 '21 19:03 LeftoverChineseFood

I have the same issue but i want to use blessed for an intro and then switch to inquirer, so i get double key input.

Trackhe avatar Mar 05 '21 12:03 Trackhe

ooof, yeah Inquirer doesn't work well with competitors. Just imagine like Windows & Linux.

sorry, you'll probably have to choose one.

LeftoverChineseFood avatar Mar 05 '21 14:03 LeftoverChineseFood

I just encountered the same issue and I can close/exit an Inquirer prompt by calling ui.close() method like this, I don't know if it will solve your issue.

Here is an example :

// Exit the inquirer prompt
function exit () {
  prompt.ui.close();
}

// close inquirer input if user press "escape" key
process.stdin.on('keypress', (_, key) => {
  if (key.name === "escape") {
    exit();
  }
});

// Declare your prompt and save it
const prompt = inquirer.prompt({
  name: "myInput",
  type: "input",
  // ...
});

// Listen when the user submit a value
prompt.then( (values) => {
  console.log( values['myInput'] );
  // ...
})

arthur-eudeline avatar Mar 30 '21 16:03 arthur-eudeline

nice, thank you!

LeftoverChineseFood avatar Apr 06 '21 18:04 LeftoverChineseFood

I just encountered the same issue and I can close/exit an Inquirer prompt by calling ui.close() method like this, I don't know if it will solve your issue.

Here is an example :

// Exit the inquirer prompt
function exit () {
  prompt.ui.close();
}

// close inquirer input if user press "escape" key
process.stdin.on('keypress', (_, key) => {
  if (key.name === "escape") {
    exit();
  }
});

@arthur-eudeline Thank you for this code. I just wanted to give the user the option to exit out of the Command Line Prompts by pressing the esc key and your code above did just this!!! THANK YOU 🥇

R0bsaunders avatar Feb 19 '23 11:02 R0bsaunders

The new API has a cleaner & documented way to cancel a prompt. Details on the README

I'll close this ticket as there's multiple solution in the thread for the previous API, and this version is now only in maintenance mode.

SBoudrias avatar Jun 03 '23 20:06 SBoudrias