Nodejitsu

Save time managing and deploying your node.js app

npmawesome: Multiple choice user prompt for CLI tools using terminal-menu

About the author

Name
Location
Worldwide
nodejitsu nodejitsu

Other popular posts

- Scaling Isomorphic Javascript Code - Keep a node.js server up with Forever - Package.json dependencies done right
- npm cheat sheet - 6 Must Have Node.js Modules
Sign up to our platform for free - get $20 usage

This is a guest post from Alex Gorbatchev and npmawesome.com. Nodejitsu loved what Alex was doing at npmawesome.com and is now supporting the project. Like what you see here? Why don't you contribute on Github?

Lets continue building out awesome CLI (command line interface) tool. Previously we have discussed how to take in command line arguments, output tabular data and display progress of long running tasks.

Prompting a user with options and asking to pick one is a fairly common scenario. Most of the time you get to read the options and press associated key. That works, but we are human and like pretty interfaces. Same thing can be achieved with a common menu interface. This is where terminal-menu (Github: substack/terminal-menu, License: MIT) by James Halliday comes in.

npm install terminal-menu

Features

There are a few options available to customize the menu such as:

  • Background color
  • Foreground color
  • Position
  • Width
  • Padding

Usage

var terminalMenu = require('terminal-menu');

var menu = terminalMenu({
  width: 60,
  x: 4,
  y: 2
});

menu.reset();
menu.write('Recently on our blog\n');
menu.write('-------------------------\n');

menu.add('How to setup a multi-user Ghost blog');
menu.add('Patched PaaS Vulnerability');
menu.add('npmawesome: Progress reporting in CLI applications');
menu.add('Improved SSL experience for private npm');
menu.add('npmawesome: Table formatting in the terminal with...');

menu.on('select', function (label, index) {
  menu.close();
  console.log('You selected:');
  console.log(index, label);
});

menu.createStream().pipe(process.stdout);

Check out interactive example on runnable.

Other modules to checkout

Generally there is a list of alternative modules here, but as strange as it sounds, I couldn't find any. If you know of one, please drop us a line.

Closing thoughts

Over the last couple of CLI related posts we've progressively been improving user experience of our terminal application. I feel that you can never spend too much time doing this, but you can definetely spend too little. I love little things that feel good. terminal-menu was created for stream-adventure, an educational terminal based stream adventure game for Node.js and stream-adventure in turn inspired browser based version written by John Resig of the jQuery fame. Both are pretty cool and fun!