npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

cli-select-2

v2.0.0

Published

Simple and interactive solution to provide a list of selectable items on the command line

Downloads

2,772

Readme

Improvements for cli-select-2 vs cli-select

Currently the only difference is that you can use k and j to move up and down

Note: cli-select does not produce colored output by default to keep the dependencies at a minimum. See the examples below on how to reproduce this preview.

Table of contents

Installation

npm install --save cli-select-2

Usage

const cliSelect = require('cli-select-2');

cliSelect(options, callback);

See the configuration section for all available options.

The select list gets immediately rendered when the function gets called and the user can then select an option with the up and down arrow keys. You can also use k for up and j for down (VIM style). To confirm the selection, just press enter/return. It is also possible to cancel a selection with ctrl+c or esc but it is up to you how you want to handle the cancellation of a selection, the process won't be ended by default.

cli-select supports both, a callback function or a Promise (if your node environment supports promises).

Callback

If the callback function is defined, two parameters will get passed where valueId is the numeric index if values is an array or the object key if values is an object. Both parameters will be null if the selection gets cancelled.

cliSelect(options, (valueId, value) => {
    if (valueId !== null) {
        console.log('selected ' + valueId + ': ' + value);
    } else {
        console.log('cancelled');
    }
});

Promise

If no callback function is defined, a Promise will be returned:

cliSelect(options).then((response) => {
    console.log('selected ' + response.id + ': ' + response.value);
}).catch(() => {
    console.log('cancelled');
});

Configuration

Note: All options are optional except values and will default as specified below.

The configuration gets passed to the cliSelect function as a normal object and can contain the following keys:

cliSelect({
    /**
     * All values which can be selected.
     * This can either be an array or an object with string keys and values.
     * If an array is specified, the numerical index gets passed to the callback,
     * if an object is specified, the object key will get passed to the callback.
     * If you use the `valueRenderer` option, you can also pass in any array/object you want,
     * the value can be anything as the `valueRenderer` returns the string to render on the terminal.
     *
     * @type {array|object}
     */
    values: [],

    /**
     * The default value which will be pre-selected when the list shows up.
     * If `values` is an object, the object key can be specified, otherwise
     * it requires the numerical index in the array.
     *
     * @type {number|string}
     */
    defaultValue: 0,

    /**
     * Symbol which gets rendered if an option is selected.
     *
     * @type {string}
     */
    selected: '(x)',

    /**
     * Symbol which gets rendered if an option is not selected.
     *
     * @type {string}
     */
    unselected: '( )',

    /**
     * If you want an indent before the symbol and options,
     * you can specify it here with the number of spaces.
     *
     * @type {number}
     */
    indentation: 0,

    /**
     * If true, the list will get removed from the output
     * once an item gets selected or it gets cancelled.
     *
     * @type {boolean}
     */
    cleanup: true,

    /**
     * If you use a custom object for values or want to render the values differently,
     * you can overwrite it with a custom function.
     * The function gets two parameters passed, the value
     * (which can be a string or your custom object, depending what you have in the `values` option)
     * and a boolean specifying if the value is selected.
     * See the examples below for a simple example.
     *
     * @type {function}
     */
    valueRenderer: (value, selected) => value,

    /**
     * Stream where the output should be written to.
     *
     * @type {Stream}
     */
    outputStream: process.stdout,

    /**
     * Stream to use for the keyboard events.
     *
     * @type {Stream}
     */
    inputStream: process.stdin,
});

Examples

cli-select does not produce colored outputs by default so you can use the package for that which you already have in your project and you don't have two packages doing basically the same at the end. If you don't have such a package already in your project and you want the output to look similar to the preview gif above, I recommend the packages chalk for colored output and figures for unicode symbols with fallbacks. These two packages are also used in the examples below but cli-select is also compatible with every other package.

Custom value renderer

const cliSelect = require('cli-select');
const chalk = require('chalk');

cliSelect({
    values: ['Major', 'Minor', 'Patch'],
    valueRenderer: (value, selected) => {
        if (selected) {
            return chalk.underline(value);
        }

        return value;
    },
}).then(...);

Todo: more examples, also the one in the preview gif

License

MIT