I have a "simple" problem that I cant seem to get around. Im just going to read input from standard input (terminal) and then save the input to a variable (here an array) and then print it all out to the terminal. When I try to paste in more than 55 lines of text, the program just print out the first 55 lines (of my input) and then exit. I cant understand why it stops and how to fix this.
My code is:
let readline = require('readline');
let rl = readline.createInterface({
input: process.stdin,
terminal: true
})
let inputLines = []
const pushLinesToArray = function (line) {
inputLines.push(line)
}
const readOneLine = function () {
return new Promise((resolve, reject) => {
rl.on('line', resolve)
}).then(rl.on('line', pushLinesToArray))
}
const start = async () => {
// Save all input to inputLines array
await readOneLine()
// Print inputLines array to terminal/console
inputLines.forEach((line) => console.log(line))
process.exit()
}
start()
How to get around this problem?
I should also mention that when I change the function (readOneLine) above to the code down below it works IF I close the program with ctrl+C (Mac OSX). This makes me think It doesn't have anything with maxLineLength or memory buffer to do. The only thing I changed down below is close
instead of line
the first rl.on
const readOneLine = function () {
return new Promise((resolve, reject) => {
rl.on('close', resolve)
}).then(rl.on('line', pushLinesToArray))
}
I should also mention that I'm going to process the input a bit before I actually print it out. In the code above I just took all that away because I realize there was a problem already with the reading of the input. i.e I want to read input of more than 55 lines of text. Then save the text to an array (line by line). Then do my other code (to calculate how much each person has in debt). And lastly print it out.
Example input will be like this:
When I copy --> paste this as input (and mark every line (1-13)) this work. If I, on the other hand, have 57 lines like this (where line nr 1-56 is with text and line nr 57 is empty) it doesn't work. Then my program exits after printing the first 55 lines with text (line nr 56 will never get printed)
Edit: I need this to work with Kattis (online site for programming problems ) and the only thing I know is they are running my code with the above mention flags: -c {files}. I need to modify my code so it can handles different inputs of everything between 3 lines and up to 100 000 lines. The input will always come in a chunk (a string with all the lines copy--> pasted into the terminal) and I need to attach listener or read this input and do something with it (calculate everything) before I print out the answer to the terminal. So I still looking for an answer to solve this input problem.