Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NodeJS simple horizontal line on console.log

I'm making a simple Node JS app. It logs a lots of informations on console. I would like to know if it's possible to add a horizontal lines in Node JS command line without using any extra packages or dependencies. If command prompt supports HTML elements, then I could use something like console.log("<hr>"); for adding a horizontal line but it does not support HTML.

Is there any way ?

like image 597
user9850863 Avatar asked Oct 17 '25 17:10

user9850863


2 Answers

To create the string for the horizontal line:

const line = '-'.repeat(process.stdout.columns)

.repeat() method repeats the string.

process.stdout.columns returns the number of columns.

To use it:

console.log(line)
like image 157
Sergey Volkov Avatar answered Oct 20 '25 06:10

Sergey Volkov


The console does not support rendering HTML elements.

That does not prevent you from making a custom line however!

const lineBreak = '----------------------'
console.log(lineBreak)

Of course, customize the linebreak however you'd like:

______ //Underscores!
-----  //Hyphens!
====== //Equals!

For grouping related data, refer to the docs here: console reference

Example:

function name(obj) {
  console.group('name');
  console.log('first: ', obj.first);
  console.log('middle: ', obj.middle);
  console.log('last: ', obj.last);
  console.groupEnd();
}

name({"first":"Wile","middle":"E","last":"Coyote"});

Will output grouped data to the console, visually giving it a line break & arrow to collapse the group. I think this would work well for your use case.

like image 30
nxSolari Avatar answered Oct 20 '25 08:10

nxSolari



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!