Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

▶ Improves log readability using ASCII box-drawing characters.

License

NotificationsYou must be signed in to change notification settings

RienNeVaPlus/console2

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

87 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

console2

Massively extends theconsole features to produce human readable output. Provides content boxes using ASCII box-drawing characters, improvements for the output of object inspections (using tables), stack traces and more. It can even beep!

Features

  • Structured output usingASCII box-draing characters
  • Fully compatible to the systemconsole
  • Improved object inspection (pretty nice tables)
  • Improved stack traces
  • Improved timer
  • Ability to nest boxes
  • Additional console features
  • Various formatting shortcuts
  • Clean and focused
  • Intelligent use of colors to make contents even more distinguishable

Install

$ npm install --save console2

Quick Start

Have ascreenshot of the output.

constconsole2=require('console2')()console2.help()

Usage

Console2 integrates seamlessly into the nodeconsole. However, you should make yourself familiar with the additional features, especiallybox,line,over &out.

constconsole2=require('console2')()// log a stringconsole2.log("They're minerals! Jesus Christ, Marie.")// as you know and love it, native methods are fully supported// logs a string "the new way"console2.line("You shall not pass (immediately)")// this queues your string until you call "console.out"// read more on this below// insert empty line & start a timerconsole2.spacer().time('Timer1')// build a box - returns a new console instanceconstbox=console2.box('I am a child.')// add a line to our new boxbox.line('I am the 2nd line of the sub box!')// indicate that the box can be printed and there will be no more lines/boxes appended to itbox.over()// insert empty line & print timerconsole2.spacer().time('Timer1')// make noiseconsole2.beep()// print everything, this exists because most actions are asyncconsole2.out()

Result

Managing boxes.Over andOut.

The main feature is the generation ofeasy-to-read ASCII box-drawing character sections - short:boxes.

// returns a new console instance which acts as a child boxvar box = console.box()

These boxes are 2 dimensional (meaning they depend on the previous / following lines), you can no longerjust stdout a line when building a box. Doing so would result in a big mess of lines without any context to each other, because of thenature of time. Imagine you want to log the process of updating a database inside a single box, whilst complimenting yourself:

box.log('Trying database update')setTimeout(functionmimicDatabaseUpdate(){box.log('Database updated')},1)console.log('You look gorgeous!')// expected output:// ├ You look gorgeous// ├─ Starting database update// └─ Database updated// actual output:// ├─ Starting database update// ├ You look gorgeous// └─ Database updated

"Houston we have a problem."

As you see, you need to wait until you are done adding new lines before you can print a box. The solution is simple:We queue stuff.Instead ofconsole.log, useconsole.line. It does the same thing, except for callingstdout (it's not printing the line).

box.line('Trying database update')setTimeout(functionmimicDatabaseUpdate(){box.line('Database updated')},1)console.log('You look gorgeous!')

But now there's only the part where I compliment myself? Right, here's what happened: You've added a line, then printed everythingthat's ready (console.log did that) and finally added another line to your box. But you didn't mark the box asover /ready and therefore console2 thinks you might want to add more lines.

"No you don't. Over and out!"

// ...box.line('Database updated').out()// ..

By callingconsole.out() (or in this casebox.out())you tell the parent of all boxesto print every child boxthat's ready.

Pro-Tip:out can take the same arguments asline does. So you could simplify the above to:box.out('Database updated').

"And what about over? Over."

You might run into situations where you want to mark a box as printable but don't want to print everything. For example when you're working on multiple child-boxes at once: you have to wait until every child box is done, before you can output the whole thing. That's whatbox.over is for:

constbox=console2.box('I am a box with children')constchild1=box.box('I am child #1')constchild2=box.box('I am child #2')async.each(arr,(data,callback)=>{child1.line('Processing item #1:',data)},()=>child1.over())// you don't know if i'm faster or slower than the onEnd above!setTimeout(()=>child2.line('Hello friend').over(),123)// out .out() to print queued linesfunctionprint(){console2.out('Additional output')}

Pro-Tip:over can take the same arguments asline does. So you could simplify the above to:box.over('Hello friend').

Reference

Console2 not only improves the native console functions (log,info,warn,error,dir,time,timeEnd,trace) but also provides additional functions.


console2.help()

Displays ashort tutorial with examples.


console2.box(content, option)

Create a sub box.


console2.line({...*}[, option])

Add a line.


console2.over({...*}[, option])

Adds a line and sets the option{over:true}


console2.out({...*}[, option])

Flush current buffer (use this to actuallysee something).


console2.spacer()

Flush current buffer + adds an empty line.


console2.log({...*}[, option])

Same asconsole.line but with an additional call toconsole.out to remain compatible to the nativeconsole.


console2.title({String} line)

Creates a title by adding two lines (above & below) the text.


console2.beep({String} [label])

Makes your terminal beep, outputsbeep: label.


console2.time({String} [label], {Boolean} [reset])

Useful stopwatch that shows the elapsed time in a readable format (ms + years, months, days...).When called twice, the time in between the two calls is also measured & displayed!

// Prints time since box was initializedconsole2.time()// (1st call) starts a new timer, outputs 'Timer1: start'console2.time('Timer1')// (1st call) same as above, no outputconsole2.time('Timer1',true)// (2nd call) outputs 'Timer1: Xms'console2.time('Timer1')// (2nd call) outputs 'Timer1: Xms - reset', resets the timerconsole2.time('Timer1',true)

console2.trace({String} [label])

Beautifiedconsole.trace.


console2.build(stripLevels=0, useParent=false)

Returns a promise with the output ofconsole.out() as a string.


console2.options({Object|String|Number} data)

OptionTypeDefaultHelp
colorStringgreyPrimary color
colorTextStringgreyText color
borderNumber1Vertical border-width:1 () or2 ()
consoleObjectconsoleObject to receive the output of console2.out.
Needs to have the same properties as theconsole.
mapArray[['...','…']]Simple replace for input strings (e.g.... to a single char)
isWorkerBooleanfalseAct as aworker
overBooleanfalseAllow output of box when a parent usesout()
disableAutoOutBooleanfalseConsole2 tries to detect whether to automatically call
console.out after new lines have been added.
overrideBooleanfalseWhether to override the nativeconsole.
Can only be set when first calling the main function.

Shortcuts

  • 1,2 ⇔ sets{border:Number}
  • chalkcolor orcommand (see console.col) ⇔ sets{color:String,colorText::String}

console2.col({String} input, {...String} color)

Colorizes theinput, can take multiple colors / commands (see modulechalk).

  • Colors:cyan,green,yellow,red,magenta,blue,white,grey,black
  • Backgrounds:bgCyan,bgGreen,bgYellow,bgRed,bgMagenta,bgBlue,bgWhite,bgGrey,bgBlack
  • Commands:bold (bright color),dim (dark color),italic (bad support),underline (bad support),inverse,hidden,strikethrough (bad support)
  • Specials:rainbow,zebra,code

Use to colorize a string before adding it:

console2.log(console.col('I am a rainbow!','rainbow'))

console2.strip({String} input)

Removes any ansi codes from theinput string (see modulechalk).


console2.pad({String} padSymbol, {Number} length, {Number} [str], {Boolean} [useLeftSide])

Note:pad will be deprecated, useString.padStart orString.padEnd instead.

Utility to generate a pad string when working with aligned texts.

console.pad('-',5)// = '-----'console.pad('.',7,'Hello')// = 'Hello..'console.pad(' ',7,'Hello',true)// = '  Hello'

Aliases

Alias exist to cover features of the nativeconsole or to provide shortcuts for lazy people like me.

ShortcutAlias
console._console.line
console.infoconsole.log (green)
console.warnconsole.log (yellow)
console.errorconsole.log (red)
console.dirconsole.log
console.timeEndconsole.time
console.okconsole.time('OK').out()

Overriding the native console

You can enable overriding of the nativeconsole by passingtrue into the main function or by usingconsole2.options({override: true}).

var console2 = require('console2')(false) // "false" is a shortcut for the option {override:false}console2.title('Hello World')

Thanks to

About

▶ Improves log readability using ASCII box-drawing characters.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp