Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

draftlog

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

draftlog

Update logged lines after logging them

  • 1.0.7
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
8.8K
decreased by-6.36%
Maintainers
1
Weekly downloads
 
Created
Source

DraftLog

Build Status Dependency Status

Because Logging can be pretty and fun

DraftLog GIF

Installation

$ npm install draftlog

What it does

It allows you to re-write a line of your log after being written. Just like post 'updating'.

It does that by keeping track of the current lines of code written thorugh the stream, and moving the cursor up to the line of the LogDraft you created previously, and updating it's content.

How the HECK is that even possible?

Usage

// Setup
const DraftLog = require('draftlog')
DraftLog(console)

// Or, in a single line:
require('draftlog').into(console)

To create a updatable log, use the draft method injected into the provided console:

// Create a Draft log
var update = console.draft('Hy, my name is')

// You can call logs after it
console.log('Something else')

// Use the received callback to update it as many times as you want
update('Hy, my name is Ivan!')

Here is some interesting exemples:

// Prints a clock incrementing one every second in the same line
var draft = console.draft()
var elapsed = 1
setInterval( () => {
  draft('Elapsed', elapsed++, 'seconds')
}, 1000)

console.log('It doesn't matter')
console.log('How \n many \n lines \n ituses')

Or maybe, to show an flow process?

function someAsyncFunction(){ 
  var TAG = '[someAsyncFunction]'
  var log = console.draft(TAG, 'init')

  function a() {
    setTimeout(() => {
      log(TAG, 'calling b')
      b()
    }, 500)
  }

  function b() {
    setTimeout(() => {
      log(TAG, 'finished')
    })
  }
}

You can create your own progress bar, just like "that":

require('draftlog').into(console)

// Input progess goes from 0 to 100
function ProgressBar(progress) {
  // Make it 50 characters length
  var units = Math.round(progress / 2)
  return '[' + '='.repeat(units) + ' '.repeat(50 - units) + '] ' + progress + '%'
}

var barLine = console.draft('Starting download...')
downloadFile(function (progress) {
  barLine(ProgressBar(progress))
})

// Will show something like: (being updated in realtime)
// [============================                      ] 56%

Learn from examples!

We have a few of them ready for you to use! Take a look at the examples folder. Remember to replace require('../') with require('draftlog').

Also, install chalk to get colors on your terminal ;)

Important things to know

Because of the way Terminals are built, it is not possible to update a text outside the viewing area of the terminal.

That said, DraftLogs are setup to automagically be rewritten on a new line if they reach the end of the viewport. Note that, you can disable that behavior, by setting DraftLog.defaults.canReWrite = false

Also, if the NodeJS environment cannot detect the number of rows of your terminal automatically, it will use the default height on DraftLog.defaults.maximumLinesUp. Modify that if needed.

Discouragements

This library is awesome for development, cli tools and what ever you want to created, that is NOT an optmized "slave" server. Please, disable it passing true as a seccond parameter to the DraftLog initialization:

// Disable Initialization (true = production; false = development)
DraftLog(console, true)

Creator

Ivan Seidel

Keywords

FAQs

Package last updated on 07 Mar 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc