最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Multiple menus with Node.js - Stack Overflow

programmeradmin5浏览0评论

How can I create multiple menus with Node.js. I already found documentation at .html#process_process_stdin and used most of it in my example.

The problem is that in the next menu showSub the main menu stayed as listener. When I try something like process.stdin.pause or process.stdin.end it will exit my code.

Side note: I don't want to use any dependencies. Thanks for helping!

Here an example:

// Main
function showMain() {
    console.log(
        '1 = Show sub' + '\n' +
        '2 = Show other sub blabla...'  + '\n' +
        '3 = Exit'  + '\n\n' +
        'Choose number, then press ENTER:'
        );
    process.stdin.setEncoding('utf8');
    process.stdin.on('readable', checkMenu);

    function checkMenu() {
        var input = process.stdin.read();
        if(input !== null) {
            switch(input.trim()) {
                case '1': showSub(); break;
                case '2': showOtherSubBlaBla; break;
                case '3': process.exit(); break;
                default: showMain();
            }
        }
    }
}

// Sub
function showSub() {
    // process.stdin.pause(); Do I need this somewhere? It should not exit my code
    console.log(
        '1 = Do something bla bla' + '\n' +
        '2 = Go back to main'  + '\n\n' +
        'Choose number, then press ENTER:'
        );
    process.stdin.setEncoding('utf8');
    process.stdin.on('readable', checkMenu);

    function checkMenu() {
        var input = process.stdin.read();
        if(input !== null) {
            switch(input.trim()) {
                case '1': doSomethingBlaBla(); break;
                case '2': showMain; break;
                default: showSub();
            }
        }
    }
}

showMain();

How can I create multiple menus with Node.js. I already found documentation at https://nodejs/api/process.html#process_process_stdin and used most of it in my example.

The problem is that in the next menu showSub the main menu stayed as listener. When I try something like process.stdin.pause or process.stdin.end it will exit my code.

Side note: I don't want to use any dependencies. Thanks for helping!

Here an example:

// Main
function showMain() {
    console.log(
        '1 = Show sub' + '\n' +
        '2 = Show other sub blabla...'  + '\n' +
        '3 = Exit'  + '\n\n' +
        'Choose number, then press ENTER:'
        );
    process.stdin.setEncoding('utf8');
    process.stdin.on('readable', checkMenu);

    function checkMenu() {
        var input = process.stdin.read();
        if(input !== null) {
            switch(input.trim()) {
                case '1': showSub(); break;
                case '2': showOtherSubBlaBla; break;
                case '3': process.exit(); break;
                default: showMain();
            }
        }
    }
}

// Sub
function showSub() {
    // process.stdin.pause(); Do I need this somewhere? It should not exit my code
    console.log(
        '1 = Do something bla bla' + '\n' +
        '2 = Go back to main'  + '\n\n' +
        'Choose number, then press ENTER:'
        );
    process.stdin.setEncoding('utf8');
    process.stdin.on('readable', checkMenu);

    function checkMenu() {
        var input = process.stdin.read();
        if(input !== null) {
            switch(input.trim()) {
                case '1': doSomethingBlaBla(); break;
                case '2': showMain; break;
                default: showSub();
            }
        }
    }
}

showMain();
Share Improve this question edited Sep 12, 2015 at 0:15 Ben Besuijen asked Sep 8, 2015 at 12:54 Ben BesuijenBen Besuijen 6341 gold badge9 silver badges24 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

Maybe this solution could still be made a bit more elegant, but it should work. Create a global handler for your stdin stream and point it to a different implementeation according to your needs, like this:

var menuHandler;

// Initialize
function initialize() {
    showMain();
    process.stdin.setEncoding('utf8');
    process.stdin.on('readable', checkMenu);

    function checkMenu() {
        var input = process.stdin.read();
        if(input !== null) {
            menuHandler(input.trim());
        }
    }
}

// Main
function showMain() {
    console.log(
        '1 = Show sub' + '\n' +
        '2 = Show other sub blabla...'  + '\n' +
        '3 = Exit'  + '\n\n' +
        'Choose number, then press ENTER:'
        );

    menuHandler = function(input){
        switch(input) {
            case '1': showSub(); break;
            case '2': showOtherSubBlaBla; break;
            case '3': process.exit(); break;
            default: showMain();
        }
    };
}

// Sub
function showSub() {
    console.log(
        '1 = Do something bla bla' + '\n' +
        '2 = Go back to main'  + '\n\n' +
        'Choose number, then press ENTER:'
        );

    menuHandler = function(input){
        switch(input) {
            case '1': doSomethingBlaBla(); break;
            case '2': showMain(); break;
            default: showSub();
        }
    };
}

initialize();

I found a very nice solution. Node.js launched v4.0.0 recently. Now you can easily create menus with the module readline

Read more here: https://nodejs/api/readline.html

I also made a working sample:

// Requires readline and create global variable menu
var readline = require('readline'),
    menu;

// Main
function showMain() {
    // Clear screen
    process.stdout.write('\033c');

    // Log the menu
    console.log(
        'Main menu\n\n' +
        '1 = Go to sub\n' +
        '2 = Can be another sub... For now same as option 1\n' +
        '3 = Exit'
        );

    // Check if there is already a menu active. If true, close it.
    if(menu) menu.close();

    //Creates a readline Interface instance
    menu = readline.createInterface({
        input: process.stdin,
        output: process.stdout
    });

    // Ask question
    menu.question('Where to go? ', function(input) {
        switch(input) {
            case '1': showSub(); break;
            case '2': showSub(); break;
            case '3': process.exit(); break;
            default: showMain() /* show menu again if input does not match */;
        }
    });
}

// Sub
function showSub() {
    // Clear screen
    process.stdout.write('\033c');

    // Log the menu
    console.log(
        'Sub menu\n\n' +
        '1 = Another sub blabla...\n' +
        '2 = Go back to main'
        );

    // Check if there is already a menu active. If true, close it.
    if(menu) menu.close();

    // Creates a readline Interface instance
    menu = readline.createInterface({
        input: process.stdin,
        output: process.stdout
    });

    // Ask question
    menu.question('Where to go? ', function(input) {
        switch(input) {
            case '1': console.log('Another sub blabla...'); break;
            case '2': showMain(); break;
            default: showSub() /* show menu again if input does not match */;
        }
    });
}

showMain();
发布评论

评论列表(0)

  1. 暂无评论