How to use the cursorTo function from readline

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

readline.cursorTo is a function that moves the cursor in a readline interface to a specified column position, allowing you to control where the user's next input will be displayed.

240
241
242
243
244
245
246
247
248
249
function clearScreen(msg) {
  // source: https://github.com/vuejs/vue-cli/blob/dev/packages/%40vue/cli-shared-utils/lib/logger.js
  if (process.stdout.isTTY) {
    const blank = '\n'.repeat(process.stdout.rows);
    console.log(blank);
    readline.cursorTo(process.stdout, 0, 0);
    readline.clearScreenDown(process.stdout);
    if (msg) {
      console.log(msg);
    }
fork icon56
star icon71
watch icon16

231
232
233
234
235
236
237
238
239
240
ProgressBar.prototype.terminate = function () {
  if (this.clear) {
    if (this.stream.clearLine) {
      readline.clearLine(this.stream, 0)
      // this.stream.clearLine()
      readline.cursorTo(this.stream, 0)
      // this.stream.cursorTo(0)
    }
  } else {
    readline.cursorTo(this.stream, 0)
fork icon6
star icon20
watch icon0

+ 7 other calls in file

How does readline.cursorTo work?

readline.cursorTo is a function provided by the Node.js readline module that allows you to move the cursor position in a readline interface.

When readline.cursorTo is called with a readline.Interface object and a column position as its arguments, it moves the cursor position to the specified column, allowing you to control where the user's next input will be displayed.

If the column position is greater than the width of the terminal window, readline.cursorTo will move the cursor to the last column of the last row.

readline.cursorTo is often used in conjunction with other readline methods, such as readline.clearLine, to create interactive command-line interfaces that display information and prompts to the user in a specific format.

232
233
234
235
236
237
238
239
240
241
{
  onUploadProgress: (evt) => {
    if ((process.env.NODE_ENV || "").trim() !== "production") {
      const progress = (evt.bytesRead / fileSize) * 100;
      readline.clearLine(process.stdout, 0);
      readline.cursorTo(process.stdout, 0, null);
      process.stdout.write(`UPLOAD PROGRESS: ${Math.round(progress)}%`);
    }
  },
}
fork icon0
star icon1
watch icon0

50
51
52
53
54
55
56
57
58
59
  return;
}

console.clear();
readline.clearLine(process.stdout, 0);
readline.cursorTo(process.stdout, 0);

resultEndTime = result.resultEndTime.seconds * 1000 + Math.round(result.resultEndTime.nanos / 1000000);
lastTranscriptWasFinal = result.isFinal;
const stdoutText = result.alternatives[0] ? result.alternatives[0].transcript : "";
fork icon0
star icon0
watch icon2

+ 4 other calls in file

Ai Example

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

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

rl.question("What is your name? ", (name) => {
  readline.cursorTo(process.stdout, 0);
  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. We're then using readline.cursorTo to move the cursor position to the beginning of the current line, so that the greeting message will be displayed on the same line as the user's input. 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 and enter "John" as our name, we get the following output: csharp Copy code

126
127
128
129
130
131
132
133
134
135
136
assert.strictEqual(readline.cursorTo(writable, 'a', 'b'), true);
assert.strictEqual(writable.data, '');


writable.data = '';
assert.throws(
  () => readline.cursorTo(writable, 'a', 1),
  {
    name: 'TypeError',
    code: 'ERR_INVALID_CURSOR_POS',
    message: 'Cannot set cursor row without setting its column'
fork icon0
star icon0
watch icon0

+ 11 other calls in file

108
109
110
111
112
113
114
115
116
117
  updateProgress() {
    if (!process.stderr.isTTY || process.stdout.isTTY) {
      return;
    }
    readline.clearLine(process.stderr);
    readline.cursorTo(process.stderr, 0);
    process.stderr.write(this.getProgress());
  }
}

fork icon0
star icon0
watch icon0

428
429
430
431
432
433
434
435
436
  // Otherwise, do nothing.
  if (this._stdout.isTTY && process.env.TERM !== 'dumb') {
    // The require is here intentionally to avoid readline being
    // required too early when console is first loaded.
    const { cursorTo, clearScreenDown } = require('readline');
    cursorTo(this._stdout, 0, 0);
    clearScreenDown(this._stdout);
  }
},
fork icon0
star icon0
watch icon0

76
77
78
79
80
81
82
83
84
clearConsole () {
  if (!this.capturing && this.enabled && process.stdout.isTTY) {
    // Fill screen with blank lines. Then move to 0 (beginning of visible part) and clear it
    const blank = '\n'.repeat(process.stdout.rows)
    console.log(blank)
    readline.cursorTo(process.stdout, 0, 0)
    readline.clearScreenDown(process.stdout)
  }
}
fork icon0
star icon0
watch icon0