Git Product home page Git Product logo

Comments (21)

nahtnam avatar nahtnam commented on May 11, 2024 3

@jonschlinkert Excuse the mess, I just extracted this from my project. Select "client" and then press ctrl + c.

const { prompt } = require('enquirer');

const contractor = async () => {
  const register = async () => {
    menu();
  }
  const menu = async () => {
    const { action } = await prompt({
      type: 'select',
      name: 'action',
      message: 'what would you like to do?',
      choices: ['register', 'update', 'mine'],
      initial: 'register',
    });
    switch (action) {
      case 'register': {
        await register();
        break;
      }
      case 'update': {
        console.log('update');
        break;
      }
      case 'mine': {
        console.log('mine');
        break;
      }
      default: {
        console.log('mine');
        break;
      }
    }
  }
  menu();
}

const client = async () => {
  const query = async () => {
    menu();
  }
  const menu = async () => {
    const { action } = await prompt({
      type: 'select',
      name: 'action',
      message: 'what would you like to do?',
      choices: ['query', 'check'],
      initial: 'query',
    });
    switch (action) {
      case 'query': {
        await query();
        break;
      }
      case 'check': {
        console.log('check');
        break;
      }
      default: {
        await query();
        break;
      }
    }
  }
  menu();
}

const program = async () => {
  const { type } = await prompt({
    type: 'select',
    name: 'type',
    message: 'would you like to start a contractor or client?',
    choices: ['contractor', 'client'],
    initial: 'client',
  });

  if (!type || type === 'contractor') {
    return contractor();
  }
  return client();
}

program().catch((err) => {
  console.log(err);
});

from enquirer.

nahtnam avatar nahtnam commented on May 11, 2024 3

Haha, I agree! I wouldn't call it customer service though, maybe community service instead.

from enquirer.

mikeerickson avatar mikeerickson commented on May 11, 2024 2

This is proof that @jonschlinkert and @doowb have put a lot of thought into making this a great tool for CLI developers. And the customer service is top notch

from enquirer.

jonschlinkert avatar jonschlinkert commented on May 11, 2024 1

Any chance of getting something like enquirer.cleanup() to close any pipes/handles that aren't cleaned up when an .ask is done?

@doowb and I just talked about something related (I believe). @doowb did you mention having a local branch with a fix for this?

from enquirer.

jonschlinkert avatar jonschlinkert commented on May 11, 2024 1

Ahhh, got it. It's, fortunately a much simpler issue than what I was thinking (assuming you agree).

Try adding the following right below the require statement:

const { prompt } = require('enquirer');

prompt.on('cancel', () => process.exit());

Sorry about the confusion on this, I just wasn't thinking. The reason it works this way is that you can control whether or not to exit the process.

Let me know if this is what you're looking for, or if you have suggestions for other before.

from enquirer.

mrozbarry avatar mrozbarry commented on May 11, 2024

Wow you guys are fast :)

from enquirer.

mrozbarry avatar mrozbarry commented on May 11, 2024

Also, it appears that enquirer is globbling up my Ctrl + c and process.on("SIGINT", function () { console.log("Ctrl+C pressed!") }) doesn't fire - not sure if this is a related problem, or a separate issue, though.

from enquirer.

doowb avatar doowb commented on May 11, 2024

just talked about something related (I believe).

There's a bug in prompt-base that in some situations the input stream is held open causing the app to stay open, requiring the use of Ctrl + c. I have a fix for that and I'll get it pushed up soon.

it appears that enquirer is globbling up my Ctrl + c and process.on("SIGINT", function () { console.log("Ctrl+C pressed!") }) doesn't fire

This has something to do with readline, but I don't remember off the top of my head how it's handled. I'll look into it to see if this is a problem. It might be that we need to handle Ctrl + c explicitly in enquirer.

We're using cli-cursor to hide the cursor in some prompts, which is supposed to handle restoring the cursor when the process exits. Which node version are you using and are you able to capture a gif showing the bug?

from enquirer.

jonschlinkert avatar jonschlinkert commented on May 11, 2024

Wow you guys are fast :)

haha, well let's see how fast we can get that fix shipped. Sorry it's taking so long, we wanted to do more testing locally before we pushed it up. thanks!

from enquirer.

jonschlinkert avatar jonschlinkert commented on May 11, 2024

It might be that we need to handle Ctrl + c explicitly in enquirer.

I think we already explicitly handle it somewhere, probably readline-utils, but we probably are not doing anything to hide or show the cursor. A pr for this would be great, or we'll try to get this fixed asap.

from enquirer.

jonschlinkert avatar jonschlinkert commented on May 11, 2024

I'm pretty sure this was fixed. We can re-open or create a new issue if anyone still has a problem with this

from enquirer.

vjpr avatar vjpr commented on May 11, 2024

@jonschlinkert @doowb

I'm still seeing this.

When showing a multiselect, if I press ctrl+c, it doesn't ever exit after that.

from enquirer.

doowb avatar doowb commented on May 11, 2024

@vjpr I tried running some of the multiselect examples and I can press Ctrl+c to get it to exit. Can you show the code that you're running that's failing?

from enquirer.

vjpr avatar vjpr commented on May 11, 2024

I am running my app as a child process of another Node app using spawn. In the parent app, I am listening to SIGINT, and then forwarding the process kill command to the child. Maybe its is some interaction with that.

from enquirer.

nahtnam avatar nahtnam commented on May 11, 2024

I am having a similar issue. I can provide an example if no one else is able to reproduce it.

from enquirer.

jonschlinkert avatar jonschlinkert commented on May 11, 2024

I am having a similar issue. I can provide an example if no one else is able to reproduce it.

That would be great, any additional info will help. Thanks!

from enquirer.

jonschlinkert avatar jonschlinkert commented on May 11, 2024

@nahtnam thanks so much for the detail! I'm sorry I didn't see your reply until now.

I'll spend some time reviewing to see if I can wrap my brain around this. Thanks again.

from enquirer.

jonschlinkert avatar jonschlinkert commented on May 11, 2024

Also, to be clear, hitting ctrl+c will exit when you run a prompt directly, like this:

const { Select } = require('enquirer');
const prompt = new Select({
  type: 'select',
  name: 'type',
  message: 'would you like to start a contractor or client?',
  choices: ['contractor', 'client'],
  initial: 'client',
});

prompt.run()
  .then(console.log)
  .catch(console.error);

But when you run pass an array of questions to enquirer it doesn't exit (currently).

I'm not opposed to changing this default behavior if makes more sense to exit. I'd probably prefer that personally, it just didn't get as much thought as it deserved before.

from enquirer.

nahtnam avatar nahtnam commented on May 11, 2024

Hello!

Thanks for taking a look, it makes sense now. I think the opposite would be a better idea. Most people expect the program to quit after you press ctrl + c, but there are definitely applications where you wouldn't want to be allowed to quit. I think it would be better if by default it quit, and you can add a hook to do something else on ctrl + c. However, if it is changed, I think you should bump the version up by 1.0 since it would be a breaking change. As for now, I think adding prompt.on('cancel', () => process.exit()); to the docs would be the best idea.

from enquirer.

jonschlinkert avatar jonschlinkert commented on May 11, 2024

I think the opposite would be a better idea. Most people expect the program to quit after you press ctrl + c,

I think this makes sense. I'll get a patch pushed up soon.

from enquirer.

nahtnam avatar nahtnam commented on May 11, 2024

Sounds good, thank you!

from enquirer.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.