I am using the readline module to create a mand line interface (CLI) for an application in Node.js.
The problem is that I can not scroll up to view the past mands as I usually can in Terminal. My CLI is just a fixed window and if I print too much out to the screen, I lose information at the top and there is no way to scroll up to see it.
(I am running my program on Mac OSX Mavericks)
Thanks in advance.
Code Snippet:
var readline = require('readline');
var Cli = function () {
this.txtI = process.stdin;
this.txtO = process.stdout;
process.stdout.write('CLI initialized.');
this.rl = readline.createInterface({input: this.txtI, output: this.txtO });
this.rl.setPrompt('>>>');
this.rl.prompt();
this.rl.on('line', function(line) {
var input = line.toString().trim();
if (input) {
this.txtO.write('cmd: ' + input);
}
this.rl.prompt();
}.bind(this)).on('close', function() {
this.txtO.write('Have a great day!');
process.exit(0);
}.bind(this));
};
new Cli();
Save this file as snippet.js and run
node snippet.js
in terminal.
I am using the readline module to create a mand line interface (CLI) for an application in Node.js.
The problem is that I can not scroll up to view the past mands as I usually can in Terminal. My CLI is just a fixed window and if I print too much out to the screen, I lose information at the top and there is no way to scroll up to see it.
(I am running my program on Mac OSX Mavericks)
Thanks in advance.
Code Snippet:
var readline = require('readline');
var Cli = function () {
this.txtI = process.stdin;
this.txtO = process.stdout;
process.stdout.write('CLI initialized.');
this.rl = readline.createInterface({input: this.txtI, output: this.txtO });
this.rl.setPrompt('>>>');
this.rl.prompt();
this.rl.on('line', function(line) {
var input = line.toString().trim();
if (input) {
this.txtO.write('cmd: ' + input);
}
this.rl.prompt();
}.bind(this)).on('close', function() {
this.txtO.write('Have a great day!');
process.exit(0);
}.bind(this));
};
new Cli();
Save this file as snippet.js and run
node snippet.js
in terminal.
Share Improve this question edited Nov 21, 2013 at 20:53 rustybeanstalk asked Nov 20, 2013 at 3:01 rustybeanstalkrustybeanstalk 2,76210 gold badges39 silver badges57 bronze badges 5- You want to show past mands how? When you press the UP arrow key for example? If so, that's something you'd have to implement. – WiredPrairie Commented Nov 20, 2013 at 3:55
- Could you post how you use readline in your node script? – Jeff Sisson Commented Nov 20, 2013 at 4:14
- I've added a distilled version of my CLI. I seem to have fixed my scrolling problem with this code. I do have another problem. I can not seem to write to the process.stdout stream. Any help with that? – rustybeanstalk Commented Nov 20, 2013 at 23:30
-
Why are you cacheing the
process.stdout
with thethis.txtO
variable? Why not just use / referenceprocess.stdout
directly? – Jeff Sisson Commented Nov 21, 2013 at 4:16 - I would like to use any streams, not just process.stdin, and process.stdout. I could pipe any stream into txtI and pipe txtO to any stream. – rustybeanstalk Commented Nov 21, 2013 at 20:51
2 Answers
Reset to default 4It probably is working, just readline
is overwriting your line. Try outputting multiple lines:
process.stdout.write("1\n2\n3\n4\n5");
Readline is quite an awesome module. History is already there. As is the possibility to add pletion. Try the snippet below.
var readline = require('readline');
function createCLI(opt) {
var rl = readline.createInterface({
input : opt.input,
output : opt.output,
terminal : opt.terminal || true,
pleter : opt.pleter ||
function asyncCompleter(linePartial, callback){
var pletion = linePartial.split(/[ ]+/);
callback(null, [pletion, linePartial]);
}
});
rl.on('line', function(line) {
if( !line.trim() ){ this.prompt(); }
else { this.write(line); }
}).on('close', function() {
this.output.write('\n Have a great day!');
process.exit(0);
}).setPrompt(' > ');
rl.output.write(' CLI initialized\n');
return rl;
}
var cli = createCLI({
input : process.stdin,
output : process.stdout
});
cli.prompt();
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742343199a4426034.html
评论列表(0条)