SBoudrias / Inquirer.js

A collection of common interactive command line user interfaces.
MIT License
20.09k stars 1.3k forks source link

Provide ability to intercept answers before printing them to terminal. #579

Closed dnutels closed 1 month ago

dnutels commented 7 years ago

Currently, with the following

const {selection} = await inquirer.prompt([{
    type: 'checkbox',
    name: 'selection',
    message: 'Select your favorite color',
    choices: ['red', 'green', 'blue']
}]);

when a user selects any combination (all in the screenshot below): image

image

the choices are printed to the terminal, with no ability to impact that process (that I could find).

In cases where the list is fairly long (and the choices are long), this creates an unsightly block of text.

Can we add a formatter function:

const {selection} = await inquirer.prompt([{
    type: 'checkbox',
    name: 'selection',
    message: 'Select your favorite color',
    choices: ['red', 'green', 'blue'],
    format: (input, answers) => { // similar to validate?
        // format the answers and return a String
    }
}]);

so that the decision what to print and how to format it is in userland?

Thank you.

SBoudrias commented 7 years ago

You can use the short to provide shorter choice option to display in the list. There's not support for fully customized function to change the output.

dnutels commented 7 years ago

Thank you.

This, unfortunately, is rather unsatisfactory, as the values of short are joined, resulting in(when short is _, for example):

 _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _

Would you accept a PR for this? Like, in a couple of days?

igor-sky commented 5 years ago

Is there solution for this in current version? I want to prevent printing result of selection

chamberlainpi commented 5 years ago

+1 for this feature!

I'm using a "checkbox" type prompt to select/deselect a list of files (showing their shortened paths) and when the results show up in the blueish color, it just looks ugly to have them listed in one long running results.join(', ') line.

Inquirer checkbox results too long

Since I get the results back, I'd much rather control the output in a subsequent call to console.log(...) myself.

Is there any work or at least plans to have a way to disable the result output that's automatically spit out by inquirer?

SBoudrias commented 1 month ago

This can be now be done with customizing theme.style.renderSelectedChoices of the checkbox prompt.