- Notifications
You must be signed in to change notification settings - Fork0
▶ Improves log readability using ASCII box-drawing characters.
License
RienNeVaPlus/console2
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
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!
- Structured output usingASCII box-draing characters
- Fully compatible to the system
console
- 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
$ npm install --save console2
Have ascreenshot of the output.
constconsole2=require('console2')()console2.help()
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()
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
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.
// ...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')
.
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')
.
Console2 not only improves the native console functions (log
,info
,warn
,error
,dir
,time
,timeEnd
,trace
) but also provides additional functions.
Displays ashort tutorial with examples.
Create a sub box.
Add a line.
Adds a line and sets the option{over:true}
Flush current buffer (use this to actuallysee something).
Flush current buffer + adds an empty line.
Same asconsole.line
but with an additional call toconsole.out
to remain compatible to the nativeconsole
.
Creates a title by adding two lines (above & below) the text.
Makes your terminal beep, outputsbeep: label
.
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)
Beautifiedconsole.trace
.
Returns a promise with the output ofconsole.out()
as a string.
Option | Type | Default | Help |
---|---|---|---|
color | String | grey | Primary color |
colorText | String | grey | Text color |
border | Number | 1 | Vertical border-width:1 (│ ) or2 (║ ) |
console | Object | console | Object to receive the output of console2.out. Needs to have the same properties as the console . |
map | Array | [['...','…']] | Simple replace for input strings (e.g.... to a single char… ) |
isWorker | Boolean | false | Act as aworker |
over | Boolean | false | Allow output of box when a parent usesout() |
disableAutoOut | Boolean | false | Console2 tries to detect whether to automatically callconsole.out after new lines have been added. |
override | Boolean | false | Whether to override the nativeconsole .Can only be set when first calling the main function. |
Shortcuts
1
,2
⇔ sets{border:Number}
- chalk
color
orcommand
(see console.col) ⇔ sets{color:String,colorText::String}
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'))
Removes any ansi codes from theinput
string (see modulechalk).
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'
Alias exist to cover features of the nativeconsole
or to provide shortcuts for lazy people like me.
Shortcut | ⇔ | Alias |
---|---|---|
console._ | ⇔ | console.line |
console.info | ⇔ | console.log (green) |
console.warn | ⇔ | console.log (yellow) |
console.error | ⇔ | console.log (red) |
console.dir | ⇔ | console.log |
console.timeEnd | ⇔ | console.time |
console.ok | ⇔ | console.time('OK').out() |
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')
About
▶ Improves log readability using ASCII box-drawing characters.
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Releases
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors2
Uh oh!
There was an error while loading.Please reload this page.