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

javascript - Fixed position command prompt in Node.js - Stack Overflow

programmeradmin1浏览0评论

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                                         |
----------------------------------------------------------------------

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                                         |
----------------------------------------------------------------------
Share Improve this question edited Oct 2, 2012 at 17:15 Whymarrh asked Oct 1, 2012 at 11:14 WhymarrhWhymarrh 13.6k15 gold badges60 silver badges110 bronze badges 3
  • Did you find a solution ? – user2226755 Commented Feb 10, 2018 at 11:03
  • Apparently not. None of the answers proposed addresses the question. – Thomas Dickey Commented Feb 10, 2018 at 15:21
  • @ThomasDickey Try or say in comment why the answer is not good. – user2226755 Commented Aug 20, 2019 at 7:28
Add a comment  | 

4 Answers 4

Reset to default 16

Serverline module

process.stdout.write("\x1Bc")
console.log(Array(process.stdout.rows + 1).join('\n'));

const myRL = require("serverline")

myRL.init()
myRL.getRL().question("What is your favourite food? ", function(answer) {
  console.log(`Your favourite food is ${answer}.`)
});

function main() {
  let i = 0
  setInterval(function() {
    const num = () => Math.floor(Math.random() * 255) + 1
    i++
    console.log(i + " " + num() + "." + num() + "." + num() + " user connected.")
  }, 700)
}
main()

Old version :

https://stackoverflow.com/posts/24519813/revisions

Here is a simple solution (tested with node v6.4.0 on macos):

const readline = require('readline');
const rl = readline.createInterface(process.stdin, process.stdout);

// Logs a message keeping prompt on last line
function log(message) {
  readline.cursorTo(process.stdout, 0);
  console.log(message);
  rl.prompt(true);
}

// Testing the solution

rl.question('Enter something...:', userInput => {
  log('You typed ' + userInput);
  rl.close();
});

log('this should appear above the prompt');
setTimeout(
  () => log('this should appear above the prompt'),
  1000
);

Well I'm using inquirer for solve that kind of issues.. It's has all those thigs resolve already.. It's easy to use and you have diferents kind of propmt types.

Here a litle example form the doc:

var inquirer = require('inquirer');
inquirer.prompt([/* Pass your questions in here */]).then(function (answers) {
    // Use user feedback for... whatever!! 
});

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()
发布评论

评论列表(0)

  1. 暂无评论