How can we read random number of lines data from nodejs stdin? - javascript

I am trying to run a simple program in Visual studio code terminal using node.js which requires reading inputs from the user and the operating on those inputs and printing the results.
I have tried many approaches but have not got success yet. I am using the following code:
process.stdin.resume();
process.stdin.setEncoding("ascii");
var input = "";
process.stdin.on("data", (c) => (input += c));
process.stdin.on("end", () => {
console.log(input);
});
process.stdin.on("SIGINT", () => {
console.log(input);
const { EOL } = require("os");
const lines = input.split(EOL); /*your input text, split by lines*/
console.log(lines);
});
I run the above code in VSCode using the in-built terminal with the command node filename.js. The program runs and keeps taking inputs but it never ends and never triggers "end" block or "SIGINT" block. Finally to stop the program I have to use ctrl+C.
Can someone please help me how to accomplish this as I want to practise solving www.codeforces.com problems on my local machine using VSCode+terminal?

This way it won't quit until you explicitly end the program. You can use process.exit() function to achieve that.
So your program should look like this :
process.stdin.resume();
process.stdin.setEncoding("ascii");
var input = "";
const inputLength = (Math.random()*10 +1) | 0;
console.log(inputLength);
let current=0;
process.stdin.on("data", (c) => {
input += c;
current++;
if(current>= inputLength){
console.clear();
console.log("reached max number of inputs");
console.log(input);
process.exit(0);
}
});
Here is the glitch link. You can test it opening console below and type node server.js

TBH, I don't understand what do you want to achieve, but you can write something like:
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
})
rl.on('line', (input) => {
console.log(`Received: ${input}`);
});
when you hit enter, you will see you line. CTRL+C also works
readline is a part of the standard library

Related

JavaScript version of while True loop

I wrote this bit of code
while True:
text = input('type here > ')
print text
I've tried the below but it doesnt seem to be working
I'm having bit of a struggle trying to create a JavaScript version of it, since it seems js while loops constantly rerun but python seems to wait for input before rerunning. I am using the readline module to receive input from the console.
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
while (true) {
rl.question('type here > ', text => {
console.log(text)
})
}
Is there something im getting wrong? Im fairly new to programming
Any solutions?
If what you are trying to do is get input from the console in JavaScript you can use NodeJS process.stdin the implementation below :
process.stdin.resume();
process.stdin.setEncoding('utf-8');
let input = '';
let currentLine = 0;
process.stdin.on('data', inputStdin => {
input += inputStdin;
});
process.stdin.on('end', _ => {
input = input.trim()
.split('\n')
.map(str => str.trim());
// calling the function that works on the input
doSomething();
});
function readInput() {
return input[currentLine++];
}
Sample function to do something with what you get from the console
function doSomething() {
console.log(readInput())
}

node.js: take input from file using readline and process I/O in windows command prompt

I want to run name.js file from the command prompt using node.js and pass the input file and redirect that output in output.txt,
I am writing a command for this is node name.js < input.txt | > output.txt but this is not working or I am wrong.
name.js look like this:
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
var _line = "";
rl.on('line', function(line){
_line += line;
});
rl.on('pause', function(_line){
console.log(_line);
});
I have also tried this in Powershell -Command "command"
EDIT:
for example input.txt contain
hello js
hello node
hello world!
now,if i run node name.js < input.txt > output.txt.
i just get return value of console.log()'s "undefined" in output.txt
passing _line to rl.on('pause',function(_line){} hide the global _line that's why it's giving undefined and cmd command is fine.
there is other way to do this by using process I/O of node.js
function YourData(input) {
}
process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
_input += input;
});
process.stdin.on("end", function () {
YourData(_input);
});
read more about readline and process I/O

How to use readlineSync on Nodejs

So I'm trying out readlineSync to get user input (saw it was the best option to get user input). But then it doesn't output to the console. And it breaks out of node as soon as its done running. Help me out.
var readlineSync = require("readline-sync");
var firstName = readlineSync.question("First Name:");
console.log("Hi" + firstName);
"Expected Output: Hi Ifeoluwa"
"Actual result: Undefined and node exits automatically"
Node Console
The library does not work inside the REPL.
There is a line within the source code that reads:
if (process.stdin.isTTY) {
process.stdin.pause();
try {
fdR = fs.openSync('/dev/tty', 'r'); // device file, not process.stdin
ttyR = process.stdin._handle;
} catch (e) { /* ignore */ }
}
The process.stdin.pause will stop your current REPL session. However, when run from a file, the library works well.
i've read the documantion and it's working as expected, try to put the code to this sandbox
const express = require("express");
const app = express();
const readlineSync = require('readline-sync');
// Wait for user's response.
var userName = readlineSync.question('May I have your name? ');
console.log('Hi ' + userName + '!');
server.listen(3000, () => {
console.log(`Server is running`);
});

Using node.js's prompt for user input? [duplicate]

This question already has answers here:
Get user input through Node.js console
(6 answers)
Closed last month.
I'm working on a JS project running with node.js and I can't figure out how to get the prompt to work correctly for user input. I installed it from npm, followed the steps and I can get the program to prompt for user input but I can't store the result in a variable.
What I want is to prompt the user for his next move (up,down,left or right) every turn and use the result. I tried making a global move variable and affecting it in the prompt section, but it doesn't seem to work.
var Moving = function(maxMoves){
for(var n=maxMoves; n>0;){
var move;
var l;
//This would prompt for a direction (up,down,left,right)
move = prompt();
//And this would prompt for the number of tiles to advance;
l = prompt();
Direction(move,l);
n-=l;
}
};
Using require('prompt') or require('readline') are both good, easy ways to do this, but I wanted to find THE EASIEST WAY, and I didn't care about whether it is synchronous or asynchronous. Finding it took me longer than it should have, so maybe I can save people some time if they read this before digging any further.
Here you go:
# npm install readline-sync
var readline = require('readline-sync');
var name = readline.question("What is your name?");
console.log("Hi " + name + ", nice to meet you.");
Disclaimer: I am just spreading the word, here are my sources:
https://teamtreehouse.com/community/how-to-get-input-in-the-console-in-nodejs
How to take in text input from a keyboard and store it into a variable?
When you say "installed it from npm" I'm assuming you're referring to the prompt module from flatiron.
From their docs, as with most Node things, it looks like it exposes an asynchronous function, so you'll handle input inside the prompt callback:
var prompt = require('prompt');
//
// Start the prompt
//
prompt.start();
//
// Get two properties from the user: username and email
//
prompt.get(['username', 'email'], function (err, result) {
//
// Log the results.
//
console.log('Command-line input received:');
console.log(' username: ' + result.username);
console.log(' email: ' + result.email);
});
Storing it in a variable would be no different than accessing it from the result object above, but realize that since it's async it'll only be reliable available inside that callback.
you can use two packages readline and prompt
Here's a simple example with readline package https://nodejs.org/en/knowledge/command-line/how-to-prompt-for-command-line-input/
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question('What is your name ? ', function (name) {
rl.question('Where do you live ? ', function (country) {
console.log(`${name}, is a citizen of ${country}`);
rl.close();
});
});
rl.on('close', function () {
console.log('\nBYE BYE !!!');
process.exit(0);
});
Here is an another example with prompt package. https://www.npmjs.com/package/prompt
const prompt = require('prompt');
prompt.start();
prompt.get(['username','email'], function (err, result) {
console.log('Command-line input received:');
console.log(' username: ' + result.username);
console.log(' email: ' + result.email);
});
inquirer module has a prompt function.
Use
const { prompt } = require('inquirer');
prompt([array] | object | string ')

How do I implement tab completion in node.js shell?

I was looking for this feature in node.js and I haven't found it.
Can I implement it myself? As far as I know, node.js doesn't load any file at it's startup (like Bash does with .bashrc) and I haven't noticed any way to somehow override shell prompt.
Is there a way to implement it without writing custom shell?
You could monkey-patch the REPL. Note that you must use the callback version of the completer, otherwise it won't work correctly:
var repl = require('repl').start()
var _completer = repl.completer.bind(repl)
repl.completer = function(line, cb) {
// ...
_completer(line, cb)
}
Just as a reference.
readline module has readline.createInterface(options) method that accepts an optional completer function that makes a tab completion.
function completer(line) {
var completions = '.help .error .exit .quit .q'.split(' ')
var hits = completions.filter(function(c) { return c.indexOf(line) == 0 })
// show all completions if none found
return [hits.length ? hits : completions, line]
}
and
function completer(linePartial, callback) {
callback(null, [['123'], linePartial]);
}
link to the api docs: http://nodejs.org/api/readline.html#readline_readline_createinterface_options
You can implement tab functionality using completer function like below.
const readline = require('readline');
/*
* This function returns an array of matched strings that starts with given
* line, if there is not matched string then it return all the options
*/
var autoComplete = function completer(line) {
const completions = 'var const readline console globalObject'.split(' ');
const hits = completions.filter((c) => c.startsWith(line));
// show all completions if none found
return [hits.length ? hits : completions, line];
}
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
completer: autoComplete
});
rl.setPrompt("Type some character and press Tab key for auto completion....\n");
rl.prompt();
rl.on('line', (data) => {
console.log(`Received: ${data}`);
});
Reference :
https://self-learning-java-tutorial.blogspot.com/2018/10/nodejs-readlinecreateinterfaceoptions_2.html

Categories

Resources