Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

305
Visualizações
ANSI escape codes not moving cursor horizontally

I am trying to make a text editor in the terminal with node and typescript but the ansi escape codes aren't moving the cursor horizontally. THe cursor moves vertically correctly but it just wont move horizontally.

Code: (index.ts)

var width = process.stdout.columns;
var height = process.stdout.rows;

function dupe(input: string, length: number) {
    var newI = ''

    for (var i = 0; i < length; i++) {
        newI += input
    }

    return newI
}

function pad(pad: string, input: string, width: number): string {
    var space = ''

    for (var i = 0; i < width - input.length; i++) {
        space += pad
    }

    return input + space;
}

var stdin = process.stdin;

// without this, we would only get streams once enter is pressed
stdin.setRawMode( true );

// resume stdin in the parent process (node app won't quit all by itself
// unless an error or process.exit() happens)
stdin.resume();

// i don't want binary, do you?
stdin.setEncoding( 'utf8' );

// on any data into stdin
stdin.on( 'data', function( key ){
  // ctrl-c ( end of text )
  if ( key.toString() === '\u0003' ) {
    process.exit();
  }
  // write the key to stdout all normal like
  update(key.toString())
});

function setpos(x: number, y: number) {
    return `\x1b[H\x1b[${y}B\x1b[${x}C`
}


var mousex = 0;
var mousey = 0;

async function update(key: string) {
    key = key.toLowerCase();

    width = process.stdout.columns;
    height = process.stdout.rows;

    console.clear()

    console.log(dupe('-', width))
    
    for ( var i = 1; i <= height-2; i++ ) {
        process.stdout.write(`${pad(' ', i.toString(), 3)}|This is a test string\n`)
    }
    if (key === 'w') { mousey --;}
    if (key === 's') { mousey ++;}
    if (key === 'a') { mousex --;}
    if (key === 'd') { mousex ++;}
    
    if (mousey < 0) { mousey = 0}
    if (mousey > height) { mousey = height}
    if (mousex < 0) { mousex = 0 }
    if (mousex > width) { mousex = width}
    console.log(setpos(mousex, mousey))
    console.log(`\x1b[1A${mousex} ${mousey} ${width} ${height}`)
}

update('')

stack overflow why do i need to write more i literally have nothing else to write.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Nevermind I was just dumb and didn't just try a different package. Installing terminal-kit fixed my problem.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda