• Stars
    star
    1,244
  • Rank 37,776 (Top 0.8 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 8 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

📜 Create mutable log lines into the terminal, and give life to your logs!

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'. This is the building block for any dynamic element such as progress bars, loading status, animations, checkboxes and so on.

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

Look in the examples folders to see how easy it is, to create anything. No strict and fixed widgets are given to you. Instead, use your creativity with this tool to create anything you want! Share the results later with an example ;)

Looking for CUTE Unicode chars? Check out Unicute.

How the HECK is that even possible?

Usage

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

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

// Account for manual line breaks with:
require('draftlog').into(console).addLineListener(process.stdin)

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

// Create a Draft log
var update = console.draft('Hi, 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('Hi, my name is Ivan!')

Here are some interesting examples:

// 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 it uses')

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.

When using into(console).addLineListener(process.stdin), your code will no longer exit automatically, because the stream is being "read". To stop your own code, you can call process.exit(0) or pause the stream when you want with: process.stdin.pause().

Discouragements

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

// Disable Initialization (true = production; false = development)
DraftLog(console, true)
// Or, with one line require-init:
require('draftlog').into(console, true)

Creator

Ivan Seidel

More Repositories

1

IAMDinosaur

🦄 An Artificial Inteligence to teach Google's Dinosaur to jump cactus
JavaScript
2,800
star
2

Is-Now-Illegal

🚫 A NERD protest against Trump's Immigration ban
JavaScript
1,372
star
3

ArduinoThread

⏳ A simple way to run Threads on Arduino
C++
948
star
4

LinkedList

🔗 A fully implemented LinkedList made to work with general Microcontrollers and Arduino projects
C++
346
star
5

DueTimer

⏳ Timer Library fully implemented for Arduino DUE
C++
211
star
6

unicute

💙 Cute Unicode symbols. Make the terminal GREAT AGAIN
57
star
7

ArduinoSensors

A Library of Libraries of Sensors. Infrared, Ultrasonic, Compass and many others, ready to work with ArduinoThread and fully object oriented
C++
54
star
8

Gaussian

Library that makes Gaussian work easy to use with C++ and Arduino
C++
52
star
9

BigBang-js

Recreate the BigBang with JavaScript
JavaScript
48
star
10

Robot-Soccer-Simulator

⚽️ An Open-Source Soccer simulator for virtual robots tournaments
Java
34
star
11

EasyFly

A simple and just another Drone Firmware
C++
25
star
12

3DLiveView-ABB

Application using QT and native OpenGL, with a three based rendering engine to show a 3D ABB robot in sync with an arduino
C++
20
star
13

node-require-smart

The smart way of requiring multiple files in NodeJS
JavaScript
18
star
14

ABBNator

ABB Robots, playing TicTacToe with NodeJS in it's free time
JavaScript
16
star
15

Robot-Rescue-2013

RoboCup Junior Rescue B Robot sharing (CAD, Software, Electronics, materials and all stuff)
C
15
star
16

ArdUI

A generic, simple, easy to use User Interface for Arduino
C++
14
star
17

Robot-Soccer-2013

RoboCup Junior Soccer Robot sharing (CAD, Software, Electronics, materials and all stuff)
C
14
star
18

UTFT

A generic LCD Library for Arduino
C
11
star
19

Nao-Taokei

🚫Um app NERD para gerar gifs Huehue BR
JavaScript
6
star
20

NodePlate

Node boilerplate for servers
JavaScript
6
star
21

Robot-Rescue-2012

RoboCup Junior Rescue B Robot sharing (CAD, Software, Electronics, materials and all stuff)
Java
6
star
22

pcb-libraries

Place to keep and share special Altium, Eagle and Proteus components that we create and share with others.
6
star
23

oscilodrawer

Osciloscope Image drawer with Arduino and Nodejs MQTT
C++
5
star
24

Primo

Primo version using Stepper Motors and custom Software
C++
5
star
25

docker-node-python-opencv

A Dockerfile that installs Node.js, Python and OpenCV
4
star
26

tournamenter-obr

Extensão para o Tournamenter que permite pontuar pelo tablet em competições da OBR
JavaScript
4
star
27

easy-admin

Simple Admin UI with CRUD for Node.js
JavaScript
3
star
28

geddy-rest

REST Api made easy for Geddy
JavaScript
3
star
29

Robot-VerySmall-2015

Very Small Robot for 2015 CBR competition
Python
2
star
30

projetohangar

HTML
2
star
31

i23

An innovative way of connecting and communicating sensors with microcontrollers
2
star
32

node-ssl-vision

RoboCup SSL Vision Client library for Node.JS
JavaScript
2
star
33

Robot-Rescue-2011

Emerotecos Team's robot of Rescue A (OBR) 2011
HTML
1
star
34

Phyto-Gear

A simple Arduino robot that communicates with Facebook to notify that it's happy, sad, with or without light...
Arduino
1
star
35

TemPeixeNoRU

App de leitura de cardápio do RU da UFABC
1
star
36

Chenn

An real time graph as learning object for schools
JavaScript
1
star
37

rifa-generator

Projeto antigo de um Gerador de PDF's de Rifas
JavaScript
1
star
38

tournamenter-wro

Tournamenter Plugin for WRO - World Robotics Olympiad
JavaScript
1
star
39

robocup2014

Robocup 2014 Administration panel for leagues
JavaScript
1
star
40

NoFlow

A (really-simple) flow based programming framework
JavaScript
1
star