GithubHelp home page GithubHelp logo

node-menu's Introduction

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.

node-menu's People

Contributors

nbu avatar yacovmalen avatar stilesdev avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.