Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

274
Views
How do I get the current cursor position relative to the output with nodejs readline

I'm trying to get the current cursor position in a nodejs app.

I've tried getCursorPos (https://nodejs.org/api/readline.html#rlgetcursorpos) but that returns the position relative of the input prompt and the value of rows is 0

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I was able to create a promise to get this information using the answer below containing the escape sequence for requesting terminal cursor position:

How can I get position of cursor in terminal?

const getCursorPos = () => new Promise((resolve) => {
    const termcodes = { cursorGetPosition: '\u001b[6n' };

    process.stdin.setEncoding('utf8');
    process.stdin.setRawMode(true);

    const readfx = function () {
        const buf = process.stdin.read();
        const str = JSON.stringify(buf); // "\u001b[9;1R"
        const regex = /\[(.*)/g;
        const xy = regex.exec(str)[0].replace(/\[|R"/g, '').split(';');
        const pos = { rows: xy[0], cols: xy[1] };
        process.stdin.setRawMode(false);
        resolve(pos);
    }

    process.stdin.once('readable', readfx);
    process.stdout.write(termcodes.cursorGetPosition);
})

const AppMain = async function () {
    process.stdout.write('HELLO');
    const pos = await getCursorPos();
    process.stdout.write("\n");
    console.log({ pos });
}

AppMain();
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!