This post was written for Nodejitsu blog and is crossposted here. Nodejitsu loved what I was doing at npmawesome.com and is now supporting the project.

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 blogn');
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!