1

I'm working with JavaScript/Node and have a Websocket with a server that is sending a constantly updating string. My goal is to log the string dynamically in the console, so if the sequence of strings from the stream is [string1, string1, string2, string2, string2, string3], then rather than have the console show

string1
string1
string2
string2
string2
string3

I want the console to show

stringI

Where stringI is constantly updating and I is in {1,2,3}

Currently, I'm doing this with

process.stdout.clearLine(0)
process.stdout.cursorTo(0)
process.stdout.write(new_string)

But I run into issues when the string is greater than one line length in the console.

Does anyone know how I can dynamically overwrite a constantly updating string in the console? Thanks!

SleekEagle
  • 53
  • 3
  • just end the written string with a return `\r`, as for multiline updates you could slice the string to fit `new_string.slice(0, process.stdout.columns)` or see [How to update data on multiple lines of console](https://stackoverflow.com/questions/17424723/how-to-update-data-on-multiple-lines-of-console) – pilchard Jan 28 '22 at 17:06
  • You can start by reading here: https://dustinpfister.github.io/2019/09/19/nodejs-ansi-escape-codes/ – jfriend00 Jan 28 '22 at 17:13

1 Answers1

0

truncation could be an easy option:

const { exec } = require("child_process");
exec("truncate -s 0 console.log");
G-Force
  • 345
  • 3
  • 10