Sitelet https://web.archive.org/web/20201115173106/https://github.com/enquirer/enquirer/issues/33
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

todo: recipes #33

Open
jonschlinkert opened this issue Nov 9, 2018 · 4 comments
Open

todo: recipes #33

jonschlinkert opened this issue Nov 9, 2018 · 4 comments
Labels

Comments

@jonschlinkert
Copy link
Member

@jonschlinkert jonschlinkert commented Nov 9, 2018

  • Multilingual support - show how easy it is to add support for multiple languages.
  • spinners and loading indicators
  • asynchronously loaded choices
  • chained prompts
  • using prompts as choices

What other examples would you like to See?

@jonschlinkert jonschlinkert added the docs label Nov 11, 2018
@aslafy-z
Copy link

@aslafy-z aslafy-z commented Nov 14, 2018

I'd love to have a multi-select editable. This way I could select X tags from a list, and even add choices with some key (+ for example) and automatically select it after all.

@jonschlinkert
Copy link
Member Author

@jonschlinkert jonschlinkert commented Nov 15, 2018

Hi @aslafy-z, I think your request is the same as #30.

@aslafy-z
Copy link

@aslafy-z aslafy-z commented Nov 15, 2018

Thank you for pointing me to the right issue. I must have looked better. It looks great!

@sqren
Copy link

@sqren sqren commented Apr 22, 2020 •

I'm interested in seeing examples for showing loading indicators upon (async) validation.

Case in point: I've made a simple prompt that waits for the user to perform an action (in this case solving a merge conflict), and then submitting (pressing enter). If the merge conflicts were solved the prompt will end successfully, if not it'll require the user to try again. If an exception is thrown by the validation (eg. file access not available) the prompt should end and the exception be re-thrown.

Is the following EnterPrompt the correct way to approach this problem?

const { Prompt } = require("enquirer");

class EnterPrompt extends Prompt {
  constructor({ customValidation, ...options }) {
    super(options);
    this.customValidation = customValidation;
    this.cursorHide();
  }

  async validate() {
    if (this.inProgressPromise) {
      return this.inProgressPromise;
    }

    this.render();
    this.inProgressPromise = this.customValidation();
    const value = await this.inProgressPromise;
    this.inProgressPromise = null;
    return value;
  }

  async render() {
    const { size } = this.state;
    this.clear(size);

    const prefix = await this.prefix();
    const message = await this.message();

    let promptLine;
    // waiting for user to submit
    if (!this.state.submitted) {
      promptLine = `${prefix} ${message}\n\nPress enter to continue`;

      // waiting for async validation
    } else if (
      !this.state.cancelled &&
      this.state.submitted &&
      this.state.validating
    ) {
      promptLine = `${prefix} ${message}\n\nValidating...`;

      // finished
    } else if (this.state.submitted || this.state.cancelled) {
      promptLine = `${prefix} ${message}`;
    }

    this.write(promptLine);
    this.restore();
  }
}

EnterPrompt can then be consumed like (with mock data):

async function init() {
  let i = 0;
  await new EnterPrompt({
    message: "Resolve conflicts",
    async customValidation() {
      i++;
      return new Promise((resolve) => setTimeout(() => resolve(i > 2), 1000));
    }
  }).run();

  console.log("all conflicts resolved!");
}

init().catch((e) => {console.log("Error", e)});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.