Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fixed position command prompt in Node.js

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                                         |
----------------------------------------------------------------------
like image 432
Whymarrh Avatar asked Oct 01 '12 11:10

Whymarrh


People also ask

What is the node JS command prompt?

It is a computer environment the same as command prompt and an easy way to test simple Node. js/JavaScript code and allows to execute multiple javascript codes. we can simply run REPL on the command prompt using node command on the command prompt.

How do I open command prompt in node?

Node. js files must be initiated in the "Command Line Interface" program of your computer. How to open the command line interface on your computer depends on the operating system. For Windows users, press the start button and look for "Command Prompt", or simply write "cmd" in the search field.

What is on () in node JS?

In Node. js, you usually bind functions (using 'on' or other functions) to listen to events.


1 Answers

Serverline module

enter image description here

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

like image 123
Sky Voyager Avatar answered Sep 27 '22 22:09

Sky Voyager