| Leo Repp | 58b9f11 | 2021-11-22 11:57:47 +0100 | [diff] [blame^] | 1 | 'use strict'; |
| 2 | var EventEmitter = require('events').EventEmitter; |
| 3 | var fmt = require('util').format; |
| 4 | var inherits = require('util').inherits; |
| 5 | var chalk = require('chalk'); |
| 6 | var consoleStream = require('console-stream'); |
| 7 | var lpadAlign = require('lpad-align'); |
| 8 | |
| 9 | /** |
| 10 | * Initialize a new `Squeak` |
| 11 | * |
| 12 | * @param {Object} opts |
| 13 | * @api public |
| 14 | */ |
| 15 | |
| 16 | function Squeak(opts) { |
| 17 | if (!(this instanceof Squeak)) { |
| 18 | return new Squeak(opts); |
| 19 | } |
| 20 | |
| 21 | EventEmitter.call(this); |
| 22 | |
| 23 | this.opts = opts || {}; |
| 24 | this.align = this.opts.align !== false; |
| 25 | this.indent = this.opts.indent || 2; |
| 26 | this.separator = this.opts.separator || ' : '; |
| 27 | this.stream = this.opts.stream || process.stderr || consoleStream(); |
| 28 | this.types = []; |
| 29 | } |
| 30 | |
| 31 | inherits(Squeak, EventEmitter); |
| 32 | module.exports = Squeak; |
| 33 | |
| 34 | /** |
| 35 | * Write args to stream |
| 36 | * |
| 37 | * @api public |
| 38 | */ |
| 39 | |
| 40 | Squeak.prototype.write = function () { |
| 41 | this.log([].slice.call(arguments)); |
| 42 | return this; |
| 43 | }; |
| 44 | |
| 45 | /** |
| 46 | * Write args and new line to stream |
| 47 | * |
| 48 | * @api public |
| 49 | */ |
| 50 | |
| 51 | Squeak.prototype.writeln = function () { |
| 52 | this.log([].slice.call(arguments)); |
| 53 | this.stream.write('\n'); |
| 54 | return this; |
| 55 | }; |
| 56 | |
| 57 | /** |
| 58 | * Pad and write args to stream |
| 59 | * |
| 60 | * @api public |
| 61 | */ |
| 62 | |
| 63 | Squeak.prototype.writelpad = function () { |
| 64 | var args = [].slice.call(arguments); |
| 65 | var pad = new Array(this.indent + 1).join(' '); |
| 66 | |
| 67 | if (args.length) { |
| 68 | args[0] = pad + args[0]; |
| 69 | } |
| 70 | |
| 71 | this.log(args); |
| 72 | return this; |
| 73 | }; |
| 74 | |
| 75 | /** |
| 76 | * Add type |
| 77 | * |
| 78 | * @param {String} type |
| 79 | * @param {Object} opts |
| 80 | * @param {Function} cb |
| 81 | * @api public |
| 82 | */ |
| 83 | |
| 84 | Squeak.prototype.type = function (type, opts, cb) { |
| 85 | if (!cb && typeof opts === 'function') { |
| 86 | cb = opts; |
| 87 | opts = {}; |
| 88 | } |
| 89 | |
| 90 | opts = opts || {}; |
| 91 | opts.color = opts.color || 'reset'; |
| 92 | opts.prefix = opts.prefix || type; |
| 93 | |
| 94 | this.types.push(opts.prefix); |
| 95 | this[type] = function () { |
| 96 | this.log([].slice.call(arguments), opts); |
| 97 | |
| 98 | if (cb) { |
| 99 | cb(); |
| 100 | } |
| 101 | }; |
| 102 | |
| 103 | return this; |
| 104 | }; |
| 105 | |
| 106 | /** |
| 107 | * End |
| 108 | * |
| 109 | * @param {Function} cb |
| 110 | * @api public |
| 111 | */ |
| 112 | |
| 113 | Squeak.prototype.end = function (cb) { |
| 114 | this.stream.write('\n'); |
| 115 | |
| 116 | if (cb) { |
| 117 | cb(); |
| 118 | } |
| 119 | |
| 120 | return this; |
| 121 | }; |
| 122 | |
| 123 | /** |
| 124 | * Log |
| 125 | * |
| 126 | * @param {Array} args |
| 127 | * @param {Object} opts |
| 128 | * @api private |
| 129 | */ |
| 130 | |
| 131 | Squeak.prototype.log = function (args, opts) { |
| 132 | opts = opts || {}; |
| 133 | |
| 134 | var msg = [fmt.apply(null, args)]; |
| 135 | var color = opts.color; |
| 136 | var prefix = opts.prefix; |
| 137 | |
| 138 | if (prefix) { |
| 139 | var arr = this.align ? this.types : [prefix]; |
| 140 | msg.unshift(chalk[color](lpadAlign(prefix, arr, this.indent))); |
| 141 | } |
| 142 | |
| 143 | this.stream.write(msg.join(this.separator)); |
| 144 | this.stream.write('\n'); |
| 145 | |
| 146 | return this; |
| 147 | }; |