Is there a way to have a command prompt (just a question
prompt, or something similar) fixed to the bottom of the Terminal, and to log output above it, using Node.js.
A really bad example:
Your favourite food is sushi.
Your favourite food is chicken.
Your favourite food is okra.
--> What is your favourite food?
So essentially, I'm looking to have the user always able to type, and have input echoed above the prompt.
interface.question("What is your favourite food?", function(answer) {
// output the answer above where this prompt was
// and somehow keep this same prompt where is is
});
The particular application I'm hoping to use this in is a simple IRC client, where I have a spot for the user to type, and have all the output (what the user has typed, and what others have also typed) outputted above where the user is typing. The lines in the diagram below are imaginary.
----------------------------------------------------------------------
| |
| |
| |
| |
| |
| |
| |
| Stuff |
| Stuff |
| Stuff |
----------------------------------------------------------------------
| --> The user can type here |
----------------------------------------------------------------------
See https://asciinema.org/a/10583, is console record.
This script has display problems when prompt has several lines (more 4), or if prompt is middle screen and have several lines.
process.stdout.write("\x1Bc");
var readline = require('readline'),
util = require('util');
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
completer: completer
});
rl.setPrompt("> ", 2);
rl.on("line", function(line) {
if (line == "help") { console.log("no command.") }
rl.prompt();
});
rl.on('close', function() {
return process.exit(1);
});
rl.on("SIGINT", function() {
rl.clearLine();
rl.question("Confirm exit : ", function(answer) {
return (answer.match(/^o(ui)?$/i) || answer.match(/^y(es)?$/i)) ? process.exit(1) : rl.output.write("> ");
});
});
rl.prompt();
var fu = function(type, args) {
var t = Math.ceil((rl.line.length + 3) / process.stdout.columns);
var text = util.format.apply(console, args);
rl.output.write("\n\x1B[" + t + "A\x1B[0J");
rl.output.write(text + "\n");
rl.output.write(Array(t).join("\n\x1B[E"));
rl._refreshLine();
};
console.log = function() {
fu("log", arguments);
};
console.warn = function() {
fu("warn", arguments);
};
console.info = function() {
fu("info", arguments);
};
console.error = function() {
fu("error", arguments);
};
console.log(">> Readline : Ok.");
function completer(line) {
var completions = ["help", "command1", "command2", "login", "check", "ping"];
var hits = completions.filter(function(c) {
return c.indexOf(line) == 0;
});
if (hits.length == 1) {
return [hits, line];
} else {
console.log("Suggest :");
var list = "",
l = 0,
c = "",
t = hits.length ? hits : completions;
for (var i = 0; i < t.length; i++) {
c = t[i].replace(/(\s*)$/g, "")
if (list != "") {
list += ", ";
}
if (((list + c).length + 4 - l) > process.stdout.columns) {
list += "\n";
l = list.length;
}
list += c;
}
console.log(list + "\n");
return [hits, line];
}
}
//-----------------------------
function main() {
var ___i=0;
setInterval(function () {
var num = function () { return Math.floor(Math.random() * 255) + 1; };
console.log(num()+"."+num()+"."+num()+" user connected.");
}, 1000);
//your code.
}
setTimeout(function () {
main();
}, 10);
just use the readline core module:
var readline = require('readline');
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question("What do you think of node.js? ", function(answer) {
console.log("Thank you for your valuable feedback:", answer);
rl.close();
});
This will solve your problem:
var readline = require('readline');
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
var fav_foods = [];
var ask_question = function() {
rl.question("What is your favourite food? ", function(answer) {
fav_foods.push(answer)
fav_foods.forEach(function (element) {
console.log("Your favourite food is " + element)
})
ask_question()
});
}
ask_question()