Initial commit
This commit is contained in:
48
themes/keepit/node_modules/draftlog/lib/CSIHelper.js
generated
vendored
Normal file
48
themes/keepit/node_modules/draftlog/lib/CSIHelper.js
generated
vendored
Normal file
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* CSIHelper, or "Control Sequence Introducer Helper" is the translator of actions into
|
||||
* sequence of ANSI characters that will trigger some action in the Terminal.
|
||||
*
|
||||
* Note: CSI is not from the TV Series.
|
||||
*
|
||||
* More info:
|
||||
* - https://en.wikipedia.org/wiki/ANSI_escape_code
|
||||
* - http://tldp.org/HOWTO/Bash-Prompt-HOWTO/x361.html
|
||||
* - http://ascii-table.com/ansi-escape-sequences-vt-100.php
|
||||
*/
|
||||
var CSIHelper = module.exports = {}
|
||||
|
||||
const ESC = CSIHelper.ESC = '\u001b'
|
||||
|
||||
/*
|
||||
* Save current cursor position
|
||||
*/
|
||||
CSIHelper.save = function save() {
|
||||
return '\u001b7'
|
||||
}
|
||||
|
||||
/*
|
||||
* Restore cursor position
|
||||
*/
|
||||
CSIHelper.restore = function restore() {
|
||||
return '\u001b8'
|
||||
}
|
||||
|
||||
/*
|
||||
* Move cursor up `n` lines. Default is 1
|
||||
*/
|
||||
CSIHelper.up = function up(n) {
|
||||
n = typeof n === 'number' ? n : 1
|
||||
return n > 0 ? ESC + '[' + n + 'A' : ''
|
||||
}
|
||||
|
||||
/*
|
||||
* Move cursor down `n` lines. Default is 1
|
||||
*/
|
||||
CSIHelper.down = function down(n) {
|
||||
n = typeof n === 'number' ? n : 1
|
||||
return n > 0 ? ESC + '[' + n + 'B' : ''
|
||||
}
|
||||
|
||||
CSIHelper.clearLine = function clearLine() {
|
||||
return ESC + '[2K' + ESC + '[1G'
|
||||
}
|
||||
118
themes/keepit/node_modules/draftlog/lib/LineCountStream.js
generated
vendored
Normal file
118
themes/keepit/node_modules/draftlog/lib/LineCountStream.js
generated
vendored
Normal file
@@ -0,0 +1,118 @@
|
||||
'use strict'
|
||||
const util = require('util')
|
||||
const stream = require('stream')
|
||||
const PassThrough = stream.PassThrough
|
||||
|
||||
// Expose
|
||||
module.exports = LineCountStream
|
||||
|
||||
/*
|
||||
* This is a PassThrough stream that keep track of how many lines have been logged.
|
||||
*
|
||||
* TODO: Account for terminal size changes/wrapping
|
||||
*/
|
||||
function LineCountStream(outStream) {
|
||||
// Checks if the outStream is already a LineCountStream. If so, return it
|
||||
if (outStream instanceof LineCountStream)
|
||||
return outStream
|
||||
|
||||
// Make sure this is a new instance
|
||||
if (!(this instanceof LineCountStream))
|
||||
return new LineCountStream(outStream)
|
||||
|
||||
// Call super constructor
|
||||
PassThrough.call(this)
|
||||
|
||||
// Save outStream
|
||||
this._outStream = outStream
|
||||
|
||||
// Initialize line and logs (line starts as 1)
|
||||
this._line = 1
|
||||
this._logs = 0
|
||||
|
||||
// Flag indicating a change is being made not with current cursor
|
||||
this._editing = false
|
||||
|
||||
// Pipe this data to output stream
|
||||
outStream && this.pipe(outStream)
|
||||
}
|
||||
|
||||
// Inherits from PassThrough
|
||||
util.inherits(LineCountStream, PassThrough)
|
||||
|
||||
/*
|
||||
* Get current line
|
||||
*/
|
||||
LineCountStream.prototype.line = function line() {
|
||||
return this._line
|
||||
}
|
||||
|
||||
/*
|
||||
* Get log count
|
||||
*/
|
||||
LineCountStream.prototype.logs = function logs() {
|
||||
return this._logs
|
||||
}
|
||||
|
||||
/*
|
||||
* On write, increment lines and logs
|
||||
*
|
||||
* Benchmark: http://jsperf.com/count-the-number-of-characters-in-a-string
|
||||
*/
|
||||
LineCountStream.prototype.write = function write(data) {
|
||||
if (! this._editing) {
|
||||
this.countLines(data)
|
||||
}
|
||||
this.push(data)
|
||||
}
|
||||
|
||||
/*
|
||||
* Binds a inputStream to this, in order to account for extra lines
|
||||
* typed in the Terminal.
|
||||
* This method will bind event 'data' into the stream, and call
|
||||
* countLines(data) with the data. It will not account for '_editing',
|
||||
* because there can be user input while editing.
|
||||
*/
|
||||
LineCountStream.prototype.addLineListener = function addLineListener(inStream) {
|
||||
// Binds to 'data' event
|
||||
inStream.on('data', this.countLines.bind(this))
|
||||
}
|
||||
|
||||
/*
|
||||
* Counts lines on the data and increment counters.
|
||||
* Compensation for `_editing` mode should be done
|
||||
* outside this method. Keep this as clean as possible
|
||||
*/
|
||||
LineCountStream.prototype.countLines = function countLines(data) {
|
||||
var dataLines = (data.toString().split('\n').length - 1)
|
||||
this._logs++
|
||||
this._line += dataLines || 0
|
||||
}
|
||||
|
||||
/*
|
||||
* Call this to stop line counts (during some change of data in previous records)
|
||||
*/
|
||||
LineCountStream.prototype.stopLineCount = function stopLineCount() {
|
||||
this._editing = true
|
||||
}
|
||||
|
||||
/*
|
||||
* Call this to resume line counts
|
||||
*/
|
||||
LineCountStream.prototype.resumeLineCount = function resumeLineCount() {
|
||||
this._editing = false
|
||||
}
|
||||
|
||||
/*
|
||||
* Proxy rows from the stream
|
||||
*/
|
||||
LineCountStream.prototype.rows = function rows() {
|
||||
return this._outStream.rows
|
||||
}
|
||||
|
||||
/*
|
||||
* Proxy columns from the stream
|
||||
*/
|
||||
LineCountStream.prototype.columns = function columns() {
|
||||
return this._outStream.columns
|
||||
}
|
||||
95
themes/keepit/node_modules/draftlog/lib/LogDraft.js
generated
vendored
Normal file
95
themes/keepit/node_modules/draftlog/lib/LogDraft.js
generated
vendored
Normal file
@@ -0,0 +1,95 @@
|
||||
'use strict'
|
||||
const util = require('util')
|
||||
const defaults = require('./defaults')
|
||||
const CSIHelper = require('./CSIHelper')
|
||||
|
||||
// Expose
|
||||
module.exports = LogDraft
|
||||
|
||||
/*
|
||||
* This is a single Line object, that saves it's relative position
|
||||
* in terminal. It is responsible by updating itself.
|
||||
*/
|
||||
function LogDraft(console, methodName) {
|
||||
this._stream = console._stdout
|
||||
this._styleFn = console[methodName]
|
||||
|
||||
// Valid flag. If set to false, should NOT write anymore
|
||||
this.valid = true
|
||||
|
||||
// Save line where content will be saved
|
||||
this.saveLine()
|
||||
}
|
||||
|
||||
/*
|
||||
* After creating a draft, you can call as many times as you want to update it
|
||||
*/
|
||||
LogDraft.prototype.update = function update(/* log arguments */) {
|
||||
// Get line difference
|
||||
var linesUp = this.linesUp()
|
||||
|
||||
// Check if is offscreen
|
||||
if (this.isOffScreen()) {
|
||||
if (defaults.canReWrite) {
|
||||
// It can be rewritten, move line to current cursor line, and keep updating
|
||||
this.saveLine(-1)
|
||||
} else {
|
||||
// Invalidate and prevent writting
|
||||
this.valid = false
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Start editing stream
|
||||
this._stream.stopLineCount()
|
||||
|
||||
// Save state (if content is not null)
|
||||
this._stream.write(CSIHelper.save())
|
||||
|
||||
// Move up cursor up
|
||||
this._stream.write(CSIHelper.up(linesUp))
|
||||
|
||||
// Clear line
|
||||
this._stream.write(CSIHelper.clearLine())
|
||||
|
||||
// Call write function
|
||||
this.write.apply(this, arguments)
|
||||
|
||||
// Restore state
|
||||
this._stream.write(CSIHelper.restore())
|
||||
|
||||
// Resume counting lines
|
||||
this._stream.resumeLineCount()
|
||||
}
|
||||
|
||||
/*
|
||||
* Returns true if line is out of screen
|
||||
*/
|
||||
LogDraft.prototype.isOffScreen = function isOffScren() {
|
||||
var rows = this._stream.rows() || defaults.maximumLinesUp
|
||||
return this._stream.rows() <= this.linesUp()
|
||||
}
|
||||
|
||||
/*
|
||||
* Return how many lines past our current log
|
||||
*/
|
||||
LogDraft.prototype.linesUp = function linesUp() {
|
||||
return this._stream.line() - this._line
|
||||
}
|
||||
|
||||
/*
|
||||
* Writes to the stream by calling the writeFn.
|
||||
* Will not print if it's invalid
|
||||
* Defaults to `_stream.write` (set on constructor)
|
||||
*/
|
||||
LogDraft.prototype.write = function write() {
|
||||
this.valid && this._styleFn.apply(this._styleFn, arguments)
|
||||
}
|
||||
|
||||
/*
|
||||
* Saves current line number as the insertion point
|
||||
*/
|
||||
LogDraft.prototype.saveLine = function saveLine(relative) {
|
||||
relative = relative || 0
|
||||
this._line = this._stream.line() + relative
|
||||
}
|
||||
20
themes/keepit/node_modules/draftlog/lib/defaults.js
generated
vendored
Normal file
20
themes/keepit/node_modules/draftlog/lib/defaults.js
generated
vendored
Normal file
@@ -0,0 +1,20 @@
|
||||
module.exports = {
|
||||
/*
|
||||
* Number of lines after log has been created, to stop updating Log
|
||||
* Default value if console does not support rows counting
|
||||
*/
|
||||
maximumLinesUp: 30,
|
||||
|
||||
/*
|
||||
* ReWrites the line if maximumLinesUp reached, and reset _line
|
||||
*/
|
||||
canReWrite: true,
|
||||
|
||||
/*
|
||||
* Allows automatically binding to process.stdin as a input source.
|
||||
* Set to false, because it prevents the process from exiting,
|
||||
* and that's not cool. Let user decide if it's good to use or not.
|
||||
* Calls lineCountStream.addLineListener(process.stdin) when true
|
||||
*/
|
||||
stdinAutoBind: false,
|
||||
}
|
||||
22
themes/keepit/node_modules/draftlog/lib/index.js
generated
vendored
Normal file
22
themes/keepit/node_modules/draftlog/lib/index.js
generated
vendored
Normal file
@@ -0,0 +1,22 @@
|
||||
'use strict'
|
||||
|
||||
// Lib main object is 'into' method
|
||||
var Lib = require('./into')
|
||||
|
||||
// Expose injecter 'into' method
|
||||
Lib.into = Lib
|
||||
|
||||
// Expose Defaults
|
||||
Lib.defaults = require('./defaults')
|
||||
|
||||
// Expose CSIHelper
|
||||
Lib.CSIHelper = require('./CSIHelper')
|
||||
|
||||
// Expose LogDraft
|
||||
Lib.LogDraft = require('./LogDraft')
|
||||
|
||||
// Expose LineCountStream
|
||||
Lib.LineCountStream = require('./LineCountStream')
|
||||
|
||||
// Expose API
|
||||
module.exports = Lib
|
||||
52
themes/keepit/node_modules/draftlog/lib/into.js
generated
vendored
Normal file
52
themes/keepit/node_modules/draftlog/lib/into.js
generated
vendored
Normal file
@@ -0,0 +1,52 @@
|
||||
'use strict'
|
||||
const defaults = require('./defaults')
|
||||
const LogDraft = require('./LogDraft')
|
||||
const LineCountStream = require('./LineCountStream')
|
||||
|
||||
/*
|
||||
* Injects DrafLog into a console object
|
||||
* call with a seccond parameter as 'true' to
|
||||
* Mock instalation, and add only the `draft` method
|
||||
* as a alias to `console.log`
|
||||
*/
|
||||
module.exports = function into(console, extra) {
|
||||
// If extra is set to `true`, it's production mode
|
||||
var production = (extra === true ? true : false)
|
||||
|
||||
// If production mode, mock api
|
||||
if (production) {
|
||||
// Mock draft and set is as console.log
|
||||
console.draft = function draft() {
|
||||
// Log this
|
||||
console.log.apply(null, arguments)
|
||||
|
||||
// Return usual console.log method
|
||||
return console.log.bind(console)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Transform stdout from console to LineCounter
|
||||
var lineCountStream = LineCountStream(console._stdout)
|
||||
console._stdout = lineCountStream
|
||||
|
||||
// Can it bind to process.stdin automatically?
|
||||
if (defaults.stdinAutoBind) {
|
||||
lineCountStream.addLineListener(process.stdin)
|
||||
}
|
||||
|
||||
// Add "draft" to console
|
||||
console.draft = console.draft || function draft() {
|
||||
// Create Draft at this point in time
|
||||
var logDraft = new LogDraft(console, 'log')
|
||||
|
||||
// Log first
|
||||
logDraft.write.apply(logDraft, arguments)
|
||||
|
||||
// Return update function
|
||||
return logDraft.update.bind(logDraft)
|
||||
}
|
||||
|
||||
// Return the created Transform Stream
|
||||
return lineCountStream
|
||||
}
|
||||
Reference in New Issue
Block a user