"use strict" var hasUnicode = require("has-unicode") var ansi = require("ansi") var align = { center: require("lodash.pad"), left: require("lodash.padend"), right: require("lodash.padstart") } var defaultStream = process.stderr function isTTY() { return process.stderr.isTTY } function getWritableTTYColumns() { // Writing to the final column wraps the line // We have to use stdout here, because Node's magic SIGWINCH handler only // updates process.stdout, not process.stderr return process.stdout.columns - 1 } var ProgressBar = module.exports = function (options, cursor) { if (! options) options = {} if (! cursor && options.write) { cursor = options options = {} } if (! cursor) { cursor = ansi(defaultStream) } this.cursor = cursor this.showing = false this.theme = options.theme || (hasUnicode() ? ProgressBar.unicode : ProgressBar.ascii) this.template = options.template || [ {type: "name", separated: true, length: 25}, {type: "spinner", separated: true}, {type: "startgroup"}, {type: "completionbar"}, {type: "endgroup"} ] this.updatefreq = options.maxUpdateFrequency == null ? 50 : options.maxUpdateFrequency this.lastName = "" this.lastCompleted = 0 this.spun = 0 this.last = new Date(0) var self = this this._handleSizeChange = function () { if (!self.showing) return self.hide() self.show() } } ProgressBar.prototype = {} ProgressBar.unicode = { startgroup: "╢", endgroup: "╟", complete: "█", incomplete: "░", spinner: "▀▐▄▌", subsection: "→" } ProgressBar.ascii = { startgroup: "|", endgroup: "|", complete: "#", incomplete: "-", spinner: "-\\|/", subsection: "->" } ProgressBar.prototype.setTheme = function(theme) { this.theme = theme } ProgressBar.prototype.setTemplate = function(template) { this.template = template } ProgressBar.prototype._enableResizeEvents = function() { process.stdout.on('resize', this._handleSizeChange) } ProgressBar.prototype._disableResizeEvents = function() { process.stdout.removeListener('resize', this._handleSizeChange) } ProgressBar.prototype.disable = function() { this.hide() this.disabled = true } ProgressBar.prototype.enable = function() { this.disabled = false this.show() } ProgressBar.prototype.hide = function() { if (!isTTY()) return if (this.disabled) return this.cursor.show() if (this.showing) this.cursor.up(1) this.cursor.horizontalAbsolute(0).eraseLine() this.showing = false } var repeat = function (str, count) { var out = "" for (var ii=0; ii