Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

simple-terminal-menu

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

simple-terminal-menu

A menu that is easier to use on the terminal than terminal-menu

  • 1.1.3
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

simple-terminal-menu

terminal-menu does a good job for starting a menu but it is a bit tedious to set-up reliably for process.stdin / process.stdout and also for the use with double width characters.

For simply taking charge of the power this terminal menu offers a few things:

Automatically connects to process.stdin/stdout

You don't have to do that, it will just work :)

Markers

.add gets an new signature

.add(<label>[, <marker>][, <cb>])

With this you can add entries that have a right-aligned marker text shown.

Menu Items

You can also use .addItem to use objects to add menu items.

.addItem({
  label: "<label>",
  marker: "<marker>",
  handler: "<cb>"
})

Separators

Just use .writeSeparator() to create a separator line.

Automatic truncating of entries

If an entry exceeds the width of the menu it will be truncated with opts.truncator or ...

Writing of text

Similar like .add it supports .writeLine that allows you to write a text that is both left & right aligned.

.writeLine(<left>[, <right>])

tty Tests

If the terminal doesn't support TTY the will just return null!

Comfort functions

To write a nice title and subtitle the comfort functions .writeTitle and .writeSubtitle exist.

Factory

If you have several menus that need to look alike, you can use the factory. It is available via require(simple-terminal-menu/factory).

Installation & Usage

Install it using npm

npm install simple-terminal-menu --save

And then create a menu it in your code using

var createMenu = require('../simple-terminal-menu')

function showSelection(label, marker) {
  console.log("label: " + label + "; marker: " + marker + ";")
}

function mainMenu() {
  var menu = createMenu({ // settings passed through to terminal-menu
    x: 3,
    y: 2
  })
  menu.writeLine("My Menu", "(tm)")
  menu.writeSeparator()
  menu.add("A", "[selected]", showSelection)
  menu.add("B", showSelection)
  menu.writeSeparator()
  menu.add("open submenu", subMenu)
  menu.add("exit", menu.close)
}

function subMenu() {
  var menu = createMenu()
  menu.writeLine("SubMenu")
  menu.writeSeparator()
  menu.add("C", "[selected]", showSelection)
  menu.add("D", showSelection)
  menu.writeSeparator()
  menu.add("cancel", mainMenu)
  menu.add("niceTitle", nicelyTitledMenu)
  menu.add("exit", menu.close)
}

function nicelyTitledMenu() {
  var menu = createMenu();
  menu.writeTitle("Awesome window")
  menu.writeSubtitle("A little more colorful")
  menu.writeSeperator()
  menu.add("cancel", subMenu)
  menu.add("factoryA", factoryMenuA)
  menu.add("exit", menu.close)
}


// Options for the menu when created through the factory
var factoryMenuOptions = {} // Can be empty! the factory uses some sensible defaults!

// Defaults for creating menu with the factory
var defaultFactoryOptions = {
  title: "Factory Title",
  // you could also specify `subtitle:`, menu & extras are not available.
}
var factory = require('simple-terminal-menu/factory')(factoryMenuOptions, defaultFactoryOptions);

function factoryMenuA() {
  factory.create({
    subtitle: "factory-a",
    menu: [{
      label: "E",
      handler: showSelection
    }, {
      label: "F",
      handler: showSelection
    }],
    extras: [{
        label: "factoryB",
        handler: factoryMenuB
      },{
        label: "cancel",
        handler: nicelyTitledMenu
      }]
  })
}

function factoryMenuB() {
  factory.create({
    subtitle: "factory-b",
    menu: [{
        label: "G",
        handler: showSelection
      }],
    extras: [{
        label: "factoryA",
        handler: factoryMenuA
      },{
        label: "cancel",
        handler: nicelyTitledMenu
      }]
}


mainMenu()

Keywords

FAQs

Package last updated on 13 May 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc