node-menu
TypeScript icon, indicating that this package has built-in type declarations

1.3.2 • Public • Published

node-menu

This module allows to create console menu for your REPL application. It allows you to register menu items with their handlers. Optionally you may provide handler's owner object and list of arguments being passed to handler.

Installation

npm install node-menu

Methods

var menu = require('node-menu');

Each method returns reference on self object, so calls could be chained.

menu.addItem(title, handler, owner, args)

Add item to the menu. Returns menu for chaining calls.

  • title - title of the menu item;
  • handler - item handler function;
  • owner - owner object of the function (this);
  • args - array of objects argument names being passed to the function, argument is an object with two fields: 'name' and 'type'. Available types are: 'numeric', 'bool' and 'string';
menu.addItem(
    'Menu Item',
    function(str, bool, num1, num2) {
        console.log('String: ' + str);
        if (bool) {
            console.log('bool is true');
        } else {
            console.log('bool is false');
        }
        var sum = num1 + num2;
        console.log('num1 + num2: ' + sum);
    },
    null,
    [
        {'name': 'Str Arg', 'type': 'string'},
        {'name': 'Bool Arg', 'type': 'bool'},
        {'name': 'num1', 'type': 'numeric'},
        {'name': 'num2', 'type': 'numeric'}
    ]);

menu.addDelimiter(delimiter, cnt, title)

Adds delimiter to the menu. Returns menu for chaining calls.

  • delimiter - delimiter character;
  • cnt - delimiter's repetition count;
  • title - title of the delimiter, will be printed in the middle of the delimiter line;

The output of the delimiter:

menu.addDelimiter('-', 33, 'Main Menu')
------------Main Menu------------

menu.addDelimiter('*', 33)
*********************************

menu.enableDefaultHeader()

Turns on default header (turned on by default). Returns menu for chaining calls.

menu.enableDefaultHeader()

menu.disableDefaultHeader()

Turns off default header. No header will be printed in this case. Returns menu for chaining calls.

menu.disableDefaultHeader()

menu.customHeader(customHeaderFunc)

Turns off default header and prints custom header passed in customHeaderFunc. Returns menu for chaining calls.

menu.customHeader(function() {
    process.stdout.write("\nCustom header\n");
})

menu.enableDefaultPrompt()

Turns on default prompt (turned on by default). Returns menu for chaining calls.

menu.enableDefaultPrompt()

menu.disableDefaultPrompt()

Turns off default prompt. No prompt will be printed in this case. Returns menu for chaining calls.

menu.disableDefaultPrompt()

menu.customPrompt(customPromptFunc)

Turns off default prompt and prints custom header passed in customPromptFunc. Returns menu for chaining calls.

menu.customPrompt(function() {
    process.stdout.write("Custom prompt\n");
})

menu.resetMenu()

Clears all data and listeners from the menu object so the object can be updated and reused.

menu.continueCallback(continueCallback)

Set callback which must be invoked when "Enter" button pressed to continue.

menu.resetMenu()

menu.start()

Start menu.

Example

Live Example

Source

var menu = require('node-menu');

var TestObject = function() {
    var self = this;
    self.fieldA = 'FieldA';
    self.fieldB = 'FieldB';
}

TestObject.prototype.printFieldA = function() {
    console.log(this.fieldA);
}

TestObject.prototype.printFieldB = function(arg) {
    console.log(this.fieldB + arg);
}

var testObject = new TestObject();
var timeout;

menu.addDelimiter('-', 40, 'Main Menu')
    .addItem(
        'No parameters',
        function() {
            console.log('No parameters is invoked');
        })
    .addItem(
        "Print Field A",
        testObject.printFieldA,
        testObject)
    .addItem(
        'Print Field B concatenated with arg1',
        testObject.printFieldB,
        testObject,
        [{'name': 'arg1', 'type': 'string'}])
    .addItem(
        'Sum',
        function(op1, op2) {
            var sum = op1 + op2;
            console.log('Sum ' + op1 + '+' + op2 + '=' + sum);
        },
        null,
        [{'name': 'op1', 'type': 'numeric'}, {'name': 'op2', 'type': 'numeric'}])
    .addItem(
        'String and Bool parameters',
        function(str, b) {
            console.log("String is: " + str);
            console.log("Bool is: " + b);
        },
        null,
        [{'name': 'str', 'type': 'string'}, {'name': 'bool', 'type': 'bool'}])
    .addItem(
        'Long lasting task which is terminated when Enter pressed',
        function(time) {
            console.log("Starting long lasting job for " + time + " sec");
            timeout = setTimeout(function() {
                console.log("Long lasting job is done");
                timeout = undefined;
            }, time * 1000);
        },
        null,
        [{'name': 'Job execution time, sec', 'type': 'numeric'}])
    .addDelimiter('*', 40)
    .continueCallback(function () {
        if (timeout) {
            clearTimeout(timeout);
            console.log("Timeout cleaned");
            timeout = undefined;
        }
    })
    // .customHeader(function() {
    //     process.stdout.write("Hello\n");
    // })
    // .disableDefaultHeader()
    // .customPrompt(function() {
    //     process.stdout.write("\nEnter your selection:\n");
    // })
    // .disableDefaultPrompt()
    .start();

Output of this example:

    _   __            __       __  ___
   / | / /____   ____/ /___   /  |/  /___   ____   __  __
  /  |/ // __ \ / __  // _ \ / /|_/ // _ \ / __ \ / / / /
 / /|  // /_/ // /_/ //  __// /  / //  __// / / // /_/ /
/_/ |_/ \____/ \__,_/ \___//_/  /_/ \___//_/ /_/ \__,_/  v.1.0.0

---------------Main Menu---------------
1. No parameters
2. Print Field A
3. Print Field B concatenated with arg1: "arg1"
4. Sum: "op1" "op2"
5. String and Bool parameters: "str" "bool"
***************************************
6. Quit

Please provide input at prompt as: >> ItemNumber arg1 arg2 ... (i.e. >> 2 "string with spaces" 2 4 noSpacesString true)

>>

To invoke item without arguments just type number and Enter. To invoke item with arguments, type number then arguments delimited with space. If string argument has spaces it must be double quoted.

Additional links

Similar library for other languages:

Dependents (6)

Package Sidebar

Install

npm i node-menu

Weekly Downloads

7

Version

1.3.2

License

MIT

Unpacked Size

23 kB

Total Files

9

Last publish

Collaborators

  • nbu