How to use the createInterface function from readline

Find comprehensive JavaScript readline.createInterface code examples handpicked from public code repositorys.

readline.createInterface is a function that creates a new interface for reading input from a readable stream, such as a user's command-line input.

324
325
326
327
328
329
330
331
332
333
},

_parseGeoNamesAlternateNamesCsv: function (pathToCsv, callback) {
  var that = this;
  that._alternateNames = {};
  var lineReader = readline.createInterface({
    input: fs.createReadStream(pathToCsv),
  });
  lineReader.on('line', function (line) {
    line = line.split('\t');
fork icon50
star icon156
watch icon10

+ 3 other calls in file

485
486
487
488
489
490
491
492
493
494
495
};


const ui = {
  async input(query) {
    return new Promise((resolve) => {
      const rl = readline.createInterface({
        input: process.stdin,
        output: process.stdout,
      });
      rl.question(query, (answer) => {
fork icon55
star icon154
watch icon14

+ 3 other calls in file

How does readline.createInterface work?

readline.createInterface is a function provided by the Node.js readline module that allows you to read input from a user through a command line interface.

When readline.createInterface is called, it takes in an object with properties specifying the input and output streams to use for the interface.

Once the interface is created, you can use its question method to prompt the user for input and receive the user's response.

The readline module is often used for creating command-line interfaces for Node.js applications, and can be useful for building interactive command-line tools and utilities.

196
197
198
199
200
201
202
203
204
205

if (revisionAge < 2) {
	logger.logWarning(`Revision Age is ${revisionAge}; currently processing jobs will be removed when revision age is set to 0.`);

	if (!force) {
		const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
		const response = await new Promise((resolve) => rl.question('Continue? (y/N): ', (ans) => {
			rl.close();
			resolve(ans);
		}));
fork icon37
star icon90
watch icon21

218
219
220
221
222
223
224
225
226
227
})
const rlout = new PassThrough()
rlout.pipe(output, {
  end: false
})
const rli = rl.createInterface(rlin, rlout)
try {
  while (true) {
    /**
     * @type {string}
fork icon1
star icon47
watch icon0

Ai Example

1
2
3
4
5
6
7
8
9
10
11
const readline = require("readline");

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

rl.question("What is your name? ", (name) => {
  console.log(`Hello, ${name}!`);
  rl.close();
});

In this example, we're using readline.createInterface to create a new interface for reading input from the process.stdin and process.stdout streams. We're then using the question method to prompt the user for input with the message "What is your name?" and receive the user's response as the name parameter in the callback function. Finally, we're using console.log to output a personalized greeting to the user, and the rl.close() method to close the interface and exit the program. When we run this code, we get the following output: csharp Copy code

171
172
173
174
175
176
177
178
179
180
this.exe.stdin.once("error", (err) => {
	this.log_and_alert("Got exe.stdin error:", err.toString());
	this.shutdown();
});

this.scanner = readline.createInterface({
	input: this.exe.stdout,
	output: undefined,
	terminal: false
});
fork icon6
star icon42
watch icon3

+ 7 other calls in file

130
131
132
133
134
135
136
137
138
139
140
const prompt = async (question, onlyMainnet = true) => {
  if (onlyMainnet && hre.network.name != 'mainnet') {
    return;
  }


  const r = rl.createInterface({
    input: process.stdin,
    output: process.stdout,
    terminal: false
  });
fork icon24
star icon41
watch icon5

1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
  changeValue() {
    this.value = this._value.value;
  }
};
function aD({ input: t = import_node_process.stdin, output: u2 = import_node_process.stdout, overwrite: F2 = true, hideCursor: e2 = true } = {}) {
  const s = f.createInterface({ input: t, output: u2, prompt: "", tabSize: 1 });
  f.emitKeypressEvents(t, s), t.isTTY && t.setRawMode(true);
  const C2 = (D2, { name: i }) => {
    if (String(D2) === "" && process.exit(0), !F2)
      return;
fork icon1
star icon6
watch icon4

+ 3 other calls in file

1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
}


const loadLog = async function(host, dir) {
  const stream = fs.createReadStream(dir + '/serverlog.txt');
  
  const readLine = readline.createInterface({
    input: stream,
    crlfDelay: Infinity
  });
  
fork icon1
star icon3
watch icon3

+ 2 other calls in file

6
7
8
9
10
11
12
13
14
15
16
17
18
 */


var readline = require("readline");


var lines = [];
var rl = readline.createInterface({
  input: process.stdin,
});


rl.on("line", function (line) {
fork icon0
star icon1
watch icon1

9
10
11
12
13
14
15
16
17
this.ticTacToeLayout = '';
this.currentPlayer = false; // player 1 = false (O), player 2 = true (X)
this.gameEnded = false;
this.moveRegister = [];

this.rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
})
fork icon0
star icon1
watch icon2

+ 3 other calls in file

192
193
194
195
196
197
198
199
200
201
    console.log(crash.stack);  //defined for Error object exceptions
    console.log("--------");
    console.log("It would be greatly appreciated if you would copy the above info and mail it to support@diggerhub.com");
    //the console window immediately disappears on windows.  Prompt to
    //hold it open so the user has a chance to read and email the err.
    const rl = readline.createInterface({input: process.stdin,
                                         output: process.stdout});
    rl.question("\n", function (ignore) {
        process.exit(1); });
}
fork icon0
star icon1
watch icon1

179
180
181
182
183
184
185
186
187
188
    console.error('[wpt] httpd exited with', code, signal);
  }
  this.wptHttpd = null;
});

const rl = readline.createInterface(this.wptHttpd.stderr);
rl.on('line', line => {
  if (line.startsWith('DEBUG')) {
    return;
  }
fork icon0
star icon12
watch icon3

115
116
117
118
119
120
121
122
123
124
125
/*
 * Parse `key=value` lines from stdin until EOF or the first blank line.
 */
function parse() {
  return new Promise((resolve, reject) => {
    const rl = readline.createInterface({input: process.stdin});


    let resolved = false;
    const query = {};

fork icon0
star icon0
watch icon1

12
13
14
15
16
17
18
19
20
21
22
class UI {
  constructor(opt) {
    // Instantiate the Readline interface
    // @Note: Don't reassign if already present (allow test to override the Stream)
    if (!this.rl) {
      this.rl = readline.createInterface(setupReadlineOptions(opt));
    }


    this.rl.resume();

fork icon0
star icon0
watch icon1

63
64
65
66
67
68
69
70
71
72
73
    console.log(commands[command].description);
    console.log(commands[command].usage ? `usage:\n${commands[command].usage}` : '');
  }
};


const HBNBCommand = readline.createInterface({
  input: process.stdin,
  output: process.stdout
});

fork icon0
star icon0
watch icon1

37
38
39
40
41
42
43
44
45
46
const path = '/opt/dcloud/demo-automation/.env'
const text = fs.readFileSync(path, 'utf-8')
const lines = text.split('\n')
// const fileStream = fs.createReadStream('/opt/dcloud/demo-automation/.env')

// const lines = readline.createInterface({
//   input: fileStream,
//   crlfDelay: Infinity
// })
// Note: we use the crlfDelay option to recognize all instances of CR LF
fork icon0
star icon0
watch icon1

80
81
82
83
84
85
86
87
88
89
    });
}
///------------------------------------
function convertOriginalCSV() {
    const stream = fs.createReadStream("../audibleBookData.csv");
    const rl = readline.createInterface({ input: stream });
    let data = [];
    rl.on("line", (row) => {
        data.push(row.split(","));
    });
fork icon0
star icon0
watch icon2

85
86
87
88
89
90
91
92
93
94
if ('srt' != fileInfo.extension.toLowerCase()) {
	throw new Error(`El fichero ${path} no es un subtítulo`);
}
const temporary = `${fileInfo.parent}/${fileInfo.name}.tmp`;

const lineReader = readline.createInterface({
	input: fs.createReadStream(path, {encoding: 'binary'})
});

const output = fs.createWriteStream(temporary, {encoding: 'binary'});
fork icon0
star icon0
watch icon1

+ 4 other calls in file

144
145
146
147
148
149
150
151
152
153
if (!(species in proteinsCovered)) {
  proteinsCovered[species] = new Set();
}
datasets[species].push(dataset);
const input = fs.createReadStream(`./data/abundances/${d}`);
const rl = readline.createInterface({ input })
rl.on('close', function () {

  //add ranks
  var abundancesSorted = []
fork icon0
star icon0
watch icon6

111
112
113
114
115
116
117
118
119
120
}

static async processLineByLine(filePath, onLine) {
    const instream = fs.createReadStream(filePath);
    const outstream = new stream;
    const rl = readline.createInterface(instream, outstream);
    const fname = path.parse(filePath).base;

    let line = 0
    for await (const s of rl) {
fork icon0
star icon0
watch icon1

+ 11 other calls in file