1

I'm using Readline for my interpreter, and I want to insert newline character on enter instead into going into next line, so if I use history I will have full command instead of one line of my multi line command separated.

My code look like this:

var prompt = 'lips> ';
var continuePrompt = '... ';
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout,
    prompt: prompt,
    terminal: !!process.stdin.isTTY && !(process.env.EMACS || process.env.INSIDE_EMACS)
});
if (process.stdin.isTTY) {
    rl.prompt();
}
boostrap(interp.env).then(function() {
    rl.on('line', function(line) {
        code += line + '\n';
        if (balanced_parenthesis(code)) {
            rl.pause();
            run(code, interp.env).then(function(result) {
                if (process.stdin.isTTY) {
                    print(result);
                    if (multiline) {
                        rl.setPrompt(prompt);
                    }
                    rl.prompt();
                }
                rl.resume();
            }).catch(function() {
                if (process.stdin.isTTY) {
                    if (multiline) {
                        rl.setPrompt(prompt);
                    }
                    rl.prompt();
                }
            });
            code = '';
        } else {
            multiline = true;
            var ind = indent(code, 2, prompt.length - continuePrompt.length);
            rl.setPrompt(continuePrompt);
            rl.prompt();
            rl.write(new Array(ind + 1).join(' '));
        }
    });
});

So basically what I need is some continue or preventDefault (like in browser). Is something like this possible?

One if the issue I have is that I have auto indent and when I copy paste the code I have double indent if code already have indentation, because each line from clipboard is single line in Readline. I want to insert single text. Or at least rewrite history to add multi line command all at once at the end, maybe disable history completely and add items to history in my own code.

Is something like this possible with Readline in Node.js or do I need recreate same readline API without readline. Do you know of any example that use this technique or maybe open source project that use this?

EDIT:

I've found this question and answer Is there a nice way of handling multi-line input with GNU readline?

rl_bind_key('\r', return_func) ===> int return_func(int cnt, int key) { ... }

but it seems that Node don't support this API. Any way to have same functionality in Node?

jcubic
  • 61,973
  • 54
  • 229
  • 402

0 Answers0